how to allocate class without gc?

Igor via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jan 25 21:47:42 PST 2016


On Tuesday, 26 January 2016 at 05:11:54 UTC, Mike Parker wrote:
> On Tuesday, 26 January 2016 at 01:09:50 UTC, Igor wrote:
>> Is there any examples that shows how to properly allocate an 
>> object of a class type with the new allocators and then 
>> release it when desired?
>
> Allocate a block of memory big enough to hold an instance of 
> your class using whichever allocator you need, then instantiate 
> a class instance with std.conv.emplace.
>
> http://p0nce.github.io/d-idioms/#Placement-new-with-emplace

I created a class using this example. But my code is now failing. 
It seems one can't just replace new with this and expect it to 
work?

What is happening is some fields(strings) are not retaining their 
value.

ubyte[__traits(classInstanceSize, App)] buffer;
auto app = cast(App)emplace!App(buffer[]);
//auto app = new App();

Basically the comment is the original. When I finally call 
createWindow, it fails because the string representing the name 
inside App is null... Which doesn't happen when I use new.

Should it work as expected(which it isn't) or do I have to also 
emplace all the fields and such so they are not for some reason 
released?



More information about the Digitalmars-d-learn mailing list