|
-
March 21st, 2010, 10:05 PM
#1
Assert error with destructor
So currently I am toying around with classes in the setting of a game in order to learn them better. However, when I attempt to free some dynamic memory with a class deconstructor, I the execution error "_BLOCK_IS_VALID"
//MONSTER.H
#ifndef MONSTER
#define MONSTER
class Monster
{
private:
char* description;
int atk;
int def;
int agi;
int HP;
public:
const char* getDescription();
int getAtk();
int getDef();
int getAgi();
int getHP();
void changeHP(int);
Monster(const char*, int, int, int, int);
~Monster();
};
#endif
//MONSTER.CPP
#include <iostream>
#include <cstring>
#include "Monster.h"
using namespace std;
const char* Monster::getDescription()
{
return description;
}
int Monster::getAtk()
{
return atk;
}
int Monster::getDef()
{
return def;
}
int Monster::getAgi()
{
return agi;
}
int Monster::getHP()
{
return HP;
}
void Monster::changeHP(int change)
{
HP+=change;
}
Monster::Monster(const char* desc, int attack, int defense, int agility, int hitPoints)
{
description=new char [strlen(desc)+1];
strcpy(description, desc);
atk=attack;
def=defense;
agi=agility;
HP=hitPoints;
}
Monster::~Monster()
{
delete [] description;
}
//MAINFILE.CPP
#include <iostream>
#include <iomanip>
#include "Monster.h"
using namespace std;
void displayMonster(Monster);
int main()
{
Monster ghost("Ghost",1,1,1,1);
displayMonster(ghost);
cin.get();
return 0;
}
void displayMonster(Monster monsterType)
{
cout<<"Monster name: "<<monsterType.getDescription()<<endl;
cout<<"Monster atk : "<<monsterType.getAtk()<<endl;
cout<<"Monster def : "<<monsterType.getDef()<<endl;
cout<<"Monster agil: "<<monsterType.getAgi()<<endl;
cout<<"Monster HP : "<<monsterType.getHP()<<endl;
}
I'm sorry if that took up a ton of space, I wasn't sure exactly what could be contributing to the error.
-
March 22nd, 2010, 09:00 AM
#2
Re: Assert error with destructor
 Originally Posted by gameman144
So currently I am toying around with classes in the setting of a game in order to learn them better. However, when I attempt to free some dynamic memory with a class deconstructor, I the execution error "_BLOCK_IS_VALID"
Please next time. use code tags when posting code.
The reason for your error is that your class lacks a user-defined copy constructor and assignment operator that copies the data pointed to by the pointer. You are passing the Monster by value to displayMonster, and passing by value means a copy will happen.
Why are you using char* and strcpy() in a C++ program, especially one for a beginner, and not std::string? If instead, you used std::string and not char*, you will no longer have the problem, and you do not need to code copy constructor and assignment operator.
I leave it to you to see why this simple two line program doesn't work, using your class.
Code:
int main()
{
Monster ghost("Ghost",1,1,1,1);
Monster ghost2 = ghost; // error
}
Regards,
Paul McKenzie
-
March 22nd, 2010, 09:12 AM
#3
Re: Assert error with destructor
It's a "double-deletion" problem.
When you call displayMonster
Code:
void displayMonster(Monster monsterType)
that routine is making a copy of the Monster object, but you have not defined a copy constructor or assignment operator for Monster, so the compiler defined one for you which simply copies all the fields inside the object, including "char * description".
When displayMonster ends the desctructor gets called and the description is deleted the first time. When main ends the ghost object is destroyed and the description at the same memory address is deleted again.
You probably want to add a copy-constructor to Monster for the sake of good programming practice, but in addition change displayMonster to
Code:
void displayMonster(const Monster * monsterType)
in order to avoid the structure copy.
-
March 22nd, 2010, 09:15 AM
#4
Re: Assert error with destructor
Oops, Paul McKenzie beat me to it. You can't tell when someone else is writing a reply at the same time.
Last edited by GeoRanger; March 22nd, 2010 at 09:19 AM.
Reason: fix grammar
-
March 22nd, 2010, 09:31 AM
#5
Re: Assert error with destructor
I would suggest passing to displayMonster by const reference rather than by pointer.
Alternatively, you could make display() a method of Monster to begin with. In that case, you'd probably want to pass in an istream to write to (by reference).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|