Derelict SFML destructor crashes

Jeremy DeHaan dehaan.jeremiah at gmail.com
Mon Dec 17 02:56:22 PST 2012


On Monday, 17 December 2012 at 09:52:08 UTC, Jacob Carlborg wrote:
> On 2012-12-17 09:23, Jeremy DeHaan wrote:
>
>> And how does calling destroy/delete in a struct destructor 
>> differ from
>> doing the same in a class destructor? I too would like to make 
>> sure I am
>> not getting any memory leaks!
>
> Because there are guarantees how and when a destructor for a 
> struct is called. You cannot rely on a destructor for a class 
> when it's called, in what order they are called or if they're 
> called at all.
>
> import std.stdio;
>
> struct Foo
> {
>     ~this () { writeln("destroying Foo"); }
> }
>
> class Bar
> {
>     ~this () { writeln("destroying Bar"); }
> }
>
> void foobar ()
> {
>     auto foo = Foo();
>     auto bar = new Bar();
>     // the destructor of "foo" _will always_ be called here
>     // the destructor of "bar" _may_ be called here
> }
>
> void main ()
> {
>     foobar();
> }

Thanks! This is also good to know!

Does that mean that putting some form of delete in a struct 
destructor will be called to delete contents or is that still 
considered unsafe?


And why don't classes have the same guarantee? Is it because they 
are a reference type and thus handled by the GC rather than being 
a value type?


More information about the Digitalmars-d-learn mailing list