FileHandler Error
ive writen a filehandler for loading file containig game data (maps, scripts, configs...who knows what else...) in ascii and binary using fstream.h
after prototyping with the code i finally put everything in an class and made it working and everything wents just fine until the CloseFile(); is called, but the debugger shows that all code in there is succesfully executed and after that an big bad window shows saying "DEBUG ERROR Programm: ...Maps.exe DAMAGE after Block #52 at 0x...."
if i press the Ignore button from this window the programm does its stuff as if nothing happend
here is the code from CloseFile()
void CFile::CloseFile()
{
if ((filemode & FILE_WRITE) != 0)
file_out.close();
file_in.close();
filemode = FILE_NOTHING;
pos = 0;
}
somebody know how to get rid of this evil error, already tried everything
maybe its a problem of the debuger (cause "DEBUG ERROR...") from the IDE (im using vc++ 6.0)
thx in advance 4 every good suggestion
I think you need to post more code if you want any help. My gut feeling is that your probably doing something with a file after its closed or something like that. However, I don't see anything that should be blowing up in that particular function. Perhaps the problem occurs elsewhere?
your right i called the CloseFile() function also in the destructor of the class
what a BAD idea
thank you rating+
EDIT: problem isnt solved, still same error
what a BAD idea
thank you rating+
EDIT: problem isnt solved, still same error
Can we see your read / write code? "DAMAGE after normal block" means you're overwriting an array. Also, what are the types of "file_out" and "file_in"?
file_in is a ifstream
file_out is a ofstream (isnt used in the current example)
here is some code
CFile.cpp //ithink this may be to much to read, so i cut this a little down
CFile::CFile()
{
filemode = FILE_NOTHING;
pos = 0;
}
CFile::~CFile()
{
// CloseFile();
}
bool CFile::OpenFile(const char* filename, unsigned int mode)
{
if (filemode != FILE_NOTHING) return false; //object is already in use, first close
filemode = mode;
int open = ios::in;
//if nothing is said about binary or ascci code the first sign decides (A or a for ascii)
char c;
if (((filemode & FILE_BINARY) == 0) && ((filemode & FILE_ASCII) == 0)) {
file_in.open(filename);
if (!file_in) return false;
file_in.get(c);
if ((c == 'A') || (c == 'a'))
filemode = (filemode || FILE_ASCII);
else
open |= ios::binary;
file_in.close();
}
file_in.open(filename, open);
if (!file_in) return false;
if ((filemode & FILE_WRITE) != 0) {
open |= ios::out;
open = open^ios::in;
if ((filemode & FILE_NEW_TRUNC) != 0) open |= ios::trunc;
else open |= ios::app;
file_out.open(filename, open);
if (!file_out) return false; }
return true;
}
void CFile::CloseFile()
{
if ((filemode & FILE_WRITE) != 0)
file_out.close();
file_in.close();
filemode = FILE_NOTHING;
pos = 0;
}
....
it also may be in that code
bool CMap::OpenMap(const char* ScenePath)
{
bool success;
if (!file.OpenFile(ScenePath, FILE_ASCII))
return false;
success = file.GetUInt(Size); file.EatWhite();
success = success && (file.GetUInt(Texs)); file.EatWhite();
unsigned int TexNameLength;
success = success && (file.GetUInt(TexNameLength)); file.EatWhite();
success = success && (file.GetUInt(Width)); file.EatWhite();
success = success && (file.GetUInt(Height)); file.EatWhite();
if (!success) { file.CloseFile(); return false; } //error-handler
success = true;
unsigned int len = Height * Width;
Textures = new char * [Texs];
Parts = new MapQuad [Size];
Collision= new bool [len];
unsigned int b1, b2;
for (b1 = 0; b1 < len; ++b1)
Collision[b1] = false;
char c; char * f; char * token;
for (unsigned int a = 0; a < Texs; ++a)
{
//comment
file.EatWhite();
c = (char) file.Peek();
while (c == '|')
{
while (c != '\n')
success = success && (file.GetChar(c));
c = (char) file.Peek();
}
f = new char[TexNameLength];
file.GetText(f, TexNameLength, '\n');
token = strtok(f, "\n");
if (token == NULL) { delete [] f;
success = false;
break;}
Textures[a] = new char [strlen(token)];
strcpy(Textures[a], f);
delete [] f;
}
if (success)
{
file.EatWhite();
signed int coll;
unsigned int x1, x2, y1, y2;
for (a = 0; a < Size; ++a)
{
file.EatWhite();
c = (char) file.Peek();
while (c == '|')
{
while (c != '\n')
file.GetChar(c);
c = (char) file.Peek();
}
for (b1 = 0; b1 < 2; ++b1) {
success = file.GetInt(Parts[a].x[b1]); file.EatWhite();
success = success && file.GetInt(Parts[a].y[b1]); file.EatWhite();
}
success = success && file.GetInt(coll);
if (!success) break;
if (coll != 0) {
x1 = Parts[a].x[0]; x2 = Parts[a].x[1];
y1 = Parts[a].y[0]; y2 = Parts[a].y[1];
for (b1 = y1; b1 <= y2; ++b1)
for (b2 = x1; b2 <= x2; ++b2) {
Collision[b1 * Width + b2] = true; }
}
file.EatWhite();
success = success && file.GetUInt(Parts[a].textur);
for (b1 = 0; b1 < 4; ++b1) {
success = success && file.GetFloat(Parts[a].u[b1]); file.EatWhite();
success = success && file.GetFloat(Parts[a].v[b1]); file.EatWhite();
}
if (!success) break;
}
}
file.CloseFile();
return success;
}
[Edited by - Becko on March 29, 2005 1:36:34 PM]
file_out is a ofstream (isnt used in the current example)
here is some code
CFile.cpp //ithink this may be to much to read, so i cut this a little down
CFile::CFile()
{
filemode = FILE_NOTHING;
pos = 0;
}
CFile::~CFile()
{
// CloseFile();
}
bool CFile::OpenFile(const char* filename, unsigned int mode)
{
if (filemode != FILE_NOTHING) return false; //object is already in use, first close
filemode = mode;
int open = ios::in;
//if nothing is said about binary or ascci code the first sign decides (A or a for ascii)
char c;
if (((filemode & FILE_BINARY) == 0) && ((filemode & FILE_ASCII) == 0)) {
file_in.open(filename);
if (!file_in) return false;
file_in.get(c);
if ((c == 'A') || (c == 'a'))
filemode = (filemode || FILE_ASCII);
else
open |= ios::binary;
file_in.close();
}
file_in.open(filename, open);
if (!file_in) return false;
if ((filemode & FILE_WRITE) != 0) {
open |= ios::out;
open = open^ios::in;
if ((filemode & FILE_NEW_TRUNC) != 0) open |= ios::trunc;
else open |= ios::app;
file_out.open(filename, open);
if (!file_out) return false; }
return true;
}
void CFile::CloseFile()
{
if ((filemode & FILE_WRITE) != 0)
file_out.close();
file_in.close();
filemode = FILE_NOTHING;
pos = 0;
}
....
it also may be in that code
bool CMap::OpenMap(const char* ScenePath)
{
bool success;
if (!file.OpenFile(ScenePath, FILE_ASCII))
return false;
success = file.GetUInt(Size); file.EatWhite();
success = success && (file.GetUInt(Texs)); file.EatWhite();
unsigned int TexNameLength;
success = success && (file.GetUInt(TexNameLength)); file.EatWhite();
success = success && (file.GetUInt(Width)); file.EatWhite();
success = success && (file.GetUInt(Height)); file.EatWhite();
if (!success) { file.CloseFile(); return false; } //error-handler
success = true;
unsigned int len = Height * Width;
Textures = new char * [Texs];
Parts = new MapQuad [Size];
Collision= new bool [len];
unsigned int b1, b2;
for (b1 = 0; b1 < len; ++b1)
Collision[b1] = false;
char c; char * f; char * token;
for (unsigned int a = 0; a < Texs; ++a)
{
//comment
file.EatWhite();
c = (char) file.Peek();
while (c == '|')
{
while (c != '\n')
success = success && (file.GetChar(c));
c = (char) file.Peek();
}
f = new char[TexNameLength];
file.GetText(f, TexNameLength, '\n');
token = strtok(f, "\n");
if (token == NULL) { delete [] f;
success = false;
break;}
Textures[a] = new char [strlen(token)];
strcpy(Textures[a], f);
delete [] f;
}
if (success)
{
file.EatWhite();
signed int coll;
unsigned int x1, x2, y1, y2;
for (a = 0; a < Size; ++a)
{
file.EatWhite();
c = (char) file.Peek();
while (c == '|')
{
while (c != '\n')
file.GetChar(c);
c = (char) file.Peek();
}
for (b1 = 0; b1 < 2; ++b1) {
success = file.GetInt(Parts[a].x[b1]); file.EatWhite();
success = success && file.GetInt(Parts[a].y[b1]); file.EatWhite();
}
success = success && file.GetInt(coll);
if (!success) break;
if (coll != 0) {
x1 = Parts[a].x[0]; x2 = Parts[a].x[1];
y1 = Parts[a].y[0]; y2 = Parts[a].y[1];
for (b1 = y1; b1 <= y2; ++b1)
for (b2 = x1; b2 <= x2; ++b2) {
Collision[b1 * Width + b2] = true; }
}
file.EatWhite();
success = success && file.GetUInt(Parts[a].textur);
for (b1 = 0; b1 < 4; ++b1) {
success = success && file.GetFloat(Parts[a].u[b1]); file.EatWhite();
success = success && file.GetFloat(Parts[a].v[b1]); file.EatWhite();
}
if (!success) break;
}
}
file.CloseFile();
return success;
}
[Edited by - Becko on March 29, 2005 1:36:34 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement