Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualTallkotten

Posted 12 August 2012 - 03:20 PM

The C++ keyword 'break' lets you break out of the most recent loop, so this:

for(vector<Container*>::iterator it = containers.begin(); it != containers.end(); it++)
		{
				if(!itemMoved)
				{
						if(!(*it)->getFull())
						{
								(*it)->addItem(itemAdd);
								itemMoved = true;
						}
				}
		}
...can be converted to:
for(vector<Container*>::iterator it = containers.begin(); it != containers.end(); it++)
{
	   if(!(*it)->getFull())
	  {
			 (*it)->addItem(itemAdd);
			 break;
	  }
}

That's unrelated to your problem, though.


Thank, will use that in the future! But the reasons i use the bool there is because other code need to check if that code succeeds or not ;)

I expect the cause of your crash is here: (you have "if(content != NULL)" twice in a row) (oops, misread the code statements)

void Container::addItem(Item* itemAdd)
{
		//If  'content' (whatever that is!) is not NULL.
		if(content != NULL)
		{
				removeItem(); //Maybe you invalidate the pointer here by mistake?
		}
		//You repeat the same condition statement... Did you mean "is null?". You probably want an 'else' here.
		if(itemAdd != NULL)
		{
				content = itemAdd;
				content->setInCont(true);
				content->setX(contBox.x); //What is 'contBox'?
				content->setY(contBox.y);
				full = true;
		}
}


Content is declared as "Item* content" and stores the pointer to the object which is locked in container (think of a bag slot in WOW or any other RPG).

removeitem sets the content variable to NULL, so i'm sure that not the problem. Actually i dont think the problem is in the Container class since both the class Weapon and Armor which got Item as an interface works fine.

contBox is the location and size of the container.

I'm pretty sure with any modern day compiler, your able to stop the execution of code at your desired lines. So start from the top of the stack,

I have a feeling your are slicing, you could try declare your setX and setY as virtual (in the inheritted classes) - or it could be possible that you've casted an object that doesn't even have the setX/setY

As you are calling a Pointer to Item, if you wish to have Polymorphic functionality you need to declare virtual on the desired functions eg

in weapon.cpp

void Weapon::setX(int x)
{
	box.x = x;
}

is now

virtual void Weapon::setX(int x)
{
	box.x = x;
}

I'm not sure what object Item* is supposed to represent, I've gone through your code briefly but I have not compiled it


I actually doesn't really know how to debug roperly. I've learned myself to use Watches and Call-stack aswell as breakpoints. And as i said in my first post "This code is where it crashes: "content->setX(contBox.x);". If i comment that out it will instead crash on the render function. The reason is that the pointer is no longer (Currency*) but (class Item*) in the "watches window".".

Whilst googeling around on the problem i was having i say some post about slicing. I don't really know what it is thought. Might have to read into that.

Why would i make them virtual in the inheritted classes? What would that change, i only thought you made them virtual in interfaces.

Item* is an interface all the items will be using in the game. At the moment there are only "Weapon", "Armor" and the broken "Currency". this is my first project ever so i'm pretty sure i'll have to rewrite my code from the start at some point, even now i see how much worse i was at coding when i look at the little older code in my project.

Well without debug symbols I can't really debug, also disassembly looks way too clean to be debug build. It crashes on nullptr error, looks like virtual function call. Do you ever use memcpy() or something similar, that could corrupt itemAdd somewhere before Container::Update()?

Edit:
After running several times I noticed it doesn't crash that often. Somehow I feel it might be heap corruption.


I compiled it as debug, what did you mean i should do?

To your Edit:
Yeah, it doesn't but when you debug in CodeBlocks it crashes every time, but like you said, when you just run the code it doesn't crash all the time.

----

I think the problematic code lies within itemmngr.cpp in the randLoot func and createGold func. Or maybe in the Currency class itself. Because that is the only place i treat Currency any different from Weapon or Armor (also part of Item) and they work fine.

#1Tallkotten

Posted 12 August 2012 - 03:17 PM

The C++ keyword 'break' lets you break out of the most recent loop, so this:

for(vector<Container*>::iterator it = containers.begin(); it != containers.end(); it++)
		{
				if(!itemMoved)
				{
						if(!(*it)->getFull())
						{
								(*it)->addItem(itemAdd);
								itemMoved = true;
						}
				}
		}
...can be converted to:
for(vector<Container*>::iterator it = containers.begin(); it != containers.end(); it++)
{
	   if(!(*it)->getFull())
	  {
			 (*it)->addItem(itemAdd);
			 break;
	  }
}

That's unrelated to your problem, though.


Thank, will use that in the future!

I expect the cause of your crash is here: (you have "if(content != NULL)" twice in a row) (oops, misread the code statements)

void Container::addItem(Item* itemAdd)
{
		//If  'content' (whatever that is!) is not NULL.
		if(content != NULL)
		{
				removeItem(); //Maybe you invalidate the pointer here by mistake?
		}
		//You repeat the same condition statement... Did you mean "is null?". You probably want an 'else' here.
		if(itemAdd != NULL)
		{
				content = itemAdd;
				content->setInCont(true);
				content->setX(contBox.x); //What is 'contBox'?
				content->setY(contBox.y);
				full = true;
		}
}


Content is declared as "Item* content" and stores the pointer to the object which is locked in container (think of a bag slot in WOW or any other RPG).

removeitem sets the content variable to NULL, so i'm sure that not the problem. Actually i dont think the problem is in the Container class since both the class Weapon and Armor which got Item as an interface works fine.

contBox is the location and size of the container.

I'm pretty sure with any modern day compiler, your able to stop the execution of code at your desired lines. So start from the top of the stack,

I have a feeling your are slicing, you could try declare your setX and setY as virtual (in the inheritted classes) - or it could be possible that you've casted an object that doesn't even have the setX/setY

As you are calling a Pointer to Item, if you wish to have Polymorphic functionality you need to declare virtual on the desired functions eg

in weapon.cpp

void Weapon::setX(int x)
{
	box.x = x;
}

is now

virtual void Weapon::setX(int x)
{
	box.x = x;
}

I'm not sure what object Item* is supposed to represent, I've gone through your code briefly but I have not compiled it


I actually doesn't really know how to debug roperly. I've learned myself to use Watches and Call-stack aswell as breakpoints. And as i said in my first post "This code is where it crashes: "content->setX(contBox.x);". If i comment that out it will instead crash on the render function. The reason is that the pointer is no longer (Currency*) but (class Item*) in the "watches window".".

Whilst googeling around on the problem i was having i say some post about slicing. I don't really know what it is thought. Might have to read into that.

Why would i make them virtual in the inheritted classes? What would that change, i only thought you made them virtual in interfaces.

Item* is an interface all the items will be using in the game. At the moment there are only "Weapon", "Armor" and the broken "Currency". this is my first project ever so i'm pretty sure i'll have to rewrite my code from the start at some point, even now i see how much worse i was at coding when i look at the little older code in my project.

Well without debug symbols I can't really debug, also disassembly looks way too clean to be debug build. It crashes on nullptr error, looks like virtual function call. Do you ever use memcpy() or something similar, that could corrupt itemAdd somewhere before Container::Update()?

Edit:
After running several times I noticed it doesn't crash that often. Somehow I feel it might be heap corruption.


I compiled it as debug, what did you mean i should do?

To your Edit:
Yeah, it doesn't but when you debug in CodeBlocks it crashes every time, but like you said, when you just run the code it doesn't crash all the time.

----

I think the problematic code lies within itemmngr.cpp in the randLoot func and createGold func. Or maybe in the Currency class itself. Because that is the only place i treat Currency any different from Weapon or Armor (also part of Item) and they work fine.

PARTNERS