GC: finalization order?!

Steven Schveighoffer schveiguy at yahoo.com
Sun Feb 20 08:14:54 PST 2011


On Sun, 20 Feb 2011 07:15:06 -0500, Martin Kinkelin <noone at spam.com> wrote:

> I came to the same conclusion. Even if Parent is a struct, it may get
> destructed after its Child (e.g., in case the Parent struct is a field
> of another class).
> What I did is to use a custom allocator for Child, so that Child
> instances are not managed by the GC:
>
> ----------
> import std.stdio;
> import std.c.stdlib;
> import core.exception;
>
> private class Child
> {
>     new(size_t size)
>     {
>         writeln("Child.new()");
>         void* p = malloc(size);
>         if (!p)
>             onOutOfMemoryError();
>         return p;
>     }
>     delete(void* p)
>     {
>         writeln("Child.delete()");
>         if (p)
>             free(p);
>     }


I hate to deflate your bubble, but this feature (custom handlers for new  
and delete) is going to be deprecated.

However, you can reinflate with the intended replacement.  Essentially,  
you will move allocation handling outside of child and move it to Parent.   
Check out emplace (not sure what module it is).

-Steve


More information about the Digitalmars-d-learn mailing list