I’m writing a small plugin system for a program. Some parts of it are done already, loading the file and calling the constructor function works.
In one of the functions I need to pass some (class-)pointers back to the handler, that’s where I get a seg-fault.
in program:
class RenderInterface {
public:
RenderInterface();
virtual ~RenderInterface();
void RegisterBufferInterface(BufferInterface* interface)
{
bInterface = interface; // <---- this is where segfault occurs
}
void RegisterCameraInterface(CameraInterface* interface){}
void RegisterRenderInterface(RenderInterface* interface){}
static RenderInterface* GetSingletonPtr()
{
return _singleton;
}
private:
static RenderInterface* _singleton;
BufferInterface* bInterface;
CameraInterface* cInterface
RenderInterface* rInterface;
};
RenderInterface::_singleton is set to 0 elsewhere.
in registration function (in dll):
class BInterface : public BufferInterface {
public:
... various stuff ....
}
class GLPlugin : public Plugin {
public:
Plugin() : bInterface(0) {}
~Plugin(){}
void Initialize() // <--- is called after dll has been loaded
{
bInterface = new BInterface();
InterfaceManager::GetSingletonPtr()->RegisterBufferInterface(bInterface); // segfault
// register other stuff
}
private:
BufferInterface* bInterface;
};
What am I missing for this to work? Is there another way to do it?
Edit: missed the * after BufferInterface while simplifying the code, thanks Luchian
Well, it crashes because you never initialize your singleton:
I’m assuming you initialize
_singletontoNULLin your implementation file, in which case you’d need:Some other hints:
private?bInterface = new BInterface();is illegal, sincebInterfaceis an object, not a pointer.