Fixed memory leaks

Optimized message handling a bit
This commit is contained in:
David Anderson 2005-07-14 04:50:46 +00:00
parent 116984afed
commit db293cc451
4 changed files with 60 additions and 67 deletions

View File

@ -18,14 +18,14 @@ void ClearHooks()
msgHooks[i].clear(); msgHooks[i].clear();
msgBlocks[i] = 0; msgBlocks[i] = 0;
} }
for (i=0; i<Msg.size(); i++)
delete Msg[i];
for (i=0; i<Touches.size(); i++) for (i=0; i<Touches.size(); i++)
delete Touches[i]; delete Touches[i];
for (i=0; i<Impulses.size(); i++) for (i=0; i<Impulses.size(); i++)
delete Impulses[i]; delete Impulses[i];
for (i=0; i<Thinks.size(); i++) for (i=0; i<Thinks.size(); i++)
delete Thinks[i]; delete Thinks[i];
Msg.clear(); Msg.clear();
Touches.clear(); Touches.clear();
Impulses.clear(); Impulses.clear();

View File

@ -151,12 +151,10 @@ static cell AMX_NATIVE_CALL DispatchKeyValue(AMX *amx, cell *params)
int iLength; int iLength;
char *char1 = MF_GetAmxString(amx, params[1], 0, &iLength); char *char1 = MF_GetAmxString(amx, params[1], 0, &iLength);
char *char2 = MF_GetAmxString(amx, params[2], 1, &iLength); char *char2 = MF_GetAmxString(amx, params[2], 1, &iLength);
char *charA = new char[strlen(char1)+1]; const char *charA = STRING(ALLOC_STRING(char1));
char *charB = new char[strlen(char2)+1]; const char *charB = STRING(ALLOC_STRING(char2));
strcpy(charA, char1); g_pkvd->szKeyName = const_cast<char *>(charA);
strcpy(charB, char2); g_pkvd->szValue = const_cast<char *>(charB);
g_pkvd->szKeyName = charA;
g_pkvd->szValue = charB;
} }
return 1; return 1;
} }
@ -1259,10 +1257,7 @@ static cell AMX_NATIVE_CALL entity_set_model(AMX *amx, cell *params)
edict_t *pEnt = INDEXENT2(iEnt); edict_t *pEnt = INDEXENT2(iEnt);
int iLen; int iLen;
char *szModel = MF_GetAmxString(amx, params[2], 0, &iLen); char *szModel = MF_GetAmxString(amx, params[2], 0, &iLen);
char *szStatic = new char[iLen+1]; const char *szStatic = STRING(ALLOC_STRING(szModel));
memset(szStatic, 0, iLen+1);
strcpy(szStatic, szModel);
SET_MODEL(pEnt, szStatic); SET_MODEL(pEnt, szStatic);
@ -1495,7 +1490,7 @@ static cell AMX_NATIVE_CALL find_ent_by_owner(AMX *amx, cell *params) // native
static cell AMX_NATIVE_CALL get_grenade_id(AMX *amx, cell *params) /* 4 param */ static cell AMX_NATIVE_CALL get_grenade_id(AMX *amx, cell *params) /* 4 param */
{ {
int index = params[1]; int index = params[1];
char* szModel; const char *szModel;
CHECK_ENTITY(index); CHECK_ENTITY(index);
@ -1506,10 +1501,8 @@ static cell AMX_NATIVE_CALL get_grenade_id(AMX *amx, cell *params) /* 4 param *
while (!FNullEnt(pentFind)) { while (!FNullEnt(pentFind)) {
if (pentFind->v.owner == pentOwner) { if (pentFind->v.owner == pentOwner) {
if (params[3]>0) { if (params[3]>0) {
szModel = new char[params[3]];
szModel = (char*)STRING(pentFind->v.model); szModel = (char*)STRING(pentFind->v.model);
MF_SetAmxString(amx, params[2], szModel, params[3]); MF_SetAmxString(amx, params[2], szModel, params[3]);
delete [] szModel;
return ENTINDEX(pentFind); return ENTINDEX(pentFind);
} }
} }

View File

@ -1,6 +1,6 @@
#include "engine.h" #include "engine.h"
CVector<argMsg*> Msg; CVector<argMsg> Msg;
CVector<int> msgHooks[256]; CVector<int> msgHooks[256];
int msgBlocks[256] = {0}; int msgBlocks[256] = {0};
int msgDest; int msgDest;
@ -114,13 +114,13 @@ void WriteByte(int iValue)
RETURN_META(MRES_SUPERCEDE); RETURN_META(MRES_SUPERCEDE);
} else if (inhook) { } else if (inhook) {
if (++msgCount > Msg.size()) { if (++msgCount > Msg.size()) {
argMsg *p = new argMsg(); argMsg p;
p->iData = iValue; p.iData = iValue;
p->type = arg_byte; p.type = arg_byte;
Msg.push_back(p); Msg.push_back(p);
} else { } else {
Msg[msgCount-1]->iData = iValue; Msg[msgCount-1].iData = iValue;
Msg[msgCount-1]->type = arg_byte; Msg[msgCount-1].type = arg_byte;
} }
RETURN_META(MRES_SUPERCEDE); RETURN_META(MRES_SUPERCEDE);
} }
@ -134,13 +134,13 @@ void WriteChar(int iValue)
RETURN_META(MRES_SUPERCEDE); RETURN_META(MRES_SUPERCEDE);
} else if (inhook) { } else if (inhook) {
if (++msgCount > Msg.size()) { if (++msgCount > Msg.size()) {
argMsg *p = new argMsg(); argMsg p;
p->iData = iValue; p.iData = iValue;
p->type = arg_char; p.type = arg_char;
Msg.push_back(p); Msg.push_back(p);
} else { } else {
Msg[msgCount-1]->iData = iValue; Msg[msgCount-1].iData = iValue;
Msg[msgCount-1]->type = arg_char; Msg[msgCount-1].type = arg_char;
} }
RETURN_META(MRES_SUPERCEDE); RETURN_META(MRES_SUPERCEDE);
} }
@ -154,13 +154,13 @@ void WriteShort(int iValue)
RETURN_META(MRES_SUPERCEDE); RETURN_META(MRES_SUPERCEDE);
} else if (inhook) { } else if (inhook) {
if (++msgCount > Msg.size()) { if (++msgCount > Msg.size()) {
argMsg *p = new argMsg(); argMsg p;
p->iData = iValue; p.iData = iValue;
p->type = arg_short; p.type = arg_short;
Msg.push_back(p); Msg.push_back(p);
} else { } else {
Msg[msgCount-1]->iData = iValue; Msg[msgCount-1].iData = iValue;
Msg[msgCount-1]->type = arg_short; Msg[msgCount-1].type = arg_short;
} }
RETURN_META(MRES_SUPERCEDE); RETURN_META(MRES_SUPERCEDE);
} }
@ -174,13 +174,13 @@ void WriteLong(int iValue)
RETURN_META(MRES_SUPERCEDE); RETURN_META(MRES_SUPERCEDE);
} else if (inhook) { } else if (inhook) {
if (++msgCount > Msg.size()) { if (++msgCount > Msg.size()) {
argMsg *p = new argMsg(); argMsg p;
p->iData = iValue; p.iData = iValue;
p->type = arg_long; p.type = arg_long;
Msg.push_back(p); Msg.push_back(p);
} else { } else {
Msg[msgCount-1]->iData = iValue; Msg[msgCount-1].iData = iValue;
Msg[msgCount-1]->type = arg_long; Msg[msgCount-1].type = arg_long;
} }
RETURN_META(MRES_SUPERCEDE); RETURN_META(MRES_SUPERCEDE);
} }
@ -194,13 +194,13 @@ void WriteAngle(float flValue)
RETURN_META(MRES_SUPERCEDE); RETURN_META(MRES_SUPERCEDE);
} else if (inhook) { } else if (inhook) {
if (++msgCount > Msg.size()) { if (++msgCount > Msg.size()) {
argMsg *p = new argMsg(); argMsg p;
p->fData = flValue; p.fData = flValue;
p->type = arg_angle; p.type = arg_angle;
Msg.push_back(p); Msg.push_back(p);
} else { } else {
Msg[msgCount-1]->fData = flValue; Msg[msgCount-1].fData = flValue;
Msg[msgCount-1]->type = arg_angle; Msg[msgCount-1].type = arg_angle;
} }
RETURN_META(MRES_SUPERCEDE); RETURN_META(MRES_SUPERCEDE);
} }
@ -214,13 +214,13 @@ void WriteCoord(float flValue)
RETURN_META(MRES_SUPERCEDE); RETURN_META(MRES_SUPERCEDE);
} else if (inhook) { } else if (inhook) {
if (++msgCount > Msg.size()) { if (++msgCount > Msg.size()) {
argMsg *p = new argMsg(); argMsg p;
p->fData = flValue; p.fData = flValue;
p->type = arg_coord; p.type = arg_coord;
Msg.push_back(p); Msg.push_back(p);
} else { } else {
Msg[msgCount-1]->fData = flValue; Msg[msgCount-1].fData = flValue;
Msg[msgCount-1]->type = arg_coord; Msg[msgCount-1].type = arg_coord;
} }
RETURN_META(MRES_SUPERCEDE); RETURN_META(MRES_SUPERCEDE);
} }
@ -234,13 +234,13 @@ void WriteString(const char *sz)
RETURN_META(MRES_SUPERCEDE); RETURN_META(MRES_SUPERCEDE);
} else if (inhook) { } else if (inhook) {
if (++msgCount > Msg.size()) { if (++msgCount > Msg.size()) {
argMsg *p = new argMsg(); argMsg p;
p->cData.assign(sz); p.cData.assign(sz);
p->type = arg_string; p.type = arg_string;
Msg.push_back(p); Msg.push_back(p);
} else { } else {
Msg[msgCount-1]->cData.assign(sz); Msg[msgCount-1].cData.assign(sz);
Msg[msgCount-1]->type = arg_string; Msg[msgCount-1].type = arg_string;
} }
RETURN_META(MRES_SUPERCEDE); RETURN_META(MRES_SUPERCEDE);
} }
@ -254,13 +254,13 @@ void WriteEntity(int iValue)
RETURN_META(MRES_SUPERCEDE); RETURN_META(MRES_SUPERCEDE);
} else if (inhook) { } else if (inhook) {
if (++msgCount > Msg.size()) { if (++msgCount > Msg.size()) {
argMsg *p = new argMsg(); argMsg p;
p->iData = iValue; p.iData = iValue;
p->type = arg_entity; p.type = arg_entity;
Msg.push_back(p); Msg.push_back(p);
} else { } else {
Msg[msgCount-1]->iData = iValue; Msg[msgCount-1].iData = iValue;
Msg[msgCount-1]->type = arg_entity; Msg[msgCount-1].type = arg_entity;
} }
RETURN_META(MRES_SUPERCEDE); RETURN_META(MRES_SUPERCEDE);
} }
@ -292,8 +292,8 @@ void MessageEnd(void)
} }
MESSAGE_BEGIN(msgDest, msgType, msgOrigin, msgpEntity); MESSAGE_BEGIN(msgDest, msgType, msgOrigin, msgpEntity);
for (i=0; i<msgCount; i++) { for (i=0; i<msgCount; i++) {
Msg[i]->Send(); Msg[i].Send();
Msg[i]->Reset(); Msg[i].Reset();
} }
MESSAGE_END(); MESSAGE_END();
msgCount = 0; msgCount = 0;
@ -361,7 +361,7 @@ static cell AMX_NATIVE_CALL get_msg_argtype(AMX *amx, cell *params)
return 0; return 0;
} }
return Msg[argn]->type; return Msg[argn].type;
} }
static cell AMX_NATIVE_CALL get_msg_arg_int(AMX *amx, cell *params) static cell AMX_NATIVE_CALL get_msg_arg_int(AMX *amx, cell *params)
@ -373,7 +373,7 @@ static cell AMX_NATIVE_CALL get_msg_arg_int(AMX *amx, cell *params)
return 0; return 0;
} }
int iVal = Msg[argn]->iData; int iVal = Msg[argn].iData;
return iVal; return iVal;
} }
@ -387,8 +387,8 @@ static cell AMX_NATIVE_CALL set_msg_arg_int(AMX *amx, cell *params)
return 0; return 0;
} }
Msg[argn]->type = params[2]; Msg[argn].type = params[2];
Msg[argn]->iData = params[3]; Msg[argn].iData = params[3];
return 1; return 1;
} }
@ -402,7 +402,7 @@ static cell AMX_NATIVE_CALL get_msg_arg_float(AMX *amx, cell *params)
return 0; return 0;
} }
return amx_ftoc(Msg[argn]->fData); return amx_ftoc(Msg[argn].fData);
} }
static cell AMX_NATIVE_CALL set_msg_arg_float(AMX *amx, cell *params) static cell AMX_NATIVE_CALL set_msg_arg_float(AMX *amx, cell *params)
@ -416,7 +416,7 @@ static cell AMX_NATIVE_CALL set_msg_arg_float(AMX *amx, cell *params)
REAL fVal = amx_ctof(params[2]); REAL fVal = amx_ctof(params[2]);
Msg[argn]->fData = fVal; Msg[argn].fData = fVal;
return 1; return 1;
} }
@ -430,7 +430,7 @@ static cell AMX_NATIVE_CALL get_msg_arg_string(AMX *amx, cell *params)
return 0; return 0;
} }
const char *szVal = Msg[argn]->cData.c_str(); const char *szVal = Msg[argn].cData.c_str();
return MF_SetAmxString(amx, params[2], szVal?szVal:"", params[3]); return MF_SetAmxString(amx, params[2], szVal?szVal:"", params[3]);
} }
@ -447,7 +447,7 @@ static cell AMX_NATIVE_CALL set_msg_arg_string(AMX *amx, cell *params)
char *szVal = MF_GetAmxString(amx, params[2], 0, &iLen); char *szVal = MF_GetAmxString(amx, params[2], 0, &iLen);
Msg[argn]->cData.assign(szVal); Msg[argn].cData.assign(szVal);
return 1; return 1;
} }

View File

@ -41,7 +41,7 @@ public:
}; };
extern AMX_NATIVE_INFO msg_Natives[]; extern AMX_NATIVE_INFO msg_Natives[];
extern CVector<argMsg*> Msg; extern CVector<argMsg> Msg;
extern CVector<int> msgHooks[256]; extern CVector<int> msgHooks[256];
extern int msgBlocks[256]; extern int msgBlocks[256];