Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
April 19, 2015 Adding pointers to GC with destructers | ||||
---|---|---|---|---|
| ||||
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. |
April 20, 2015 Re: Adding pointers to GC with destructers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Freddy | 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 |
April 20, 2015 Re: Adding pointers to GC with destructers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Freddy Attachments: | On Sun, 19 Apr 2015 23:38:47 +0000, 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.
|
April 20, 2015 Re: Adding pointers to GC with destructers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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 |
April 20, 2015 Re: Adding pointers to GC with destructers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Freddy | 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
|
April 20, 2015 Re: Adding pointers to GC with destructers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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.
|
April 21, 2015 Re: Adding pointers to GC with destructers | ||||
---|---|---|---|---|
| ||||
Posted in reply to Freddy | 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.
|
Copyright © 1999-2021 by the D Language Foundation