When this will be freed?

Jacob Carlborg doob at me.com
Sat Apr 5 12:10:25 PDT 2014


On 2014-04-05 15:08, "Marc Schütz" <schuetzm at gmx.net>" wrote:

> Yes, but it doesn't necessarily contain `s` anymore. Today's compilers
> are intelligent enough to see that `s` is never used after the function
> call, and therefore don't even allocate a stack slot for it.

Ok, I see.

> `foo` could be implemented like this (it's a C function, so `in` boils
> down to `const` without `scope`):
>
> char *b;
> void foo (const char *a) {
>      b = a;
>      // do something complex that causes all the registers to be reused
>      // => the only reference to the string is now in b, outside of the
> GC's view
>      // --> GC collects here <--
>      printf(b); // the string may have been collected here
> }

Of course, if the C function is storing the parameter in a global 
variable you got problems. You really need to be sure of what the C 
functions is doing. To be on the safe side there's always GC.addRoot.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list