Skip to content

Commit

Permalink
fixing memory problem that my getBaseStat fix created
Browse files Browse the repository at this point in the history
removed roots from screenhooks - screen hooks now get properly deleted
added more checks to ensure crit sections leave
added check to TriggerOperationCallback for active cx.
  • Loading branch information
bobode committed Jun 9, 2013
1 parent bc266c3 commit cde52d8
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 117 deletions.
27 changes: 3 additions & 24 deletions CriticalSections.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ class CriticalRoom

public:
CriticalRoom() : bEnteredCriticalSection(false) {}
~CriticalRoom() { LeaveSection(); }
~CriticalRoom() {
LeaveSection();
}

void EnterSection() {
InterlockedIncrement(&Vars.SectionCount);
Expand All @@ -26,26 +28,3 @@ class CriticalRoom
}
};

class CriticalMisc
{
private:
bool bEnteredCriticalSection;

public:
CriticalMisc() : bEnteredCriticalSection(false) {}
~CriticalMisc() { LeaveSection(); }

void EnterSection() {
InterlockedIncrement(&Vars.SectionCount);
EnterCriticalSection(&Vars.cGameLoopSection);
bEnteredCriticalSection = true;
}

void LeaveSection() {
if(bEnteredCriticalSection) {
bEnteredCriticalSection = false;
LeaveCriticalSection(&Vars.cGameLoopSection);
InterlockedDecrement(&Vars.SectionCount);
}
}
};
2 changes: 1 addition & 1 deletion D2BS.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#define XP_WIN

#define D2BS_VERSION "1.5.1776" //uptodate with d branch 1765 ff 20b
#define D2BS_VERSION "1.5.1777" //uptodate with d branch 1765 ff 20b

#include <windows.h>
#include <vector>
Expand Down
1 change: 1 addition & 0 deletions JSArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ JSAPI_PROP(area_getProperty)
JSObject* pExit = BuildObject(cx, &exit_class, NULL, exit_props, exit);
if(!pExit)
{
cRoom.LeaveSection();
delete exit;
JS_EndRequest(cx);
THROW_ERROR(cx, "Failed to create exit object!");
Expand Down
61 changes: 37 additions & 24 deletions JSGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ JSAPI_FUNC(my_acceptTrade)
return JS_TRUE;
}

CriticalMisc myMisc;
CriticalRoom myMisc;
myMisc.EnterSection();

if((*p_D2CLIENT_RecentTradeId) == 3 || (*p_D2CLIENT_RecentTradeId) == 5 || (*p_D2CLIENT_RecentTradeId) == 7)
Expand All @@ -166,7 +166,7 @@ JSAPI_FUNC(my_acceptTrade)
myMisc.LeaveSection();
return JS_TRUE;
}

myMisc.LeaveSection();
THROW_ERROR(cx, "Invalid parameter passed to acceptTrade!");
}

Expand All @@ -175,7 +175,7 @@ JSAPI_FUNC(my_tradeOk)
if(!WaitForGameReady())
THROW_WARNING(cx, vp, "Game not ready");

CriticalMisc myMisc;
CriticalRoom myMisc;
TransactionDialogsInfo_t* pTdi = *p_D2CLIENT_pTransactionDialogsInfo;
unsigned int i;

Expand All @@ -196,14 +196,14 @@ JSAPI_FUNC(my_tradeOk)
}
}
}

myMisc.LeaveSection();
THROW_ERROR(cx, "Not in proper state to click ok to trade.");
}

JSAPI_FUNC(my_getDialogLines)
{
JS_SET_RVAL(cx, vp, JSVAL_VOID);
CriticalMisc myMisc;
CriticalRoom myMisc;
TransactionDialogsInfo_t* pTdi = *p_D2CLIENT_pTransactionDialogsInfo;
unsigned int i;
JSObject* pReturnArray;
Expand Down Expand Up @@ -285,16 +285,22 @@ JSAPI_FUNC(my_getPath)
JS_BeginRequest(cx);
if(!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "uuuuu/uu", &lvl, &x, &y, &dx, &dy, &reductionType, &radius))
{
myMisc.LeaveSection();
JS_EndRequest(cx);
return JS_FALSE;
}
JS_EndRequest(cx);
if(reductionType == 3 &&
!(JSVAL_IS_FUNCTION(cx, JS_ARGV(cx, vp)[7]) && JSVAL_IS_FUNCTION(cx, JS_ARGV(cx, vp)[8]) && JSVAL_IS_FUNCTION(cx, JS_ARGV(cx, vp)[9])))
{
myMisc.LeaveSection();
THROW_ERROR(cx, "Invalid function values for reduction type");

}
if (lvl == 0)
{
myMisc.LeaveSection();
THROW_ERROR(cx, "Invalid level passed to getPath");
}
Level* level = GetLevel(lvl);

ActMap* map = ActMap::GetMap(level);
Expand All @@ -308,7 +314,7 @@ JSAPI_FUNC(my_getPath)
case 1: reducer = new TeleportPathReducer(map, DiagonalShortcut, radius); break;
case 2: reducer = new NoPathReducer(map); break;
case 3: reducer = new JSPathReducer(map, cx, JS_THIS_OBJECT(cx, vp), JS_ARGV(cx, vp)[7], JS_ARGV(cx, vp)[8], JS_ARGV(cx, vp)[9]); break;
default: THROW_ERROR(cx, "Invalid path reducer value!"); break;
default: myMisc.LeaveSection(); THROW_ERROR(cx, "Invalid path reducer value!"); break;
}

PointList list;
Expand Down Expand Up @@ -383,12 +389,12 @@ JSAPI_FUNC(my_getCollision)
Point point(nX, nY);
Level* level = GetLevel(nLevelId);
if(!level)
THROW_ERROR(cx, "Level Not loaded");
{ myMisc.LeaveSection(); THROW_ERROR(cx, "Level Not loaded");}

ActMap* map = ActMap::GetMap(level);
if(!map->IsValidPoint(point))
THROW_ERROR(cx, "Invalid point!");

{ myMisc.LeaveSection(); THROW_ERROR(cx, "Invalid point!");}
jsval rval;
JS_BeginRequest(cx);
rval = JS_NumberValue(map->GetMapData(point, true));
Expand All @@ -405,12 +411,13 @@ JSAPI_FUNC(my_clickItem)

if(!WaitForGameReady())
THROW_WARNING(cx, vp, "Game not ready");

CriticalMisc myMisc;
JS_SET_RVAL(cx, vp , JSVAL_NULL);
CriticalRoom myMisc;
myMisc.EnterSection();

if(*p_D2CLIENT_TransactionDialog != 0 || *p_D2CLIENT_TransactionDialogs != 0 || *p_D2CLIENT_TransactionDialogs_2 != 0)
{
myMisc.LeaveSection();
JS_SET_RVAL(cx, vp, JSVAL_FALSE);
return JS_TRUE;
}
Expand Down Expand Up @@ -451,20 +458,20 @@ JSAPI_FUNC(my_clickItem)
pmyUnit = (myUnit*)JS_GetPrivate(cx, JSVAL_TO_OBJECT(JS_ARGV(cx, vp)[0]));

if(!pmyUnit || (pmyUnit->_dwPrivateType & PRIVATE_UNIT) != PRIVATE_UNIT)
return JS_TRUE;
{myMisc.LeaveSection(); return JS_TRUE;}

pUnit = D2CLIENT_FindUnit(pmyUnit->dwUnitId, pmyUnit->dwType);

if(!pUnit)
return JS_TRUE;
{myMisc.LeaveSection(); return JS_TRUE;}

clickequip * click = (clickequip*)*(DWORD*)(D2CLIENT_BodyClickTable + (4 * pUnit->pItemData->BodyLocation));

if(!click)
return JS_TRUE;
{myMisc.LeaveSection(); return JS_TRUE;}

click(D2CLIENT_GetPlayerUnit(), D2CLIENT_GetPlayerUnit()->pInventory, pUnit->pItemData->BodyLocation);

myMisc.LeaveSection();
return JS_TRUE;
}
else if(argc == 2 && JSVAL_IS_INT(JS_ARGV(cx, vp)[0]) && JSVAL_IS_INT(JS_ARGV(cx, vp)[1]))
Expand All @@ -478,9 +485,10 @@ JSAPI_FUNC(my_clickItem)
clickequip * click = (clickequip*)*(DWORD*)(D2CLIENT_BodyClickTable + (4 * nBodyLoc));

if(!click)
return JS_TRUE;
{myMisc.LeaveSection(); return JS_TRUE;}

click(D2CLIENT_GetPlayerUnit(), D2CLIENT_GetPlayerUnit()->pInventory, nBodyLoc);

}
// Click Merc Gear
else if(nClickType == 4)
Expand All @@ -492,6 +500,7 @@ JSAPI_FUNC(my_clickItem)
if(pMerc)
{
D2CLIENT_MercItemAction(0x61, nBodyLoc);
JS_SET_RVAL(cx, vp , JSVAL_TRUE);
}
}
}
Expand All @@ -503,14 +512,14 @@ JSAPI_FUNC(my_clickItem)
pmyUnit = (myUnit*)JS_GetPrivate(cx, JSVAL_TO_OBJECT(JS_ARGV(cx, vp)[1]));

if(!pmyUnit || (pmyUnit->_dwPrivateType & PRIVATE_UNIT) != PRIVATE_UNIT)
return JS_TRUE;
{myMisc.LeaveSection(); return JS_TRUE;}

pUnit = D2CLIENT_FindUnit(pmyUnit->dwUnitId, pmyUnit->dwType);

jsint nClickType = JSVAL_TO_INT(JS_ARGV(cx, vp)[0]);

if(!pUnit || !(pUnit->dwType == UNIT_ITEM) || !pUnit->pItemData)
THROW_ERROR(cx, "Object is not an item!");
{myMisc.LeaveSection(); THROW_ERROR(cx, "Object is not an item!");}

int InventoryLocation = GetItemLocation(pUnit);
int ClickLocation = LOCATION_NULL;
Expand All @@ -531,9 +540,10 @@ JSAPI_FUNC(my_clickItem)
if(pUnit->pItemData && pUnit->pItemData->pOwner)
if(pUnit->pItemData->pOwner->dwUnitId == pMerc->dwUnitId)
{
JS_SET_RVAL(cx, vp , JSVAL_TRUE);
D2CLIENT_MercItemAction(0x61, pUnit->pItemData->BodyLocation);
}

myMisc.LeaveSection();
return JS_TRUE;
}
else if(InventoryLocation == LOCATION_INVENTORY || InventoryLocation == LOCATION_STASH || InventoryLocation == LOCATION_CUBE)
Expand Down Expand Up @@ -570,7 +580,7 @@ JSAPI_FUNC(my_clickItem)
int i = x;

if( i < 0 || i > 0x0F)
return JS_TRUE;
{myMisc.LeaveSection(); return JS_TRUE;}

if(D2GFX_GetScreenSize() == 2)
{
Expand All @@ -589,12 +599,12 @@ JSAPI_FUNC(my_clickItem)
else if(D2CLIENT_GetCursorItem() == pUnit)
{
if(nClickType < 1 || nClickType > 12)
return JS_TRUE;
{myMisc.LeaveSection(); return JS_TRUE;}

clickequip * click = (clickequip*)*(DWORD*)(D2CLIENT_BodyClickTable + (4 * nClickType));

if(!click)
return JS_TRUE;
{myMisc.LeaveSection(); return JS_TRUE;}

click(D2CLIENT_GetPlayerUnit(), D2CLIENT_GetPlayerUnit()->pInventory, nClickType);
}
Expand Down Expand Up @@ -666,6 +676,7 @@ JSAPI_FUNC(my_clickItem)
D2CLIENT_LeftClickItem(D2CLIENT_GetPlayerUnit(), D2CLIENT_GetPlayerUnit()->pInventory, x, y, 5, pLayout, clickTarget);

myMisc.LeaveSection();
JS_SET_RVAL(cx, vp , JSVAL_TRUE);
return JS_TRUE;
}
else if(nLoc == LOCATION_BELT) // Belt
Expand All @@ -683,7 +694,7 @@ JSAPI_FUNC(my_clickItem)
}

if(z == -1)
return JS_TRUE;
{myMisc.LeaveSection(); return JS_TRUE;}

int x = NULL;
int y = NULL;
Expand All @@ -706,10 +717,12 @@ JSAPI_FUNC(my_clickItem)
D2CLIENT_ClickBeltRight(D2CLIENT_GetPlayerUnit(), D2CLIENT_GetPlayerUnit()->pInventory, TRUE, z);

myMisc.LeaveSection();
JS_SET_RVAL(cx, vp , JSVAL_TRUE);
return JS_TRUE;
}
}
}
JS_SET_RVAL(cx, vp , JSVAL_TRUE);
myMisc.LeaveSection();
return JS_TRUE;
}
Expand Down
2 changes: 2 additions & 0 deletions JSPresetUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ JSAPI_FUNC(my_getPresetUnits)
{
delete mypUnit;
JS_EndRequest(cx);
cRoom.LeaveSection();
THROW_ERROR(cx, "Failed to build object?");
}

Expand Down Expand Up @@ -207,6 +208,7 @@ JSAPI_FUNC(my_getPresetUnit)
JSObject* obj = BuildObject(cx, &presetunit_class, NULL, presetunit_props, mypUnit);
if(!obj)
{
cRoom.LeaveSection();
delete mypUnit;
THROW_ERROR(cx, "Failed to create presetunit object");
}
Expand Down
Loading

0 comments on commit cde52d8

Please sign in to comment.