Reputation: 9240
I have two baseclasses; "MemoryManagedObject" and "Gameobject". The idea is to have my own classes inherit from "Gameobject", which inherits from "MemoryManagedObject".
#ifndef _GAME_OBJECT_H
#define _GAME_OBJECT_H
#include "MemoryManagedObject.h"
class GameObject : public MemoryManagedObject
{
public:
GameObject() { }
GameObject(bool UseMemPool): MemoryManagedObject(UseMemPool) { }
virtual ~GameObject() { }
long GetGameObjectID();
protected:
long mGameObjectID;
};
inline long GameObject::GetGameObjectID()
{
return mGameObjectID;
}
#endif
#ifndef _MEMORY_MANAGED_OBJECT_H
#define _MEMORY_MANAGED_OBJECT_H
#include <new>
class MemoryManagedObject
{
public:
MemoryManagedObject();
MemoryManagedObject(bool UseMemPool);
virtual ~MemoryManagedObject() { DecreaseRefCount(); }
bool IsAllocatedWithMemPool();
void* operator new(size_t size);
void* operator new(size_t size, bool UseMemPool);
void operator delete(void* obj);
long GetRefCount();
private:
void IncreaseRefCount();
void DecreaseRefCount();
long mRefCount;
bool mAllocatedWithMemPool;
};
#endif
However my compiler (android-ndk, gcc 4.4.1 afaik) throws me "Undefined Reference" linker errors in functions: ~MemoryManagedObject and ~GameObject: undefined reference to MemoryManagedObject::delete(void* obj) and MemoryManagedObject::DecreaseRefCount
Why is this? I have written all the methods in .cpp files which are included in the compilation; I believe there is something wrong with how I declare the virtual destructos but I don't know why.
EDIT: Posting the testclass and the cpp files:
TestClass1.h
#ifndef _TEST_CLASS_1_H
#define _TEST_CLASS_1_H
#include "GameObject.h"
class TestClass1 : public GameObject
{
public:
TestClass1();
TestClass1(bool UseMemPool);
~TestClass1();
void TestMe();
};
TestClass1.cpp
#include "TestClass1.h"
TestClass1::TestClass1()
{
}
TestClass1::TestClass1(bool UseMemPool): GameObject(UseMemPool)
{
}
TestClass1::~TestClass1()
{
}
void TestClass1::TestMe()
{
}
#endif
GameObject.cpp
#include "GameObject.h"
GameObject::GameObject()
{
}
GameObject::GameObject(bool UseMemPool): MemoryManagedObject(UseMemPool)
{
}
GameObject::~GameObject()
{
MemoryManagedObject.cpp
#include "MemoryManagedObject.h"
#include "Engine.h"
#include <stdint.h>
MemoryManagedObject::MemoryManagedObject()
{
mAllocatedWithMemPool = false;
IncreaseRefCount();
}
MemoryManagedObject::MemoryManagedObject(bool UseMemPool)
{
mAllocatedWithMemPool = UseMemPool;
IncreaseRefCount();
}
MemoryManagedObject::~MemoryManagedObject()
{
DecreaseRefCount();
}
long MemoryManagedObject::GetRefCount()
{
return mRefCount;
}
void MemoryManagedObject::IncreaseRefCount()
{
mRefCount++;
}
void MemoryManagedObject::DecreaseRefCount()
{
mRefCount--;
if (mRefCount <= 0)
{
delete this;
}
}
bool MemoryManagedObject::IsAllocatedWithMemPool()
{
return mAllocatedWithMemPool;
}
void* MemoryManagedObject::operator new(size_t size)
{
Engine* engine = Engine::GetEngine();
void* alloc;
alloc = engine->GetMemoryManager()->Allocate(size);
return alloc;
}
void* MemoryManagedObject::operator new(size_t size, bool UseMemPool)
{
Engine* engine = Engine::GetEngine();
void* alloc;
alloc = engine->GetMemoryManager()->Allocate(size, UseMemPool);
return alloc;
}
void MemoryManagedObject::operator delete(void* obj)
{
Engine* engine = Engine::GetEngine();
MemoryManagedObject* memObj = (MemoryManagedObject*)obj;
engine->GetMemoryManager()->Deallocate(obj,memObj->IsAllocatedWithMemPool());
}
}
Upvotes: 0
Views: 167
Reputation: 682
You are writing your virtual destructors inline, which will typically cause them to be emitted in every compilation unit, as weak symbols. In addition, since the first (and only) virtual function in your classes is the destructor, that will be the key-function, with which the vtable will emitted.
I'd guess that these two things are interacting badly, causing inline destructors not to be included in the final link.
In any case, the solution is to move the definition of your virtual destructors out-of-line, into the cpp file. Unless you declare instances of your objects on the stack, the desctructors will never be called except through the vtable anyway.
Upvotes: 1