Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualTallkotten

Posted 12 August 2012 - 12:48 PM

Hello again Gamedev forum!

I'm back with a mind twister. I've sat with this for a few hours and haven't moved an inch forward. I am beaten.

The problem is with a pointer. I've "watched" it while debugging to follow it. And basically this is how it goes.

Object created, pointer is correct -> sends it around abit until finally arrived in Class "Content" -> pointer is correct -> Assign new pointer the same value -> Still correct -> Adjust some values in the object with help from pointer -> Goes back to gameLoop -> back into Content class -> update function running -> Program crashes

The reason why it crashes is because it tries to update the same values it changed earlier with the pointer stored in the class. The pointer has now changed from being a pointer to a Class to being a pointer to the Classes Interface which no real code in it.

And the annoying thing is that I've got 3 different classes that uses the same interface and the error only occurs with this newly created class, ergo I've must have screwed something up. But i can for the love of god not locate it.

I'm going to post the code to these steps now as well as upload the project in case someone wants to try to debug it on his/her own.

Object created, with the createGold function:
int gold = 20*level*(diff/2) + (rand() % 20*level*2 - 10*level);
	if(gold > 0)
	{
		lootContVector.at(vectorsize-1)->moveToBag(createGold(x, y, gold));
	}

Item* itemMngr::createGold(int x, int y, int amount)
{
	Item* generateditem = NULL;
	generateditem = new Currency(x, y, amount);
	if(generateditem != NULL)
	{
		itemVector.push_back(generateditem);
	}
	return generateditem;
}

Sends it forward to a newly created lootcontainer's function
bool LootContainer::moveToBag(Item* itemAdd)
{
	bool itemMoved = false;
	for(vector<Container*>::iterator it = containers.begin(); it != containers.end(); it++)
	{
		if(!itemMoved)
		{
			if(!(*it)->getFull())
			{
				(*it)->addItem(itemAdd);
				itemMoved = true;
			}
		}
	}
	return itemMoved;
}

Adds it to the content and changes some values
void Container::addItem(Item* itemAdd)
{
	if(content != NULL)
	{
		removeItem();
	}
	if(itemAdd != NULL)
	{
		content = itemAdd;
		content->setInCont(true);
		content->setX(contBox.x);
		content->setY(contBox.y);
		full = true;
	}
}

Goes back to the gameloop which sends an update command that eventually reaches the Container class within lootcontainer
void Container::update()
{
	if(content == NULL)
	{
		full = false;
	}
	else
	{
		  content->setX(contBox.x);
		  content->setY(contBox.y);
	}
}

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".

Another weird thing it that is does work sometimes, if you are quick enough to spawn the loot... but only once.

If you want to download this yourself you should know that it is my first project and probably has a lot of sloppy code in it. To make the error occur you must spawn a Chest by pressing "R" and open it up.

Greatly appreciates help Posted Image

EDIT: The attached file does not come with any DLL's... i am running all the SDL DLL's and by adding these files to the project folder you should be able to run it: https://www.dropbox.com/s/p8jz7na8yodojwc/0.0.0080.rar

#3Tallkotten

Posted 12 August 2012 - 12:45 PM

Hello again Gamedev forum!

I'm back with a mind twister. I've sat with this for a few hours and haven't moved an inch forward. I am beaten.

The problem is with a pointer. I've "watched" it while debugging to follow it. And basically this is how it goes.

Object created, pointer is correct -> sends it around abit until finally arrived in Class "Content" -> pointer is correct -> Assign new pointer the same value -> Still correct -> Adjust some values in the object with help from pointer -> Goes back to gameLoop -> back into Content class -> update function running -> Program crashes

The reason why it crashes is because it tries to update the same values it changed earlier with the pointer stored in the class. The pointer has now changed from being a pointer to a Class to being a pointer to the Classes Interface which no real code in it.

And the annoying thing is that I've got 3 different classes that uses the same interface and the error only occurs with this newly created class, ergo I've must have screwed something up. But i can for the love of god not locate it.

I'm going to post the code to these steps now as well as upload the project in case someone wants to try to debug it on his/her own.

Object created, with the createGold function:
int gold = 20*level*(diff/2) + (rand() % 20*level*2 - 10*level);
	if(gold > 0)
	{
		lootContVector.at(vectorsize-1)->moveToBag(createGold(x, y, gold));
	}

Item* itemMngr::createGold(int x, int y, int amount)
{
	Item* generateditem = NULL;
	generateditem = new Currency(x, y, amount);
	if(generateditem != NULL)
	{
		itemVector.push_back(generateditem);
	}
	return generateditem;
}

Sends it forward to a newly created lootcontainer's function
bool LootContainer::moveToBag(Item* itemAdd)
{
	bool itemMoved = false;
	for(vector<Container*>::iterator it = containers.begin(); it != containers.end(); it++)
	{
		if(!itemMoved)
		{
			if(!(*it)->getFull())
			{
				(*it)->addItem(itemAdd);
				itemMoved = true;
			}
		}
	}
	return itemMoved;
}

Adds it to the content and changes some values
void Container::addItem(Item* itemAdd)
{
	if(content != NULL)
	{
		removeItem();
	}
	if(itemAdd != NULL)
	{
		content = itemAdd;
		content->setInCont(true);
		content->setX(contBox.x);
		content->setY(contBox.y);
		full = true;
	}
}

Goes back to the gameloop which sends an update command that eventually reaches the Container class within lootcontainer
void Container::update()
{
	if(content == NULL)
	{
		full = false;
	}
	else
	{
    	  content->setX(contBox.x);
          content->setY(contBox.y);
	}
}

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".

Another weird thing it that is does work sometimes, if you are quick enough to spawn the loot... but only once.

If you want to download this yourself you should know that it is my first project and probably has a lot of sloppy code in it. To make the error occur you must spawn a Chest by pressing "R" and open it up.

Greatly appreciates help Posted Image

#2Tallkotten

Posted 12 August 2012 - 12:44 PM

Hello again Gamedev forum!

I'm back with a mind twister. I've sat with this for a few hours and haven't moved an inch forward. I am beaten.

The problem is with a pointer. I've "watched" it while debugging to follow it. And basically this is how it goes.

Object created, pointer is correct -> sends it around abit until finally arrived in Class "Content" -> pointer is correct -> Assign new pointer the same value -> Still correct -> Adjust some values in the object with help from pointer -> Goes back to gameLoop -> back into Content class -> update function running -> Program crashes

The reason why it crashes is because it tries to update the same values it changed earlier with the pointer stored in the class. The pointer has now changed from being a pointer to a Class to being a pointer to the Classes Interface which no real code in it.

And the annoying thing is that I've got 3 different classes that uses the same interface and the error only occurs with this newly created class, ergo I've must have screwed something up. But i can for the love of god not locate it.

I'm going to post the code to these steps now as well as upload the project in case someone wants to try to debug it on his/her own.

Object created, with the createGold function:
int gold = 20*level*(diff/2) + (rand() % 20*level*2 - 10*level);
	if(gold > 0)
	{
		lootContVector.at(vectorsize-1)->moveToBag(createGold(x, y, gold));
	}

Item* itemMngr::createGold(int x, int y, int amount)
{
	Item* generateditem = NULL;
	generateditem = new Currency(x, y, amount);
	if(generateditem != NULL)
	{
		itemVector.push_back(generateditem);
	}
	return generateditem;
}

Sends it forward to a newly created lootcontainer's function
bool LootContainer::moveToBag(Item* itemAdd)
{
	bool itemMoved = false;
	for(vector<Container*>::iterator it = containers.begin(); it != containers.end(); it++)
	{
		if(!itemMoved)
		{
			if(!(*it)->getFull())
			{
				(*it)->addItem(itemAdd);
				itemMoved = true;
			}
		}
	}
	return itemMoved;
}

Adds it to the content and changes some values
void Container::addItem(Item* itemAdd)
{
	if(content != NULL)
	{
		removeItem();
	}
	if(itemAdd != NULL)
	{
		content = itemAdd;
		content->setInCont(true);
		content->setX(contBox.x);
		content->setY(contBox.y);
		full = true;
	}
}

Goes back to the gameloop which sends an update command that eventually reaches the Container class within lootcontainer
void Container::update()
{
	if(content == NULL)
	{
		full = false;
	}
	else
	{
        content->setX(contBox.x);
		content->setY(contBox.y);
	}
}

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".

Another weird thing it that is does work sometimes, if you are quick enough to spawn the loot... but only once.

If you want to download this yourself you should know that it is my first project and probably has a lot of sloppy code in it. To make the error occur you must spawn a Chest by pressing "R" and open it up.

Greatly appreciates help Posted Image

#1Tallkotten

Posted 12 August 2012 - 12:43 PM

Hello again Gamedev forum!

I'm back with a mind twister. I've sat with this for a few hours and haven't moved an inch forward. I am beaten.

The problem is with a pointer. I've "watched" it while debugging to follow it. And basically this is how it goes.

Object created, pointer is correct -> sends it around abit until finally arrived in Class "Content" -> pointer is correct -> Assign new pointer the same value -> Still correct -> Adjust some values in the object with help from pointer -> Goes back to gameLoop -> back into Content class -> update function running -> Program crashes

The reason why it crashes is because it tries to update the same values it changed earlier with the pointer stored in the class. The pointer has now changed from being a pointer to a Class to being a pointer to the Classes Interface which no real code in it.

And the annoying thing is that I've got 3 different classes that uses the same interface and the error only occurs with this newly created class, ergo I've must have screwed something up. But i can for the love of god not locate it.

I'm going to post the code to these steps now as well as upload the project in case someone wants to try to debug it on his/her own.

Object created, with the createGold function:
int gold = 20*level*(diff/2) + (rand() % 20*level*2 - 10*level);
	if(gold > 0)
	{
		lootContVector.at(vectorsize-1)->moveToBag(createGold(x, y, gold));
	}

Item* itemMngr::createGold(int x, int y, int amount)
{
	Item* generateditem = NULL;
	generateditem = new Currency(x, y, amount);
	if(generateditem != NULL)
	{
		itemVector.push_back(generateditem);
	}
	return generateditem;
}

Sends it forward to a newly created lootcontainer's function
bool LootContainer::moveToBag(Item* itemAdd)
{
	bool itemMoved = false;
	for(vector<Container*>::iterator it = containers.begin(); it != containers.end(); it++)
	{
		if(!itemMoved)
		{
			if(!(*it)->getFull())
			{
				(*it)->addItem(itemAdd);
				itemMoved = true;
			}
		}
	}
	return itemMoved;
}

Adds it to the content and changes some values
void Container::addItem(Item* itemAdd)
{
	if(content != NULL)
	{
		removeItem();
	}
	if(itemAdd != NULL)
	{
		content = itemAdd;
		content->setInCont(true);
		content->setX(contBox.x);
		content->setY(contBox.y);
		full = true;
	}
}

Goes back to the gameloop which sends an update command that eventually reaches the Container class within lootcontainer
void Container::update()
{
	if(content == NULL)
	{
		full = false;
	}
	else
	{
		[color=#ff0000]content->setX(contBox.x);[/color]
		content->setY(contBox.y);
	}
}

The red text marks where it crashes. 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".

Another weird thing it that is does work sometimes, if you are quick enough to spawn the loot... but only once.

If you want to download this yourself you should know that it is my first project and probably has a lot of sloppy code in it. To make the error occur you must spawn a Chest by pressing "R" and open it up.

Greatly appreciates help Posted Image

PARTNERS