free causes exception

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 26 11:34:22 PST 2016


On 01/26/2016 06:20 AM, Igor wrote:
 > I have successfully malloc'ed an object but when I go to free it in the
 > destructor I get an exception. The destructor simply has
 >
 > ~this() // destructor for Foo
 > {
 >      core.stdc.stdlib.free(&this);
 > }

That design suggests a complexity regarding object responsibilities: 
Assuming that the object was constructed on a piece of memory that it 
did *not* allocate, the memory was owned by somebody else. In that case 
and in general, freeing the memory should be the responsibility of that 
other somebody as well.

Even if it is acceptable, you must also make sure that opAssign() and 
post-blit do the right thing: no two object should own the same piece of 
memory.

Ali



More information about the Digitalmars-d-learn mailing list