stupid talloc questions

Volker Lendecke Volker.Lendecke at SerNet.DE
Thu Sep 24 10:25:14 MDT 2009

```On Thu, Sep 24, 2009 at 04:31:01PM +0200, Holger Hetterich wrote:
> void *context;
> char *a;
> a = talloc (context, char *);
> if ( a!= NULL) { /*do something */ }
> talloc_free(context);
>
> My understanding is that talloc gives me an "unlimited"
> number of memory under a context.
> So freeing context should free a.
>
> Is this correct?

Yes. Although the call a = talloc(context, char *) is wrong.
You would use "a = talloc_array(context, char, <stringlength>);

> Next:
>         void *context;
>         char *a;
>         char *b;
>         a = talloc (context, char *);
>         b = talloc (context, char *);
>         if ( a!=NULL && b!=NULL ) { /*do something */ }
>         talloc_free(context);
>
> If what is said above is true, in this case, a and b should be freed. Correct?

Well, you haven't assigned context yet. If you had "context
= talloc_new(...)" before you used it in a = and b =, it
would be true.

> Next:
>       void *context;
>       char *a;
>       char *b;
>       char *c;
>       a = talloc (context, char *);
>       b = talloc (a, char *);
>       c = talloc (b, char *);
>       /* do something */
>       talloc_free(b);
>
> Is this possible? According to the docs, the returned
> value is itself a talloc context.
> So freeing b should result in freeing b and c. Correct?

Same error as in the last example, context was never
initialized. But if it was (even set to NULL), you're right.

> Next:
>
> char *a;
> a = talloc ( talloc_tos(), char *);
> if ( a!=NULL) /* do something */
>
> I suppose tos() means top-of-stack. When and how will this memory be freed?
> Do I have to use talloc_free on this? And if so, with which context?

talloc_tos() is indeed a top-of-stack thing, where
talloc_stackframe() and TALLOC_FREE form an implicit stack
of talloc contexts. You can (and also should) explicitly
TALLOC_FREE(a), the talloc_tos() stack thingy is mainly a
safeguard against programming errors. Eventually this will
be cleaned up in the main smbd loop. So you can't rely on it
to persist once you leave your local routine.

Volker
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: Digital signature
URL: <http://lists.samba.org/pipermail/samba-technical/attachments/20090924/667c7b41/attachment.pgp>
```