Tuesday, September 6, 2011

Something you should know about talloc

Talloc is an excellent memory management system for C that provides hierarchical memory pools with other cool tricks like destructors. It's written by Tridge for Samba and I really like it. If you are writing a complex system in C you could do worse than to replace your calls to malloc with calls to talloc.

So that's talloc, but the thing you really should know about talloc is right there at the bottom of the project page. In particular:


when using talloc_enable_leak_report(), giving directly NULL as a parent context implicitly refers to a hidden "null context" global variable, so this should not be used in a multi-threaded environment without proper synchronization.

I've spent many days recently hunting down a bug, the bug would have been much easier to find if I had read the above line. Suddenly I was sharing contexts all over the place and very very rarely there'd be a synchronization problem that would lead to a null pointer deref. 

By the way talloc_enable_leak_report() is an excellent feature of talloc. Excellent.