Custom delete not getting called
Ali Çehreli
acehreli at yahoo.com
Fri Jun 24 23:11:40 PDT 2011
On Sat, 25 Jun 2011 07:52:36 +0200, Daniel Gibson wrote:
> Am 25.06.2011 07:50, schrieb Ali Çehreli:
>> On Sat, 25 Jun 2011 07:36:21 +0200, Daniel Gibson wrote:
>>
>>> Am 25.06.2011 06:33, schrieb Ali Çehreli:
>>>> On Sat, 25 Jun 2011 06:24:31 +0200, Andrej Mitrovic wrote:
>>>>
>>>>> These are going away, I don't know why they're still being kept in
>>>>> the docs.
>>>>
>>>> If I understood correctly, you mean that the custom new and delete
>>>> operators for user defined types are going away? If so, I am wasting
>>>> my time writing something about those. :)
>>>>
>>>> Is there a replacement for the custom new and delete?
>>>
>>> emplace() (and clear() to call the destructor)
>>
>> I am aware of the discussions about delete going away. I always thought
>> that it was about the 'delete o;' usage that was going away.
>>
>> emplace() is to construct an object at a location that you already
>> have.
>>
>> clear() is to call the destructor.
>>
>> Custom new and delete are (were?) different:
>>
>> - Custom new gets called to give you a chance to allocate the memory
>> for an object (Note that this is different than emplace() where you
>> want to construct the object on memory that is already available.)
>>
>> - Custom delete is the reverse of custom new. (And this is not clear(),
>> as when custom delete is called, the object has already been
>> destroyed.)
>>
>>
> emplace() and clear() are the functions you'll use in your custom new
> and delete implementation :-)
> See http://pastebin.com/9Qgf3vc7 for a simple example.
You have this code:
T myNew(T, Args...) (Args args) {
// get class size of class object in bytes
size_t clSize = __traits(classInstanceSize, T);
// allocate memory for the object
void* tmp = core.stdc.stdlib.malloc( clSize );
if(!tmp)
throw new Exception("no memory");
// slice it to become a void[]
void[] objMem = tmp[0..clSize];
// use std.conv.emplace to put Object into that memory
T ret = emplace!(T, Args)(objMem, args);
return ret; // return new custom allocated Object
}
void myDelete(T)(ref T obj) {
clear(obj); // so destructor is called
// free memory of the object
core.stdc.stdlib.free(cast(void*)obj);
// shouldn't hurt, the object must not be used anymore anyway
obj = null;
}
void main() {
Foo f = myNew!Foo(42);
writefln("f.x = %s", f.x);
myDelete(f);
assert(f is null); // yeah it's null.
writefln("bye.");
}
Sure: emplace() constructs an object on the memory you provide and clear
() calls the destructor.
> Of course this is not 100% the same thing as custom new/delete was, but
> it gives you more flexibility, e.g. you can use it with any class (not
> just custom classes) and you can even use several different allocators
> (including D's default garbage collected new) at the same time for the
> same class (of course then you need to be really careful to use the
> proper deallocator).
I agree that the code above is more flexible, but you must remember to
call myNew!Foo and myDelete in order to use those. They will not be
called when a library function calls new to allocate my objects. (On the
other hand, I don't think that I care. :) C++ has these too and I've
never worked in a project that needed them.)
The member custom new and delete do get called to just allocate the
memory for anybody who wants to dynamically construct a Foo object. I
don't know whether there is going to be a replacement for the current
member new and member delete.
I am happy if they are gone... :) I just wanted to know.
> Cheers,
> - Daniel
Ali
More information about the Digitalmars-d
mailing list