Search code examples
macospluginsnew-operator32bit-64bitdelete-operator

Why would memory alloc/freeing fail in a 32-bit plug-in running in a 64-bit app but not in a 32-bit app?


I'm developing audio plug-ins for both Windows and Mac (VST and AU), and I've run into a problem that only occurs when opening the plug-in in a 64-bit host DAW (in my case Reaper) in OSX 10.8.

The plug-in is a universal binary build and works fine in 32-bit hosts, but opening it in Reaper 64-bit causes it to crash on random new/delete instructions that allocate filter objects. I tried building it as a 64-bit plug-in just to test, but the same thing happened.

What could cause this to happen?

Edit: Here is a screen capture of the crash report: enter image description here


Solution

  • I found the issue causing the random memory error when using a 64-bit host. There was a memset function that referenced the size of the pointer, declared as float, to clear it's contents. Of course on a 64-bit host the pointer size is 64 bits and so the memset function corrupted the heap, leading to the fickle memory errors.

    This was the memset function:

    float *z;
    //...
    memset(z, 0, sizeof(z)*size);
    

    The intention was that should z eventually declared a double, it would be automatic in this function to adjust the block of memory allocated. I overlooked what would happen in a 64-bit context.