Explicitly avoid GC of objects?
Robert M. Münch
robert.muench at saphirion.com
Tue May 21 11:54:08 UTC 2019
The D docs for interfacing with C state:
If pointers to D garbage collector allocated memory are passed to C
functions, it's critical to ensure that that memory will not be
collected by the garbage collector before the C function is done with
it. This is accomplished by:
1. Making a copy of the data using core.stdc.stdlib.malloc() and
passing the copy instead.
2. Leaving a pointer to it on the stack (as a parameter or automatic
variable), as the garbage collector will scan the stack.
3. Leaving a pointer to it in the static data segment, as the garbage
collector will scan the static data segment.
4. Registering the pointer with the garbage collector with the
std.gc.addRoot() or std.gc.addRange() calls.
1 and 4 are pretty clear.
Is there a trick to accomplish 2 when objects are created from
different scopes which need to be kept? So, I have one function
creating the objects and one using them. How can I keep things on the
stack between these two functions?
How is 3 done? Is this only useful for static variables?
--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
More information about the Digitalmars-d-learn
mailing list