Using emplace leading to memory corruption

monarch_dodra monarchdodra at gmail.com
Sun Sep 29 03:50:17 PDT 2013


On Sunday, 29 September 2013 at 08:21:34 UTC, monarch_dodra wrote:
> I didn't see anything obviously wrong with your code.

Did some more toying around. I did notice an issue in emplace, 
but nothing that would explain what you are observing.

By replacing your block with:

//----
     version(EMPLACE)
     {
       //Allocation
       import core.stdc.stdlib;
       enum size_t size = __traits(classInstanceSize, Foo);
       auto tmp = cast(byte*)core.stdc.stdlib.malloc(size);
       if (!tmp)
         throw new Exception("Memory allocation failed");
       assert (cast(size_t)tmp % 16 == 0);

       //Result
       auto result = cast(Foo)tmp;

       //Construction
       auto mem = tmp[0..size];
       auto init = typeid(Foo).init;
       assert(init.ptr);
       mem[] = init[];
       result.__ctor();

       //Finished building, assign
       _foo = result;
     }
//----

I'm observing the same behavior (Killed by signal 11).

I *think* the code is bug free, so I'm leaning towards a code gen 
bug.

Another thing I noticed is that if I call a class function on a 
null instance, I'm not getting a NullObjectError. I thought I was 
supposed to...?


More information about the Digitalmars-d mailing list