Skip to main content
gamedev.net
🔒 Locked

error when exiting program - new class?

Started by Neen10do Apr 24, 2003 at 9:12 PM 10 replies 350+ views
Original Post
Neen10do
Neen10do
ive created a sprite class, which will be contained in a cObject class. ive created it and its tested fine on its own. now im starting to create the cObject class, but right now i only have the one cSprite member in it. when the program shuts down, the proram gives me an error: "gamedx as caused an error in GAMEDX.EXE" now normally i get this error and i can fix it. but this only happens when declaring a cObject class (because the cSprite class tested by itself fine. here is my cObject class code, its really small. is there anything in this class structure, or implementation (although the cObject instance is not imeplemented in my program to isolate the problem, this proves that the error is coming from just declaring an instance). cObject.h
  
//OBJECT CLASS


//INCLUDES//////////////

#include "cSprite.h"
////////////////////////


#pragma once

//OBJECT CLASS//////////////////////////

class cObject
{
public:
	cSprite         mySprite;

	//Constructor and Destructor

	cObject();
	~cObject();

	//Reads all information from a file

	void DataFromFile(char* filename, LPDIRECTDRAW7 renderer);
};
////////////////////////////////////////

  
and this is the implementation (although i dont think this is the problem)
  
//COBJECT IMPLEMENTATION FILE

#include "cObject.h"
#include <fstream.h>
//Constructor and Destructor///////////

cObject::cObject()
{

}
cObject::~cObject()
{

}
///////////////////////////////////////


//LOAD FROM FILE///////////////////////

void cObject::DataFromFile(char* filename, LPDIRECTDRAW7 renderer)
{
	//used to take in comment lines

	char* decoy = new char;

	//Creates the file stream//////

	ifstream in_file;
	in_file.open(filename);
	///////////////////////////////

	
	//Loads the Sprite info////////

	in_file >> decoy;
	in_file >> mySprite.file;
	in_file >> mySprite.myWidth;
	in_file >> mySprite.myHeight;
	in_file >> mySprite.rows;
	in_file >> mySprite.cols;
	in_file >> mySprite.myXCell;
	in_file >> mySprite.myYCell;
	in_file >> mySprite.myX;
	in_file >> mySprite.myY;

	//mySprite.SetBitmap(renderer, mySprite.file);

	///////////////////////////////


	//Closes the file stream///////

	in_file.close();
	decoy = NULL;
	delete decoy;
	///////////////////////////////

}
///////////////////////////////////////

  
any ideas? ---------------------- i code therefore i am. Aero DX - Coming to a bored Monitor near you!
----------------------i code therefore i am.Aero DX - Coming to a bored Monitor near you!
Sneftel
Sneftel
When you run it in the debugger, where does the error happen?


How appropriate. You fight like a cow.
Neen10do
Neen10do
when shutting down the program (eg pressing escape).

----------------------
i code therefore i am.

Aero DX - Coming to a bored Monitor near you!
----------------------i code therefore i am.Aero DX - Coming to a bored Monitor near you!
rypyr
rypyr
You have to be careful with class dependencies. Always ensure that you are rebuilding all your source and then see if you get the problem.

Also, if it happens during shutdown then it could be due to a destructor doing something bad. What does the cSprite destructor do? In fact, what does the cSprite default constructor (no args) do? Post some code there.

Then show the code that instantiates the cObject object.

Regards,
Jeff
Sneftel
Sneftel
quote:
Original post by Neen10do
when shutting down the program (eg pressing escape).


No. What line of code?


How appropriate. You fight like a cow.
Neen10do
Neen10do
i took the decoy comepletely out (Even from my file), and i still get a debugger problem. it says:

"Error in Debugger! DAMAGE: after normal block (#16) at 0x009A0E10"

i have no idea what that means.

the contructor for cObject is empty.

----------------------
i code therefore i am.

Aero DX - Coming to a bored Monitor near you!
----------------------i code therefore i am.Aero DX - Coming to a bored Monitor near you!
Neen10do
Neen10do
hey nm i think i got it. in the destructor for the cSprite, i do this:

delete file;

when in the constructor file is defined as: char* file = new char;

i thought that it was as pointer, so i had to delete it, i guess not.

----------------------
i code therefore i am.

Aero DX - Coming to a bored Monitor near you!
----------------------i code therefore i am.Aero DX - Coming to a bored Monitor near you!
matthias6S
matthias6S
quote:
Original post by Neen10do



    
//LOAD FROM FILE///////////////////////

void cObject::DataFromFile(char* filename, LPDIRECTDRAW7 renderer)
{
//used to take in comment lines

char* decoy = new char;

//Creates the file stream//////

ifstream in_file;
in_file.open(filename);
///////////////////////////////


//Loads the Sprite info////////

in_file >> decoy;
in_file >> mySprite.file;

[...]

//Closes the file stream///////

in_file.close();
decoy = NULL;
delete decoy;
///////////////////////////////

}


I guess your sprite information is saved line by line, and you want to use the "char* decoy" to skip a useless line. First you must know that you don''t allocate memory for a whole line, but for just one character with "decoy = new char". I guess that then with "in_file >> decoy;" the memory gets damaged, because the line will most likely be longer than one character. This is the well known buffer overrun that is often used to exploit poorly written software. To get around this problem, free enough space for a whole line ("= new char[ 200 ]"). You don''t even have to allocate it dynamically (use "char decoy[ 200 ]" instead). The best way is to use a std::string, which handles the memory allocation for you. As already pointed out, the way you delete your char* is wrong as well.

Regards
Matt

Lead programmer at sechsta sinn - http://www.sechsta-sinn.com - mailto:matthias@sechsta-sinn.com
rypyr
rypyr
quote:
Original post by Neen10do
hey nm i think i got it. in the destructor for the cSprite, i do this:

delete file;

when in the constructor file is defined as: char* file = new char;

i thought that it was as pointer, so i had to delete it, i guess not.



Um, it's still a pointer...I suggest you read up on pointers and memory allocation.

char* file = new char;

allocates one byte of memory, while the pointer file points to.

Regards,
Jeff

Edit: Not only that, but if it's a local variable defined in the constructor, then there is no way to access the variable in the destructor so your comment is even more confusing. Or is it a class member variable?



[edited by - rypyr on April 25, 2003 10:11:57 AM]
Neen10do
Neen10do
its a class member variable.

----------------------
i code therefore i am.

Aero DX - Coming to a bored Monitor near you!
----------------------i code therefore i am.Aero DX - Coming to a bored Monitor near you!
Mephs
Mephs
As a piece of advice for the future, if you''re tracking down errors in destructors, specifically memory allocation/deallocation, it is often useful to utilise the call stack, when the error pops up in your program (assuming you''re in debug mode and using VC++ 6) hit "retry" to debug the app, and then use "Alt+7" to show the call stack window. The call stack window should locate the source of your error if you trace back a line or two (double click the lines in the call stack window). Appologies if you knew that already, but if you didn''t it can sometimes prove invaluable

Cheers,

Steve

------------------------------
Appolgies if I inadvertantly killed your thread, people have a tendancy to stop posting after I make a reply

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.