So as many of you might know, a massive blizzard hit much of the Northeast a few days ago. Being trapped inside I decided I would write a script interpreter for a little scripting language that I'm going to use in one of my games. All was going well, when towards the end of its development, it began crashing for seemingly no reason.
Running GDB under Windows proved little use, revealing a backtrace of random system kernel calls. As a sanity check I copied the project into my Backtrack partition and tried again. Under Backtrack 5 the program crashes in the destructor (which is empty) for a very basic Value class that I have, which consists only of a string, int and enum. I ran it under valgrind to try and find heap corruption, but to no avail. I am using only STL containers and absolutely no dynamic memory (in my own code). The crash happens (consistently) when I call a function
Value runFunction(string name, vector<Value> args)
from within the
Value runTokens(vector<Token> tkns)
function. Just for shits and giggles I decided to write my own string class to use in my Value class. My string class only allocates memory, and never frees it. The program runs fine when I use it (disregarding the blatant memory leak). This leads me to believe that either the stack is being corrupted (which I highly doubt because all of my memory is managed by STL containers) or that somehow the string destructors are getting called multiple times. I cannot fathom as to why though.
I attempted to reproduce the problem on a smaller scale, but was unable to (further justifying my theory of heap corruption or multiple destructor calls). I was also unable to find any solutions through Google, only people telling the askers to do what I tried above. If anyone can provide any insight as to why this might be happening, it would be greatly appreciated.
If anyone wants to see the actual code (roughly 1200 lines), here are the files as well as the CodeBlocks projects. I'm not asking you guys to look through it, that would be crazy! But if you could run it on your systems in your setups and tell me if you observe anything differently that would be really nice.
Original:
http://filecloud.io/euqxa6ld
Leaking string class:
http://filecloud.io/mt6qwr8l
And a simple script to test it:
1 2 3 4 5 6
|
int function(int $arg1, string $arg2, int $const)
{
int $tmp = $arg1+ $const;
print($arg2);
return $tmp;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include "include.txt"
int $var = 5;
string $str = "dog"; //this is a comment
/*
Block Comment
Wooo!
*/
beg:
print("Beg\n");
pause();
$var = ($var + 5) * 2 - 90 / 3;
print("Var = ", $var, "\n");
if ($var < 15)
{
$str = $str + "gy";
}
if ($str == "doggy")
$var = $var - 15;
print("Var = ", $var, "\n");
while ($var<=5)
{
$var = $var + 1;
print("Var = ", $var, "\n");
}
print(function($var, $str, 78));
goto beg;
|