GC.collect bug ?

sclytrack sclytrack at idiot.com
Sat Sep 14 10:22:50 PDT 2013


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?


More information about the Digitalmars-d-learn mailing list