Skip to content

Commit

Permalink
Call CTO2GameClientShell's destructor correctly.
Browse files Browse the repository at this point in the history
On Linux, the destructor is called when the program closes
instead of when libCShell.so is unloaded, resulting in
crashes. Instead, allocate CTO2GameClientShell on the heap when
the library is loaded, then delete it in CClientMgr::TermClientShellDE.

We also need to set a flag in CConsole when this happens, as PrintString
incorrectly thinks i_client_shell is not null and would call OnConsolePrint
from a null pointer when CClientMgr::Term calls ClientStringWhine.
  • Loading branch information
rohit-n committed Oct 15, 2021
1 parent ee5b3c2 commit e05f7bb
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 7 deletions.
15 changes: 14 additions & 1 deletion NOLF2/ClientShellDLL/TO2/GlobalsInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,18 @@ define_holder(ILTTexInterface,g_pTexInterface);

SETUP_CLIENTSHELL();

#ifndef __LINUX
define_interface(CTO2GameClientShell, IClientShell);

#else
static CAPIInstanceDefines<CTO2GameClientShell>
__impl_CTO2GameClientShell_defines_IClientShell_Default__(
new CTO2GameClientShell,
"IClientShell" "." "Default",
IClientShell::_IClientShell_VERSION_);

static SStaticSearchInterface
__var_search_IClientShell_CTO2GameClientShell_Default_ = {
SEARCH_MARKER_INTERFACE, SEARCH_MARKER_INT, "IClientShell",
"CTO2GameClientShell", "Default",
IClientShell::_IClientShell_VERSION_};
#endif
5 changes: 0 additions & 5 deletions runtime/client/src/clientde_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1441,11 +1441,6 @@ LTRESULT CLTClient::RemoveObject(HLOCALOBJ hObj)
RETURN_ERROR(1, CLTClient::RemoveObject, LT_INVALIDPARAMS);
}

if (!g_pClientMgr)
{
RETURN_ERROR(1, CLTClient::RemoveObject, LT_CANTREMOVESERVEROBJECT);
}

LTObject *pObject = (LTObject*)hObj;
if (pObject->m_ObjectID != (uint16)-1)
{
Expand Down
6 changes: 6 additions & 0 deletions runtime/client/src/clientmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,12 @@ void CClientMgr::TermClientShellDE()
{
bm_UnbindModule(m_hShellModule);
m_hShellModule = NULL;
#ifdef __LINUX
delete i_client_shell;
i_client_shell = NULL;
con_client_shell_is_gone();
#endif

}

if (m_hClientResourceModule)
Expand Down
7 changes: 7 additions & 0 deletions runtime/client/src/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ void con_WhitePrintf(const char *pMsg, ...)
va_end( marker );
}

#ifdef __LINUX
void con_client_shell_is_gone()
{
GETCONSOLE()->ClientShellIsGone();
}
#endif

void con_OnKeyPress(uint32 key)
{
if (dsi_IsConsoleEnabled ())
Expand Down
3 changes: 3 additions & 0 deletions runtime/client/src/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ void con_DrawSmall(int nLines);
void con_PrintString(CONCOLOR theColor, int filterLevel, const char *pMsg);
void con_Printf(CONCOLOR theColor, int filterLevel, const char *pMsg, ...);
void con_WhitePrintf(const char *pMsg, ...);
#ifdef __LINUX
void con_client_shell_is_gone();
#endif

#endif // __CONSOLE_H__

8 changes: 7 additions & 1 deletion runtime/client/src/sys/linux/linuxconsole_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ CConsole::CConsole()

m_bInitialized = true;
m_bInitTerminate = false;
m_bClientShellIsGone = false;

m_Tex = NULL;
m_Font = NULL;
Expand Down Expand Up @@ -1328,7 +1329,7 @@ void CConsole::PrintString(CONCOLOR theColor, int filterLevel, const char *pMsg)

// Send the info we're using for output to the client shell so the
// game code can do whatever it wants with it.
if (i_client_shell != NULL) {
if (!m_bClientShellIsGone) {
CConsolePrintData PrintData;

// turn the CONCOLOR into an LTRGB
Expand Down Expand Up @@ -1473,6 +1474,11 @@ void CConsole::OnKeyPress(uint32 key)
}
}

void CConsole::ClientShellIsGone()
{
m_bClientShellIsGone = true;
}

uint32 GetInterfaceSurfaceMemory()
{
return 0;
Expand Down
7 changes: 7 additions & 0 deletions runtime/client/src/sys/linux/linuxconsole_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ class CConsole
// haven't been loaded
bool m_bSaveVariablesMask;

//On Linux, the client shell is heap allocated and deleted in CClientMgr::TermClientShellDE.
//For some reason, PrintString does not correctly see this, and calls OnConsolePrint to a NULL pointer.
//Instead, check this.
bool m_bClientShellIsGone;

protected:

enum EConState {
Expand Down Expand Up @@ -430,6 +435,8 @@ class CConsole

void OnKeyPress( uint32 key );

void ClientShellIsGone();

};

extern bool IsKeyDown(uint16 vkKey);
Expand Down

0 comments on commit e05f7bb

Please sign in to comment.