digitalmars.D - Return a class instance as a pointer
- "Timo =?UTF-8?B?V2VzdGvDpG1wZXIi?= <timo.westkamper gmail.com> (12/12) Mar 31 2012 I am trying to compile the following code in D
- simendsjo (5/17) Mar 31 2012 =
- Jacob Carlborg (6/18) Mar 31 2012 Why would you want to return an object as a pointer? All classes are
- "Timo =?UTF-8?B?V2VzdGvDpG1wZXIi?= <timo.westkamper gmail.com> (5/29) Mar 31 2012 I am writing a D binding for the LV2 audio plugin API. The
- Andrej Mitrovic (6/7) Mar 31 2012 Probably save the reference somewhere. I think by just returning a
- Jacob Carlborg (4/11) Mar 31 2012 I think the correct way is to use core.memory.GC.addRoot.
- "Timo =?UTF-8?B?V2VzdGvDpG1wZXIi?= <timo.westkamper gmail.com> (2/16) Mar 31 2012 Thanks, I will try that.
- Andrej Mitrovic (5/7) Mar 31 2012 Err that should be:
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
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
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
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 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.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*.
Mar 31 2012
On 3/31/12, "Timo Westk=E4mper\" <timo.westkamper gmail.com>" puremagic.comAlso 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
On 2012-03-31 17:44, Andrej Mitrovic wrote:On 3/31/12, "Timo Westkämper\"<timo.westkamper gmail.com>" puremagic.comI think the correct way is to use core.memory.GC.addRoot. -- /Jacob CarlborgAlso 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
On Saturday, 31 March 2012 at 15:52:07 UTC, Jacob Carlborg wrote:On 2012-03-31 17:44, Andrej Mitrovic wrote:Thanks, I will try that.On 3/31/12, "Timo Westkämper\"<timo.westkamper gmail.com>" puremagic.comI think the correct way is to use core.memory.GC.addRoot.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
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