# Trouble with Lua Stack overflows

This topic is 547 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am using C#, NLua and SFML.net.

I have a lua script which is causing Stack Overflow exceptions... but I am not recursing in anyway...

Here is the lua script:

import('RLWorld','RLWorld.Systems')
import('RLWorld','RLWorld.Components')
import('RLWorld','RLWorld.Entities')
import('RLWorld','RLWorld.Entities.RequestData')

function OnCollider(sysman, ent1, ent2)

if (sysman:LUAHasComponent('EntranceComponent',ent2)) then
sysman:LogToConsole('Bumped into an Entrance');
eComp=sysman:LUAGetComponent('EntranceComponent',ent2);

if  eComp.Coords=='' then
sysman:LogToConsole("Empty cords. Go up a level.");
sysman:SetCords(sysman:GetSuperCords());
else
sysman:LogToConsole('Creating dungeon tilemap');
-- sysman:LogToConsole(eComp.Coords .. "abc" .. "QRD");
pLoc=sysman:LUAGetComponent('LocationComponent',ent1);

nxtCord=eComp.Coords .. ":[";
nxtCord=nxtCord .. pLoc.TileLocation.X;
nxtCord=nxtCord .. ",";
nxtCord=nxtCord .. pLoc.TileLocation.Y;
sysman:LogToConsole(nxtCord);
--sysman:LogToConsole(pLoc.TileLocation.Y);
--sysman:LogToConsole(eComp.Coords .. ":[" .. (pLoc.TileLocation.X) .. "," .. (pLoc.TileLocation.Y));

--sysman:LogToConsole --..","..pLoc.TileLocation.Y.."]");

--nxtCord=eComp.Coords..":[";
--sysman:LogToConsole(nxtCord);
--nxtCord=nxtCord..pLoc.TileLocation.X
--sysman:LogToConsole(nxtCord);
--nxtCord=nxtCord..","
--sysman:LogToConsole(nxtCord);
--nxtCord=nxtCord..pLoc.TileLocation.Y
--sysman:LogToConsole(nxtCord);
--nxtCord=nxtCord.."]:[5,5]";
--sysman:LogToConsole(nxtCord);

--sysman:SetCords();
end
depth=sysman:CordDepth();
if (depth==1) then
sysman:LogToConsole('Creating GlobalTilemap');
iState=GlobalTilemapGameState();
end
if (depth==2) then
iState=LocalTilemapGameState();
sysman:LogToConsole('Creating LocalTilemap');
end
if (depth>2) then
iState=MinimalTilemapGameState();
sysman:LogToConsole('Creating MinimalTilemap');
end

sysman:LogToConsole('Setting run result');
rr=GamestateRunResult(eComp.Result,iState);
sysman:NameObject('RunResult',rr);

return;
end

actionData=ActionInputRequest('TRIGGER_AB1',nill);
request = Request.CreateRequest('ActionInput',ent1, actionData);
--r=sysman:LUAGetComponent('RenderComponent',ent2);
--r.Visible=not r.Visible;
return;
end


The  exception occurs when this bit of script is ran (I will number these lines)

1  sysman:LogToConsole('Creating dungeon tilemap');
-- sysman:LogToConsole(eComp.Coords .. "abc" .. "QRD");
3 pLoc=sysman:LUAGetComponent('LocationComponent',ent1);
4
5 nxtCord=eComp.Coords .. ":[";
6 nxtCord=nxtCord .. pLoc.TileLocation.X;
7 nxtCord=nxtCord .. ",";
8 nxtCord=nxtCord .. pLoc.TileLocation.Y;
9 sysman:LogToConsole(nxtCord);
10 --sysman:LogToConsole(pLoc.TileLocation.Y);
11   --sysman:LogToConsole(eComp.Coords .. ":[" .. (pLoc.TileLocation.X) .. "," .. (pLoc.TileLocation.Y));

Ok, I I comment out line 8 I do not get an overflow exception.
If I Comment out line 8 and replace line 6 with line 8 I do not get an overflow exception...

LUGGetComponent returns a struct with a set of members, one of which is TileLocation. TileLocation is a sfml Vector2i with two public members X and Y

##### Share on other sites

Blowing your stack can be messy. :-)

If it isn't recursion, the next thing to check is large amounts of data on the stack.

Look for large arrays or buffers or similar, basically looking at all the local variables in the stack trace.  If you've got arrays of several thousand items or some multidimensional arrays, both can consume your stack.

Sometimes in optimized builds, inlined functions with large stack use can also trigger blowing the stack.

Global variables can also sometimes use stack space, but generally don't; even so, look for large static items and clean them up while you are at it.

Edited by frob

##### Share on other sites

Also, check that the C-functions you use pop everything they push temporarily and the amount of return values you return matches the amount you pushed.

Edited by DaBono

##### Share on other sites

Thanks for the help. It turns out there was a bug in SFML... it had gotten fixed about two years ago but i Guess the fix never made it to the version hosted on sfml's site...

Got the source from git hub, rebuilt and everything is good to go. Thanks again.