Need help with DynamicLibrary


#1

Hello,

I generated a DLL using the Dynamic Library project from the introjucer and I succeeded in opening it and calling a function from it using the DynamicLibrary class. However, I am running into the follow error that I don't understand how to deal with.

HEAP[hellodllopenerDBG.exe]: HEAP: Free Heap block 00F0B828 modified at 00F0DD58 after it was freed

The code in my sample application just looks like this

void initialise (const String& commandLine) override
{
    // This method is where you should put your application's initialisation code..
    DynamicLibrary test;
    test.open("hello.dll");
    test.close();

    mainWindow = new MainWindow (getApplicationName());
}

 

And my DLL code

.h file

#ifdef HELLOFUNCDLL_EXPORTS
#define HELLOFUNCDLL_API __declspec(dllexport) 
#else
#define HELLOFUNCDLL_API __declspec(dllimport) 
#endif

#include "JuceHeader.h"

namespace HelloFunc
{
    class MyHelloFunc
    {
        public:
            static HELLOFUNCDLL_API int hello();
    };
}

.c file

#include "hellofunc.h"

namespace HelloFunc
{
    int MyHelloFunc::hello()
    {
        return 42;
    }
}

 

I'm probably doing a whole bunch of things wrong but I'm new with DLLs and I can't find any examples. Any advice is appreciated. Thanks!


#2

The general advice is that DLLs and C++ classes are a bad combination - you really need to understand the way the language bindings work and the way memory management works very deeply.

There are lots of articles out on the web that will tell you why not to use C++ and give you tips about this. It's not a JUCE-specific thing, but it might be worth looking at juce_Memory.h, line 101


#3

Above all, if memory is allocated in the DLL, make sure it gets deallocated in the DLL. That said, if you want easy access to C++ classes in a DLL, you can use the abstract class method (http://stackoverflow.com/questions/1516476/how-to-create-some-class-from-dllconstructor-in-dll%D1%81/1516579#1516579), which is akin towards COM, and it might in fact be quite a neat way to handle object lifetimes by having a base interface that has reference counting. All this can be hidden in the DLL though.