GC.collect bug ?

Namespace rswhite4 at googlemail.com
Sat Sep 14 10:30:03 PDT 2013


On Saturday, 14 September 2013 at 17:22:51 UTC, sclytrack wrote:
> On Friday, 13 September 2013 at 16:43:01 UTC, Namespace wrote:
>> Another use case of delete, which is unfortunately deprecated. 
>> ;)
>> ----
>> import std.stdio;
>> import core.memory;
>>
>> class A {
>> 	~this() { writeln(`dtor`); };
>> };
>>
>> void main() {
>> 	auto a = new A;
>> 	delete a;
>> 	writeln(`after dtor`);
>> }
>> ----
>>
>> Ooutput:
>> dtor
>> after dtor
>
>
> //http://dlang.org/memory.html
>
>
> import std.c.stdlib;
> import core.exception;
> import core.memory : GC;
> import std.stdio;
>
>
>
> class TObject
> {
>     new(size_t sz)
>     {
>         void* p;
>
>         p = std.c.stdlib.malloc(sz);
>
>         if (!p)
>             throw new OutOfMemoryError();
>
>         GC.addRange(p, sz);
>         writeln("Memory Allocation", sz);
>         return p;
>     }
>
>     delete(void* p)
>     {
>         if (p)
>         {
>             GC.removeRange(p);
>             std.c.stdlib.free(p);
> 	    writeln("Memory Removal");
>         }
>     }
> }
>
> class TComponent: TObject
> {
> 	int number;
> }
>
> int main()
> {
>         TComponent obj = new TComponent();
>         delete obj;
> 	writeln("testing the code");
> 	return 0;
> }
>
>
> //What is the replacement for delete?

AFAIK Andrei would like to see if D get rid of new and delete 
overloading.
Especially since delete is deprecated.

The replacement is 'destroy'. It also call the DTor but it does 
not free the memory. As soon as delete is fully removed we have 
to trust the broken GC.


More information about the Digitalmars-d-learn mailing list