Hi everyone
I'm working on this project :
https://github.com/zsteve/zebra-zmachine-interpreter
which is supposed to be an interpreter for z-machine story files, but I'm still working on it.
Anyway, the error I'm having is within ztext.cpp and ZDictionary's unit test main.cpp.
zChartoDictionaryZCharString() is supposed to return a pointer to a string/array of zwords.
It is creating/calculating that array properly, but is strangely failing to return it properly...
the error occurs at line 499 of ztext.cpp :
return (outWord);
Right at this point, outWord points to an array of zwords : {15 25, 28 ab} allocated by new[]
Once it returns to line 42 in
https://github.com/zsteve/zebra-zmachine-interpreter/blob/master/zdictionary/zdictionary/main.cpp ,
the returned zword* now points to {50 00, 00 00}, which shouldn't happen.
I spent an hour debugging the entire thing until I figured out that this was where it went wrong.
So my question is : what causes this problem in my program?
I suspect it's either unmatched new[]/delete[] (could this cause it?) or the std::vector objects in zChartoDictionaryZCharString().
Here is the code :
in main.cpp (/zdictionary/zdictionary/main.cpp) from line 40:
1 2 3 4
|
// error occurs here :
// at this point : tmp == {15 25 28 ab}
zword* tmp=zChartoDictionaryZCharString(tmp_);
|
in ztext.cpp (/ztext/ztext/ztext.cpp) from line 502:
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
zword* zChartoDictionaryZCharString(zword* zstring)
{
// converts a normal zchar string to a dictionary zchar string
// returns a zword* to either a 2 or 3 zword array.
std::vector<zchar> vector1=expandZChars(zstring);
std::vector<zchar> vector2(0);
if(zVersion<=3){
// for versions 1 to 3, dictionary word length is 4 bytes (6 zchars)
int done=0;
for(int i=0; i<vector1.size() && i<6; i++, done++)
{
vector2.push_back(vector1[i]);
}
if(done<6)
{
for(int i=done; i<6; i++)
{
vector2.push_back(5);
}
}
// now we need to pack them to 2x zwords.
zword* outData=new zword[2];
zchar* zcharArray=new zchar[vector2.size()];
for(int i=0; i<vector2.size(); i++) zcharArray[i]=vector2[i];
outData[0]=endianize(packZChars(zcharArray));
outData[1]=endianize(packZChars(zcharArray+3)|128);
delete[] zcharArray;
vector1.clear();
vector2.clear();
return outData;
}else if(zVersion>3){
// for versions 4 and up, dictionary word length is 6 bytes (9 zchars)
int done=0;
for(int i=0; i<vector1.size() && i<9; i++, done++)
{
vector2.push_back(vector1[i]);
}
if(done<9)
{
for(int i=done; i<9; i++)
{
vector2.push_back(5);
}
}
// now we need to pack them to 3x zwords.
zword* outData=new zword[3];
zchar* zcharArray=new zchar[vector2.size()];
for(int i=0; i<vector2.size(); i++) zcharArray[i]=vector2[i];
outData[0]=packZChars(zcharArray);
outData[1]=packZChars(zcharArray+3);
outData[2]=packZChars(zcharArray+6);
outData[2]|=128;
for(int i=0; i<3; i++) outData[i]=endianize(outData[i]);
delete[] zcharArray;
return outData;
}
}
|
Any help on fixing this would be greatly appreciated.
Thanks!