Yes the Kernel and Interfaces need to be deleted too...

This commit is contained in:
HMH 2017-07-21 19:46:31 +02:00
parent ae411710ba
commit bfc5700314
4 changed files with 47 additions and 28 deletions

View file

@ -2615,14 +2615,14 @@ void CClient::CheckVersionUpdate()
void CClient::RegisterInterfaces()
{
Kernel()->RegisterInterface(static_cast<IDemoRecorder*>(&m_DemoRecorder[RECORDER_MANUAL]));
Kernel()->RegisterInterface(static_cast<IDemoPlayer*>(&m_DemoPlayer));
Kernel()->RegisterInterface(static_cast<IServerBrowser*>(&m_ServerBrowser));
Kernel()->RegisterInterface(static_cast<IFetcher*>(&m_Fetcher));
Kernel()->RegisterInterface(static_cast<IDemoRecorder*>(&m_DemoRecorder[RECORDER_MANUAL]), false);
Kernel()->RegisterInterface(static_cast<IDemoPlayer*>(&m_DemoPlayer), false);
Kernel()->RegisterInterface(static_cast<IServerBrowser*>(&m_ServerBrowser), false);
Kernel()->RegisterInterface(static_cast<IFetcher*>(&m_Fetcher), false);
#if !defined(CONF_PLATFORM_MACOSX) && !defined(__ANDROID__)
Kernel()->RegisterInterface(static_cast<IUpdater*>(&m_Updater));
Kernel()->RegisterInterface(static_cast<IUpdater*>(&m_Updater), false);
#endif
Kernel()->RegisterInterface(static_cast<IFriends*>(&m_Friends));
Kernel()->RegisterInterface(static_cast<IFriends*>(&m_Friends), false);
Kernel()->ReregisterInterface(static_cast<IFriends*>(&m_Foes));
}
@ -2700,7 +2700,7 @@ void CClient::Run()
bool RegisterFail = false;
RegisterFail = RegisterFail || !Kernel()->RegisterInterface(static_cast<IEngineGraphics*>(m_pGraphics)); // register graphics as both
RegisterFail = RegisterFail || !Kernel()->RegisterInterface(static_cast<IGraphics*>(m_pGraphics));
RegisterFail = RegisterFail || !Kernel()->RegisterInterface(static_cast<IGraphics*>(m_pGraphics), false);
if(RegisterFail || m_pGraphics->Init() != 0)
{
@ -3544,7 +3544,7 @@ int main(int argc, const char **argv) // ignore_convention
CClient *pClient = CreateClient();
IKernel *pKernel = IKernel::Create();
pKernel->RegisterInterface(pClient);
pKernel->RegisterInterface(pClient, false);
pClient->RegisterInterfaces();
// create the components
@ -3566,26 +3566,31 @@ int main(int argc, const char **argv) // ignore_convention
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pConfig);
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IEngineSound*>(pEngineSound)); // register as both
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<ISound*>(pEngineSound));
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<ISound*>(pEngineSound), false);
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IEngineInput*>(pEngineInput)); // register as both
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IInput*>(pEngineInput));
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IInput*>(pEngineInput), false);
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IEngineTextRender*>(pEngineTextRender)); // register as both
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<ITextRender*>(pEngineTextRender));
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<ITextRender*>(pEngineTextRender), false);
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IEngineMap*>(pEngineMap)); // register as both
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IMap*>(pEngineMap));
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IMap*>(pEngineMap), false);
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IEngineMasterServer*>(pEngineMasterServer)); // register as both
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IMasterServer*>(pEngineMasterServer));
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IMasterServer*>(pEngineMasterServer), false);
RegisterFail = RegisterFail || !pKernel->RegisterInterface(CreateEditor());
RegisterFail = RegisterFail || !pKernel->RegisterInterface(CreateGameClient());
RegisterFail = RegisterFail || !pKernel->RegisterInterface(CreateGameClient(), false);
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pStorage);
if(RegisterFail)
{
delete pKernel;
pClient->~CClient();
mem_free(pClient);
return -1;
}
}
pEngine->Init();
@ -3657,6 +3662,8 @@ int main(int argc, const char **argv) // ignore_convention
// write down the config and quit
pConfig->Save();
delete pKernel;
pClient->~CClient();
mem_free(pClient);
return 0;

View file

@ -37,7 +37,7 @@ public:
class IKernel
{
// hide the implementation
virtual bool RegisterInterfaceImpl(const char *InterfaceName, IInterface *pInterface) = 0;
virtual bool RegisterInterfaceImpl(const char *InterfaceName, IInterface *pInterface, bool destroy) = 0;
virtual bool ReregisterInterfaceImpl(const char *InterfaceName, IInterface *pInterface) = 0;
virtual IInterface *RequestInterfaceImpl(const char *InterfaceName) = 0;
public:
@ -46,9 +46,9 @@ public:
// templated access to handle pointer convertions and interface names
template<class TINTERFACE>
bool RegisterInterface(TINTERFACE *pInterface)
bool RegisterInterface(TINTERFACE *pInterface, bool destroy = true)
{
return RegisterInterfaceImpl(TINTERFACE::InterfaceName(), pInterface);
return RegisterInterfaceImpl(TINTERFACE::InterfaceName(), pInterface, destroy);
}
template<class TINTERFACE>
bool ReregisterInterface(TINTERFACE *pInterface)

View file

@ -2685,19 +2685,22 @@ int main(int argc, const char **argv) // ignore_convention
{
bool RegisterFail = false;
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pServer); // register as both
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pServer);
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pEngine);
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IEngineMap*>(pEngineMap)); // register as both
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IMap*>(pEngineMap));
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IMap*>(pEngineMap), false);
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pGameServer);
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pConsole);
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pStorage);
RegisterFail = RegisterFail || !pKernel->RegisterInterface(pConfig);
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IEngineMasterServer*>(pEngineMasterServer)); // register as both
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IMasterServer*>(pEngineMasterServer));
RegisterFail = RegisterFail || !pKernel->RegisterInterface(static_cast<IMasterServer*>(pEngineMasterServer), false);
if(RegisterFail)
{
delete pKernel;
return -1;
}
}
pEngine->Init();
@ -2734,14 +2737,7 @@ int main(int argc, const char **argv) // ignore_convention
pServer->Run();
// free
delete pServer;
delete pKernel;
delete pEngineMap;
delete pGameServer;
delete pConsole;
delete pEngineMasterServer;
delete pStorage;
delete pConfig;
return 0;
}

View file

@ -17,10 +17,12 @@ class CKernel : public IKernel
{
m_aName[0] = 0;
m_pInterface = 0x0;
m_AutoDestroy = false;
}
char m_aName[64];
IInterface *m_pInterface;
bool m_AutoDestroy;
};
CInterfaceInfo m_aInterfaces[MAX_INTERFACES];
@ -43,8 +45,21 @@ public:
m_NumInterfaces = 0;
}
virtual ~CKernel()
{
// delete interfaces in reverse order just the way it would happen to objects on the stack
for (int i = m_NumInterfaces - 1; i >= 0 ; i--)
{
if (m_aInterfaces[i].m_AutoDestroy)
{
delete m_aInterfaces[i].m_pInterface;
m_aInterfaces[i].m_pInterface = 0;
}
}
}
virtual bool RegisterInterfaceImpl(const char *pName, IInterface *pInterface)
virtual bool RegisterInterfaceImpl(const char *pName, IInterface *pInterface, bool destroy)
{
// TODO: More error checks here
if(!pInterface)
@ -68,6 +83,7 @@ public:
pInterface->m_pKernel = this;
m_aInterfaces[m_NumInterfaces].m_pInterface = pInterface;
str_copy(m_aInterfaces[m_NumInterfaces].m_aName, pName, sizeof(m_aInterfaces[m_NumInterfaces].m_aName));
m_aInterfaces[m_NumInterfaces].m_AutoDestroy = destroy;
m_NumInterfaces++;
return true;