fix memory leaks; get curses using new invite resp. code

This commit is contained in:
Eric House 2022-09-22 17:06:26 -07:00
parent 766554d3f5
commit 7587653541
12 changed files with 68 additions and 67 deletions

View file

@ -370,6 +370,8 @@ game_makeFromStream( MPFORMAL XWEnv xwe, XWStreamCtxt* stream,
XW_UtilCtxt* util, DrawCtx* draw, CommonPrefs* cp,
const TransportProcs* procs )
{
LOG_FUNC();
XP_ASSERT( gi == util->gameInfo );
XP_Bool success = XP_FALSE;
XP_U8 strVersion;
#ifndef XWFEATURE_STANDALONE_ONLY
@ -655,10 +657,8 @@ game_dispose( XWGame* game, XWEnv xwe )
static void
disposePlayerInfoInt( MPFORMAL CurGameInfo* gi )
{
XP_U16 ii;
LocalPlayer* lp;
for ( lp = gi->players, ii = 0; ii < MAX_NUM_PLAYERS; ++lp, ++ii ) {
for ( int ii = 0; ii < gi->nPlayers; ++ii ) {
LocalPlayer* lp = &gi->players[ii];
XP_FREEP( mpool, &lp->name );
XP_FREEP( mpool, &lp->password );
XP_FREEP( mpool, &lp->dictName );
@ -676,8 +676,6 @@ gi_disposePlayerInfo( MPFORMAL CurGameInfo* gi )
void
gi_copy( MPFORMAL CurGameInfo* destGI, const CurGameInfo* srcGI )
{
XP_MEMSET( destGI, 0, sizeof(*destGI) );
replaceStringIfDifferent( mpool, &destGI->dictName,
srcGI->dictName );
XP_STRNCPY( destGI->isoCodeStr, srcGI->isoCodeStr, VSIZE(destGI->isoCodeStr)-1 );

View file

@ -159,7 +159,7 @@ mpool_destroy( MemPoolCtx* mpool )
MemPoolEntry* entry;
for ( entry = mpool->usedList; !!entry; entry = entry->next ) {
#ifndef FOR_GREMLINS /* I don't want to hear about this right now */
XP_LOGF( "%s: " XP_P " index=%d, in %s, ln %d of %s\n", __func__,
XP_LOGFF( "ptr: " XP_P "; index=%d, allocated %s, ln %d of %s\n",
entry->ptr, entry->index,
entry->func, entry->lineNo, entry->fileName );
#ifdef DEBUG
@ -299,12 +299,11 @@ void
mpool_free( MemPoolCtx* mpool, void* ptr, const char* file,
const char* func, XP_U32 lineNo )
{
MemPoolEntry* entry;
MemPoolEntry* prev;
MEMPOOL_SYNC_START(mpool);
entry = findEntryFor( mpool, ptr, &prev );
MemPoolEntry* entry = findEntryFor( mpool, ptr, &prev );
if ( !entry ) {
XP_LOGFF( "findEntryFor failed; pool %p, line %d in %s", mpool, lineNo, file );

View file

@ -1917,7 +1917,6 @@ client_readInitialMessage( ServerCtxt* server, XWEnv xwe, XWStreamCtxt* stream )
if ( accepted ) {
ModelCtxt* model = server->vol.model;
CurGameInfo* gi = server->vol.gi;
CurGameInfo localGI;
XP_U32 gameID;
PoolContext* pool;
#ifdef STREAM_VERS_BIGBOARD
@ -1940,10 +1939,11 @@ client_readInitialMessage( ServerCtxt* server, XWEnv xwe, XWStreamCtxt* stream )
server->vol.gi->gameID = gameID;
comms_setConnID( server->vol.comms, gameID );
XP_MEMSET( &localGI, 0, sizeof(localGI) );
CurGameInfo localGI = {0};
gi_readFromStream( MPPARM(server->mpool) stream, &localGI );
localGI.serverRole = SERVER_ISCLIENT;
XP_ASSERT( !localGI.dictName );
localGI.dictName = copyString( server->mpool, gi->dictName );
gi_copy( MPPARM(server->mpool) gi, &localGI );

View file

@ -136,6 +136,12 @@ static void relay_requestJoin_curses( void* closure, const XP_UCHAR* devID,
XP_U16 nPlayersTotal, XP_U16 seed, XP_U16 lang );
#endif
static void disposeBoard( CursesBoardGlobals* bGlobals );
static void initCP( CommonGlobals* cGlobals );
static CursesBoardGlobals* commonInit( CursesBoardState* cbState,
sqlite3_int64 rowid,
const CurGameInfo* gip );
CursesBoardState*
cb_init( CursesAppGlobals* aGlobals, LaunchParams* params,
CursesMenuState* menuState, OnGameSaved onGameSaved )
@ -204,29 +210,28 @@ cb_new( CursesBoardState* cbState, const cb_dims* dims )
}
void
cb_newFor( CursesBoardState* XP_UNUSED(cbState), const NetLaunchInfo* XP_UNUSED(nli),
const CommsAddrRec* XP_UNUSED(returnAddr),
cb_newFor( CursesBoardState* cbState, const NetLaunchInfo* nli,
const cb_dims* XP_UNUSED(dims) )
{
/* FIX ME soon */
XP_ASSERT(0);
/* LaunchParams* params = cbState->params; */
/* CurGameInfo gi = {0}; */
/* gi_copy( MPPARM(params->mpool) &gi, &params->pgi ); */
/* gi_setNPlayers( &gi, nli->nPlayersT, nli->nPlayersH ); */
/* gi.gameID = nli->gameID; */
/* XP_STRNCPY( gi.isoCodeStr, nli->isoCodeStr, VSIZE(gi.isoCodeStr) ); */
/* gi.forceChannel = nli->forceChannel; */
/* gi.inDuplicateMode = nli->inDuplicateMode; */
/* gi.serverRole = SERVER_ISCLIENT; /\* recipient of invitation is client *\/ */
/* replaceStringIfDifferent( params->mpool, &gi.dictName, nli->dict ); */
LaunchParams* params = cbState->params;
/* CursesBoardGlobals* bGlobals = findOrOpen( cbState, -1, &gi, returnAddr ); */
CommsAddrRec selfAddr;
makeSelfAddress( &selfAddr, params );
/* gi_disposePlayerInfo( MPPARM(params->mpool) &gi ); */
CursesBoardGlobals* bGlobals =
commonInit( cbState, -1, NULL );
CommonGlobals* cGlobals = &bGlobals->cGlobals;
initCP( cGlobals );
/* enableDraw( bGlobals, dims ); */
/* setupBoard( bGlobals ); */
if ( game_makeFromInvite( &cGlobals->game, NULL_XWE, nli, &selfAddr,
cGlobals->util, (DrawCtx*)NULL,
&cGlobals->cp, (TransportProcs*)NULL ) ) {
linuxSaveGame( cGlobals );
} else {
XP_ASSERT( 0 );
}
disposeBoard( bGlobals );
}
const MenuList g_allMenuList[] = {
@ -526,19 +531,10 @@ setupBoard( CursesBoardGlobals* bGlobals )
board_draw( board, NULL_XWE );
}
static CursesBoardGlobals*
initNoDraw( CursesBoardState* cbState, sqlite3_int64 rowid,
const CurGameInfo* gi, const CommsAddrRec* returnAddr )
static void
initCP( CommonGlobals* cGlobals )
{
LOG_FUNC();
CursesBoardGlobals* result = commonInit( cbState, rowid, gi );
CommonGlobals* cGlobals = &result->cGlobals;
LaunchParams* params = cGlobals->params;
if ( !!returnAddr ) {
cGlobals->hostAddr = *returnAddr;
}
const LaunchParams* params = cGlobals->params;
cGlobals->cp.showBoardArrow = XP_TRUE;
cGlobals->cp.showRobotScores = params->showRobotScores;
cGlobals->cp.hideTileValues = params->hideValues;
@ -555,6 +551,21 @@ initNoDraw( CursesBoardState* cbState, sqlite3_int64 rowid,
#ifdef XWFEATURE_ROBOTPHONIES
cGlobals->cp.makePhonyPct = params->makePhonyPct;
#endif
}
static CursesBoardGlobals*
initNoDraw( CursesBoardState* cbState, sqlite3_int64 rowid,
const CurGameInfo* gi, const CommsAddrRec* returnAddr )
{
LOG_FUNC();
CursesBoardGlobals* result = commonInit( cbState, rowid, gi );
CommonGlobals* cGlobals = &result->cGlobals;
if ( !!returnAddr ) {
cGlobals->hostAddr = *returnAddr;
}
initCP( cGlobals );
if ( linuxOpenGame( cGlobals, &result->procs ) ) {
result = ref( result );

View file

@ -42,7 +42,7 @@ void cb_resized( CursesBoardState* cbState, const cb_dims* dims );
void cb_open( CursesBoardState* cbState, sqlite3_int64 rowid, const cb_dims* dims );
bool cb_new( CursesBoardState* cbState, const cb_dims* dims );
void cb_newFor( CursesBoardState* cbState, const NetLaunchInfo* nli,
const CommsAddrRec* returnAddr, const cb_dims* dims );
const cb_dims* dims );
bool cb_feedRow( CursesBoardState* cbState, sqlite3_int64 rowid,
XP_U16 expectSeed, const XP_U8* buf, XP_U16 len,

View file

@ -1177,8 +1177,7 @@ onJoined( void* closure, const XP_UCHAR* connname, XWHostID hid )
#endif
void
inviteReceivedCurses( void* closure, const NetLaunchInfo* invite,
const CommsAddrRec* returnAddr )
inviteReceivedCurses( void* closure, const NetLaunchInfo* invite )
{
CursesAppGlobals* aGlobals = (CursesAppGlobals*)closure;
sqlite3_int64 rowids[1];
@ -1193,7 +1192,7 @@ inviteReceivedCurses( void* closure, const NetLaunchInfo* invite,
if ( doIt ) {
cb_dims dims;
figureDims( aGlobals, &dims );
cb_newFor( aGlobals->cbState, invite, returnAddr, &dims );
cb_newFor( aGlobals->cbState, invite, &dims );
} else {
XP_LOGFF( "Not accepting duplicate invitation (nRowIDs(gameID=%d) was %d",
invite->gameID, nRowIDs );
@ -1205,9 +1204,7 @@ static void
relayInviteReceivedCurses( void* closure, const NetLaunchInfo* invite )
{
CursesAppGlobals* aGlobals = (CursesAppGlobals*)closure;
CommsAddrRec addr = {0};
nli_makeAddrRec( invite, &addr );
inviteReceivedCurses( aGlobals, invite, &addr );
inviteReceivedCurses( aGlobals, invite );
}
static void
@ -1240,11 +1237,10 @@ cursesGotBuf( void* closure, const CommsAddrRec* addr,
#endif
static void
smsInviteReceivedCurses( void* closure, const NetLaunchInfo* nli,
const CommsAddrRec* returnAddr )
smsInviteReceivedCurses( void* closure, const NetLaunchInfo* nli )
{
CursesAppGlobals* aGlobals = (CursesAppGlobals*)closure;
inviteReceivedCurses( aGlobals, nli, returnAddr );
inviteReceivedCurses( aGlobals, nli );
}
static void

View file

@ -75,8 +75,7 @@ void cursesDrawCtxtFree( DrawCtx* dctx );
void cursesmain( XP_Bool isServer, LaunchParams* params );
bool handleQuit( void* closure, int unused_key );
void inviteReceivedCurses( void* aGlobals, const NetLaunchInfo* invite,
const CommsAddrRec* returnAddr );
void inviteReceivedCurses( void* aGlobals, const NetLaunchInfo* invite );
void mqttMsgReceivedCurses( void* closure, const CommsAddrRec* from,
XP_U32 gameID, const XP_U8* buf, XP_U16 len );
void gameGoneCurses( void* closure, const CommsAddrRec* from, XP_U32 gameID );

View file

@ -359,7 +359,7 @@ make_rematch( GtkAppGlobals* apg, const CommonGlobals* cGlobals )
{
LaunchParams* params = apg->cag.params;
GtkGameGlobals* newGlobals = calloc( 1, sizeof(*newGlobals) );
initBoardGlobalsGtk( newGlobals, params, cGlobals->util->gameInfo );
initBoardGlobalsGtk( newGlobals, params, NULL );
XW_UtilCtxt* util = newGlobals->cGlobals.util;
const CommonPrefs* cp = &newGlobals->cGlobals.cp;
@ -705,8 +705,7 @@ feedBufferGTK( GtkAppGlobals* apg, sqlite3_int64 rowid,
/* Stuff common to receiving invitations */
static void
gameFromInvite( GtkAppGlobals* apg, const NetLaunchInfo* nli,
const CommsAddrRec* XP_UNUSED(returnAddr) )
gameFromInvite( GtkAppGlobals* apg, const NetLaunchInfo* nli )
{
LOG_FUNC();
@ -751,7 +750,7 @@ inviteReceivedGTK( void* closure, const NetLaunchInfo* invite )
GTK_BUTTONS_YES_NO, NULL );
}
if ( doIt ) {
gameFromInvite( apg, invite, NULL );
gameFromInvite( apg, invite );
}
}
@ -812,8 +811,7 @@ gtkNoticeRcvd( void* closure )
#endif
static void
smsInviteReceived( void* closure, const NetLaunchInfo* nli,
const CommsAddrRec* returnAddr )
smsInviteReceived( void* closure, const NetLaunchInfo* nli )
{
GtkAppGlobals* apg = (GtkAppGlobals*)closure;
XP_LOGF( "%s(gameName=%s, gameID=%d, dictName=%s, nPlayers=%d, "
@ -821,7 +819,7 @@ smsInviteReceived( void* closure, const NetLaunchInfo* nli,
nli->gameID, nli->dict, nli->nPlayersT,
nli->nPlayersH, nli->forceChannel );
gameFromInvite( apg, nli, returnAddr );
gameFromInvite( apg, nli );
}
void

View file

@ -91,9 +91,7 @@ linux_dutil_onInviteReceived( XW_DUtilCtxt* duc, XWEnv XP_UNUSED(xwe),
LaunchParams* params = (LaunchParams*)duc->closure;
if ( params->useCurses ) {
CommsAddrRec addr = {0};
nli_makeAddrRec( nli, &addr );
inviteReceivedCurses( params->appGlobals, nli, &addr );
inviteReceivedCurses( params->appGlobals, nli );
} else {
inviteReceivedGTK( params->appGlobals, nli );
}

View file

@ -141,6 +141,7 @@ ensureLocalPlayerNames( LaunchParams* XP_UNUSED_DBG(params), CurGameInfo* gi )
for ( int ii = 0; ii < gi->nPlayers; ++ii ) {
LocalPlayer* lp = &gi->players[ii];
if ( lp->isLocal && !lp->name ) {
XP_ASSERT(0);
replaceStringIfDifferent( params->mpool,
&lp->name, "LPlayer" );
}
@ -189,7 +190,7 @@ linuxOpenGame( CommonGlobals* cGlobals, const TransportProcs* procs )
cGlobals->gi,
cGlobals->util, cGlobals->draw,
&cGlobals->cp, procs );
XP_LOGF( "%s: loaded gi at %p", __func__, &cGlobals->gi );
XP_LOGFF( "loaded gi at %p", &cGlobals->gi );
stream_destroy( stream, NULL_XWE );
}
@ -2945,6 +2946,7 @@ main( int argc, char** argv )
++mainParams.nLocalPlayers;
mainParams.pgi.players[index].robotIQ = 0; /* means human */
mainParams.pgi.players[index].isLocal = XP_TRUE;
XP_ASSERT( !mainParams.pgi.players[index].name );
mainParams.pgi.players[index].name =
copyString( mainParams.mpool, (XP_UCHAR*)optarg);
break;
@ -2967,6 +2969,7 @@ main( int argc, char** argv )
++mainParams.nLocalPlayers;
mainParams.pgi.players[index].robotIQ = 1; /* real smart by default */
mainParams.pgi.players[index].isLocal = XP_TRUE;
XP_ASSERT( !mainParams.pgi.players[index].name );
mainParams.pgi.players[index].name =
copyString( mainParams.mpool, (XP_UCHAR*)optarg);
break;

View file

@ -263,7 +263,7 @@ parseAndDispatch( LaunchParams* params, uint8_t* buf, int len,
NetLaunchInfo nli = {0};
nliFromData( params, msg, &nli );
(*storage->procs->inviteReceived)( storage->procClosure,
&nli, addr );
&nli );
}
break;
default:

View file

@ -26,8 +26,7 @@
#include "nli.h"
typedef struct _SMSProcs {
void (*inviteReceived)( void* closure, const NetLaunchInfo* nli,
const CommsAddrRec* returnAddr );
void (*inviteReceived)( void* closure, const NetLaunchInfo* nli );
void (*msgReceived)( void* closure, const CommsAddrRec* from, XP_U32 gameID,
const XP_U8* buf, XP_U16 len );
void (*msgNoticeReceived)( void* closure );