Problems with dynamic library


#1

Guys, I'm having some problems with my dynamic library module system and I really need some help, please?

What happens is simple, if I load the library, close, load again, close, around the 4th time, it gives me Access Violation and crashes, and I can't figure out why.
It seems that I'm either leaving something behind or deleting something twice, but I just can't find what. And trust me, I looked for 3 days non-stop, this is driving me nuts...

The library sdk can be seen here:
https://github.com/Wusik4000/Wusik_4000_SDK/blob/master/Wusik4000mod.h

So I just create a new module from this using the following code:
https://github.com/Wusik4000/Wusik_4000_SDK/blob/master/Modules/Examples/Gain%20Effect/Source/Gain%20Effect.h

Them, inside the main host, the one that uses all those modules, I just use the code to create a new module:
// typedef W4kMod* (*CreateNewModule) (W4kMod::LayerInfo*, W4kMod::GlobalInfo*);
// DynamicLibrary dynlib;
dynlib.open(thefile);
CreateNewModule w4kMod = (CreateNewModule) dynlibMaster.getFunction("createNewModule");
wusikModule = w4kMod(&layerInfo, &globalInfo);

Now, wusikModule holds the actual module dynlib stuff that I use. I use the ScopedPointer for the wusikModule. This works, but as I said, after deleting the module a few times, something bad happens.

If anyone could give me some light on this issue, I would be forever thankful. :-)

Cheers, WilliamK @ Wusik.com


#2

samples in SampleDataZones seems fishy

 

http://stackoverflow.com/questions/4670782/the-difference-between-delete-and-delete-in-c

 

 


#3

If it's not used, then you're probably do the same mistake as other places which trash the memory from time to time leading to your crashes


#4

Ah, sorry, I'm not using that class, I already removed but forgot to re-upload the new file... ;-) I'm not doing any delete, using only the OwnArray or ScopedPointer


#5

And I'm using AudioSampleBuffer for the samples in the sample-zones. But it crashes when I don't even use that...


#6

The most common memory pitfall when using DLLs is that each module has its own heap, so if you allocate something (with new/malloc), then pass it to another module where it gets deleted using a different heap, that'll cause all kinds of horribleness.


#7

Thanks Jules. But from what I could tell I'm not deleting anything in a different heap.

Wusik 4000 Host loads the library, the library creates memory using ScopedPointer and OwnArray. Wusik 4000 do not delete any of those. But when it closes/deletes the library it calls the destructor of the library class which handles things out.

Did you look the library sdk I linked?


#8

Ok, guys, give me a hand so I can understand things up. On the dynamic library I use the OwnedArray to create an array of parameters, this happens in the library constructor. This is cleared up during destruction. So, is this bad? Could you give me some pointers on this?

When the class is deleted from memory from the host, the class destructor class is called, but this happens from the host heap? So this is why the whole thing is messed up? I can't really use OwnedArray for that?


#9

I found this article, will this help me out?

http://www.codeproject.com/Articles/594671/How-to-ensure-proper-dynamic-library-boundary-cros


#10

But I still don't get it...

1) Wusik 4000 is a VSTi, so it is a plugin, a DLL. It creates, just like any other VST, several new memory mappings. No problems, since it is been created in the VST and Destructed in the VST.

2) My modules are DLL libraries that runs inside the Wusik 4000 VST DLL. They also create and destruct the pointers itself, Wusik 4000 never does that directly.

3) what gives them? Is the VST format different somehow and I missed the point? I'm still very lost... I could just get rid of arrays and any sort of dynamic mapped memory on the modules, but I wonder about the String() class... Anyway, still lost on all this...


#11

Ok, I think I got this right now. I'm creating the pointer inside the DLL but I'm deleting it outside. So I need to delete it inside too, using this:

extern "C" EXPORTED_FN void deleteModule(W4kMod* module)    { delete module; }

#12

I'm starting to think that I'm still missing something, as I still get access violation after a few loads/unloads... :-(

So far here's my export C functions...

extern "C" EXPORTED_FN W4kMod* createNewModule(
    W4kMod::LayerInfo* _layerInfo, W4kMod::GlobalInfo* _globalInfo)
    { return (W4kMod*) new MODNAME(_layerInfo, _globalInfo); }
    
    extern "C" EXPORTED_FN const char* getModuleDescription()
    { return MODULE_DESCRIPTION; }
    
    extern "C" EXPORTED_FN uint32 getSpecialFlags()
    { return MODULE_SPECIAL_FLAGS; }
    
    extern "C" EXPORTED_FN void deleteModule(W4kMod* module)
    { delete module; }

#13

Ok, I finally found all the problems, and it was memory heap related indeed. Thanks again for pointing that out! :-)