talloc and threaded programs

Phil Mayers p.mayers at imperial.ac.uk
Fri Jun 20 08:20:38 MDT 2014


I'm trying to troubleshoot some memory corruption issues in a threaded 
program using talloc, and I wanted to check a few things.

First - the talloc page says a context must either be used by one thread 
or synchronised. Am I correct in assuming that this applies at one 
"level" of the context tree only i.e. this is illegal:

/* main thread */
c = talloc(toplevel, struct C);
/* thread #2 */
d = talloc(toplevel, struct C);

...but this is legal:

/* main thread */
c = talloc(toplevel, struct C);
/* thread #2 */
d = talloc(c, struct C);
/* main thread */
e = talloc(toplevel, struct C);

...because thread2 is not touching "toplevel"?

Similarly, this is legal:

/* main thread */
c = talloc(toplevel, struct C);
/* thread #2 */
d = talloc(c, struct C);
e = talloc(d, struct C);
talloc_free(e);

...but this is illegal:

/* main thread */
c = talloc(toplevel, struct C);
/* thread #2 */
d = talloc(c, struct C);
talloc_free(d); /* will mutate "c"
/* main thread */
e = talloc(c, struct C);

Basically - I'm ok to use (but not delete) children of a context in a 
different thread?

Second - given that the code I'm looking at must be doing something 
wrong, has anyone written a macro/wrapper that adds a mutex/trylock to 
talloc for debugging purposes, to detect unlocked uses of a context? It 
would save me writing it.

I've tried valgrind and lots of other debugging techniques but there are 
obviously unlocked or double-frees going on that are corrupting the 
heap, and by the time the app crashes it's too late to determine where 
in the code this is happening. If anyone has any ideas for detecting 
this, I'd be grateful.


More information about the samba-technical mailing list