Fix memory leaks in dedicated mode

This commit is contained in:
Azamat H. Hackimov 2024-09-04 14:17:02 +03:00
parent a4ef5fd0d5
commit d185ab9514
3 changed files with 16 additions and 6 deletions

View File

@ -1917,6 +1917,7 @@ int MissionGetKeywords(const char *mission, char *keywords) {
} }
if (!*parse_keys) { if (!*parse_keys) {
mem_free(parse_keys);
return MAX_NET_PLAYERS; return MAX_NET_PLAYERS;
} }
// Break up the mod keywords into an array // Break up the mod keywords into an array

View File

@ -210,6 +210,8 @@ int gspy_Init() {
htons(server.sin_port)); htons(server.sin_port));
} }
} }
cfclose(cfp);
} }
#endif // #ifndef OEM #endif // #ifndef OEM
return 1; return 1;

View File

@ -1142,7 +1142,7 @@ int mng_ReadNewGenericPage(CFILE *infile, mngs_generic_page *genericpage) {
char tempbuf[1024]; char tempbuf[1024];
cf_ReadString(tempbuf, 1024, infile); cf_ReadString(tempbuf, 1024, infile);
int slen = strlen(tempbuf) + 1; size_t slen = strlen(tempbuf) + 1;
genericpage->objinfo_struct.description = (char *)mem_malloc(slen); genericpage->objinfo_struct.description = (char *)mem_malloc(slen);
ASSERT(genericpage->objinfo_struct.description); ASSERT(genericpage->objinfo_struct.description);
@ -1927,10 +1927,7 @@ int mng_AssignGenericPageToObjInfo(mngs_generic_page *genericpage, int n, CFILE
if (objinfopointer->ai_info) if (objinfopointer->ai_info)
memcpy(objinfopointer->ai_info, &genericpage->ai_info, sizeof(genericpage->ai_info)); memcpy(objinfopointer->ai_info, &genericpage->ai_info, sizeof(genericpage->ai_info));
objinfopointer->multi_allowed = 1; objinfopointer->multi_allowed = true;
// since the description pointer was just copied over, no need to malloc mem, copy and then free old, just move ptr
genericpage->objinfo_struct.description = NULL;
strcpy(objinfopointer->icon_name, genericpage->objinfo_struct.icon_name); strcpy(objinfopointer->icon_name, genericpage->objinfo_struct.icon_name);
// First see if our image differs from the one on the net // First see if our image differs from the one on the net
@ -2195,7 +2192,7 @@ void mng_AssignObjInfoToGenericPage(int n, mngs_generic_page *genericpage) {
// then calls SetAndLoadgeneric to actually load in any images/models associated // then calls SetAndLoadgeneric to actually load in any images/models associated
// with it // with it
void mng_LoadNetGenericPage(CFILE *infile, bool overlay) { void mng_LoadNetGenericPage(CFILE *infile, bool overlay) {
mngs_generic_page genericpage; mngs_generic_page genericpage{};
memset(&genericpage, 0, sizeof(mngs_generic_page)); memset(&genericpage, 0, sizeof(mngs_generic_page));
if (mng_ReadNewGenericPage(infile, &genericpage)) { if (mng_ReadNewGenericPage(infile, &genericpage)) {
@ -2206,11 +2203,21 @@ void mng_LoadNetGenericPage(CFILE *infile, bool overlay) {
mng_FreePagetypePrimitives(PAGETYPE_GENERIC, genericpage.objinfo_struct.name, 0); mng_FreePagetypePrimitives(PAGETYPE_GENERIC, genericpage.objinfo_struct.name, 0);
mng_AssignGenericPageToObjInfo(&genericpage, n); mng_AssignGenericPageToObjInfo(&genericpage, n);
} }
// Free allocated memory
if (genericpage.objinfo_struct.description) {
mem_free(genericpage.objinfo_struct.description);
genericpage.objinfo_struct.description = nullptr;
}
return; // A weapon has already loaded this generic return; // A weapon has already loaded this generic
} }
int ret = mng_SetAndLoadGeneric(&genericpage, infile); int ret = mng_SetAndLoadGeneric(&genericpage, infile);
ASSERT(ret >= 0); ASSERT(ret >= 0);
// Free allocated memory
if (genericpage.objinfo_struct.description) {
mem_free(genericpage.objinfo_struct.description);
genericpage.objinfo_struct.description = nullptr;
}
} else } else
mprintf(0, "Could not load genericpage named %s!\n", genericpage.objinfo_struct.name); mprintf(0, "Could not load genericpage named %s!\n", genericpage.objinfo_struct.name);
} }