View mode: basic / threaded / horizontal-split · Log in · Help
March 31, 2012
Return a class instance as a pointer
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.
March 31, 2012
Re: Return a class instance as a pointer
On Sat, 31 Mar 2012 17:02:43 +0200, 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.
>

try
return cast(void*)new Bleep(sample_rate, features);
March 31, 2012
Re: Return a class instance as a pointer
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
March 31, 2012
Re: Return a class instance as a pointer
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.
March 31, 2012
Re: Return a class instance as a pointer
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;
March 31, 2012
Re: Return a class instance as a pointer
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).
March 31, 2012
Re: Return a class instance as a pointer
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
March 31, 2012
Re: Return a class instance as a pointer
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.
Top | Discussion index | About this forum | D home