www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Return a class instance as a pointer

reply "Timo =?UTF-8?B?V2VzdGvDpG1wZXIi?= <timo.westkamper gmail.com> writes:
I am trying to compile the following code in D

void* instantiate(_LV2_Descriptor* descriptor,
   double sample_rate, char * bundle_path,
   LV2_Feature** features) {
   Plugin plugin = new Bleep(sample_rate, features);
   return &plugin;
}

but I get the following error

../src/lv2/plugin.d(38): Error: escaping reference to local plugin

What is the right way to create a class instance and a return it 
as a pointer? Also in such a way that it is not claimed by GC. 
There is another callback for object deletion.
Mar 31 2012
next sibling parent simendsjo <simendsjo gmail.com> writes:
On Sat, 31 Mar 2012 17:02:43 +0200, Timo Westk=C3=A4mper  =

<timo.westkamper gmail.com> wrote:

 I am trying to compile the following code in D

 void* instantiate(_LV2_Descriptor* descriptor,
    double sample_rate, char * bundle_path,
    LV2_Feature** features) {
    Plugin plugin =3D new Bleep(sample_rate, features);
    return &plugin;
 }

 but I get the following error

 ../src/lv2/plugin.d(38): Error: escaping reference to local plugin

 What is the right way to create a class instance and a return it as a =

 pointer? Also in such a way that it is not claimed by GC. There is  =

 another callback for object deletion.

try return cast(void*)new Bleep(sample_rate, features);
Mar 31 2012
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
On 2012-03-31 17:02, "Timo Westkämper" <timo.westkamper gmail.com>" wrote:
 I am trying to compile the following code in D

 void* instantiate(_LV2_Descriptor* descriptor,
 double sample_rate, char * bundle_path,
 LV2_Feature** features) {
 Plugin plugin = new Bleep(sample_rate, features);
 return &plugin;
 }

 but I get the following error

 ../src/lv2/plugin.d(38): Error: escaping reference to local plugin

 What is the right way to create a class instance and a return it as a
 pointer? Also in such a way that it is not claimed by GC. There is
 another callback for object deletion.

Why would you want to return an object as a pointer? All classes are reference types. But if you actually do want to return an object as a pointer you can just cast it to void*. -- /Jacob Carlborg
Mar 31 2012
prev sibling next sibling parent "Timo =?UTF-8?B?V2VzdGvDpG1wZXIi?= <timo.westkamper gmail.com> writes:
On Saturday, 31 March 2012 at 15:29:15 UTC, Jacob Carlborg wrote:
 On 2012-03-31 17:02, "Timo Westkämper" 
 <timo.westkamper gmail.com>" wrote:
 I am trying to compile the following code in D

 void* instantiate(_LV2_Descriptor* descriptor,
 double sample_rate, char * bundle_path,
 LV2_Feature** features) {
 Plugin plugin = new Bleep(sample_rate, features);
 return &plugin;
 }

 but I get the following error

 ../src/lv2/plugin.d(38): Error: escaping reference to local 
 plugin

 What is the right way to create a class instance and a return 
 it as a
 pointer? Also in such a way that it is not claimed by GC. 
 There is
 another callback for object deletion.

Why would you want to return an object as a pointer? All classes are reference types. But if you actually do want to return an object as a pointer you can just cast it to void*.

I am writing a D binding for the LV2 audio plugin API. The instantiate function expects a void* return. I wouldn't use void* casts in D code which doesn't interface with C. The cast worked, thanks.
Mar 31 2012
prev sibling next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 3/31/12, "Timo Westk=E4mper\" <timo.westkamper gmail.com>" puremagic.com
 Also in such a way that it is not claimed by GC.

Probably save the reference somewhere. I think by just returning a void* to a C function the GC will think all references to the objects are gone and will eventually try to collect it. Perhaps you could keep a hash of objects: __gshared[Object] _store;
Mar 31 2012
parent Jacob Carlborg <doob me.com> writes:
On 2012-03-31 17:44, Andrej Mitrovic wrote:
 On 3/31/12, "Timo Westkšmper\"<timo.westkamper gmail.com>" puremagic.com
 Also in such a way that it is not claimed by GC.

Probably save the reference somewhere. I think by just returning a void* to a C function the GC will think all references to the objects are gone and will eventually try to collect it. Perhaps you could keep a hash of objects: __gshared[Object] _store;

I think the correct way is to use core.memory.GC.addRoot. -- /Jacob Carlborg
Mar 31 2012
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 3/31/12, Andrej Mitrovic <andrej.mitrovich gmail.com> wrote:
 Perhaps you could keep a hash of objects:
 __gshared[Object] _store;

Err that should be: __gshared void[0][Object] _store; Or something like that. Not sure if the void[0] part is right, but basically it's used to make set types (hashes with no values).
Mar 31 2012
prev sibling parent "Timo =?UTF-8?B?V2VzdGvDpG1wZXIi?= <timo.westkamper gmail.com> writes:
On Saturday, 31 March 2012 at 15:52:07 UTC, Jacob Carlborg wrote:
 On 2012-03-31 17:44, Andrej Mitrovic wrote:
 On 3/31/12, "Timo 
 Westkämper\"<timo.westkamper gmail.com>" puremagic.com
 Also in such a way that it is not claimed by GC.

Probably save the reference somewhere. I think by just returning a void* to a C function the GC will think all references to the objects are gone and will eventually try to collect it. Perhaps you could keep a hash of objects: __gshared[Object] _store;

I think the correct way is to use core.memory.GC.addRoot.

Thanks, I will try that.
Mar 31 2012