quuxplusone.github.io quuxplusone.github.io

Replacing `std::lock_guard` with a factory function

Recently I had to track down a use-after-free bug (which see) in some code that used librdkafka. The gist of it was: We call rd_kafka_new and pass it an rk_conf object containing a bunch of pointers to callbacks. rd_kafka_new either succeeds, in which case it gives us back a valid rd_kafka_t* and we make sure to keep those callback objects alive; or it fails (e.g. due to invalid configuration or resource exhaustion), in which case it...