Freeing of memory (garbage collection)

Christopher Wright dhasenan at gmail.com
Tue Dec 9 15:18:51 PST 2008


Daniel White wrote:
>> That would be a bad idea.  Then how would you do manual memory
>> management in the few cases that absolutely require it?
> 
> Two ways. Either:
> 
> a: being able to lock the variable so that the garbage collector
> can't touch it until you unlock it.

If you have a reference to the memory, the GC won't collect it. Unless 
you make a habit of hiding pointers inside non-pointer types, that is, 
which can result in undefined behavior if the hidden pointer points to 
GC-allocated memory.

If you don't have any reference to that memory, then the garbage 
collector can free it, but in that case, I don't see why it would be an 
issue, most of the time. If you have a destructor, just be careful -- 
for instance, if you have an open file in that object and its destructor 
closes that file.

> b: Using a slightly different version of malloc (say 'mallocl') which again,
> makes it shielded against the garbage collector's wrath.

malloc is C stdlib malloc. The garbage collector won't touch it.


More information about the Digitalmars-d-learn mailing list