Re: Adding pointers to GC with destructers

2015-04-19 Thread Ali Çehreli via Digitalmars-d-learn

On 04/19/2015 04:38 PM, Freddy wrote:

C libraries have a pattern of

HiddenType* getObj();
void freeObj(HiddenType*);

Is there any way I can make the GC search for a "HiddenType*" and run
"freeObj" when the pointer is not found.


Not automatically. Check out addRange and addRoot:

  http://dlang.org/phobos/core_memory.html

Ali



Re: Adding pointers to GC with destructers

2015-04-19 Thread ketmar via Digitalmars-d-learn
On Sun, 19 Apr 2015 23:38:47 +, Freddy wrote:

> C libraries have a pattern of 
> HiddenType* getObj();
> void freeObj(HiddenType*);
> 
> Is there any way I can make the GC search for a "HiddenType*" and run
> "freeObj" when the pointer is not found.

wrap it in class/struct and run `freeObj()` in destructor.

signature.asc
Description: PGP signature


Re: Adding pointers to GC with destructers

2015-04-20 Thread Freddy via Digitalmars-d-learn

On Monday, 20 April 2015 at 02:56:35 UTC, Ali Çehreli wrote:

Not automatically. Check out addRange and addRoot:

  http://dlang.org/phobos/core_memory.html

Ali

The destructor doesn't seem to be running

import std.stdio;
import std.c.stdlib;
import core.memory;
struct Test{
~this(){
writeln("free: ",&this);
free(&this);
}
}

void main(){
auto ptr=cast(Test*)malloc(4);
writeln("create: ",ptr);
GC.addRange(ptr,0,typeid(Test));
ptr=null;
GC.collect();
}

$ rdmd test
create: 1EEC730


Re: Adding pointers to GC with destructers

2015-04-20 Thread Ali Çehreli via Digitalmars-d-learn

On 04/20/2015 02:48 PM, Freddy wrote:

On Monday, 20 April 2015 at 02:56:35 UTC, Ali Çehreli wrote:

Not automatically. Check out addRange and addRoot:

  http://dlang.org/phobos/core_memory.html

Ali

The destructor doesn't seem to be running


Sorry, I misunderstood you. You can use a RAII object as ketmar said:

import std.stdio;
import std.c.stdlib;

struct Test{
void* ptr;

~this(){
writeln("free: ",&this);
free(ptr);
}
}

void main(){
auto ptr=cast(Test*)malloc(4);
writeln("create: ",ptr);

auto test = Test(ptr);  // <-- The dtor of this object will free
}

Ali



Re: Adding pointers to GC with destructers

2015-04-20 Thread Freddy via Digitalmars-d-learn

On Monday, 20 April 2015 at 22:24:53 UTC, Ali Çehreli wrote:

On 04/20/2015 02:48 PM, Freddy wrote:

On Monday, 20 April 2015 at 02:56:35 UTC, Ali Çehreli wrote:

Not automatically. Check out addRange and addRoot:

 http://dlang.org/phobos/core_memory.html

Ali

The destructor doesn't seem to be running


Sorry, I misunderstood you. You can use a RAII object as ketmar 
said:


import std.stdio;
import std.c.stdlib;

struct Test{
void* ptr;

~this(){
writeln("free: ",&this);
free(ptr);
}
}

void main(){
auto ptr=cast(Test*)malloc(4);
writeln("create: ",ptr);

auto test = Test(ptr);  // <-- The dtor of this object will 
free

}

Ali
I believe your original understanding was right.I want to have 
HiddenType* be garbage collected and finalized(freed,destructed) 
when no more references to it are found(stack or heap). I could 
use reference counting but it seems inefficient for a program 
rarely collecting and constantly coping.


Re: Adding pointers to GC with destructers

2015-04-20 Thread Martin Nowak via Digitalmars-d-learn

On Sunday, 19 April 2015 at 23:38:49 UTC, Freddy wrote:

C libraries have a pattern of

HiddenType* getObj();
void freeObj(HiddenType*);

Is there any way I can make the GC search for a "HiddenType*" 
and run "freeObj" when the pointer is not found.


You can't turn an arbitrary pointer into a garbage collected 
object.

What you can do, is putting the pointer into a GCed object.

class Wrapper {
  this(HiddenType* p) { _p = p; } }
  ~this() { freeObj(_p); }
  alias _p this;
}

auto obj = new Wrapper(getObj());

Since 2.067.0 we also finalize heap allocated structs, so the 
wrapper can also be a struct.