Jump to content
  • Advertisement
Sign in to follow this  
ManaStone

What can cause memory allocation to fail?

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a problem where memory is failing to allocate to a class using the new operator. This only happens when I add member variables to a class that calls another class that makes the allocation. I highly doubt is because I am out of heap memory considering how small the variables are. This is where the program is failing to allocate memory: //from what the debugger says spellbooks is now NULL.
  

void cNpc::Create(FILE *fp)
{
	//do a bunch of stuff
	….
	….
	//at this point spellbook is NULL
	if(type&MAGE || type&LORD || type&GUARD)
	{
		spellbook=new cSpellBook;

		//spellbook is still null

		spellbook->addSpell(app->spellList->getSpell(0));
		// run time error!

	}
}


This function is called by void cScene::addCreature(FILE *fp) which itself is called by void cScene::load(FILE *fp) The problem only happens when I add this to the class
  
class cScene{
	friend cNodeTree;
protected:	
	int x[40];// causes the problem//bunch of stuff
	
public:	
…
	//bunch of stuff


};



also it should be noted that it doesn't fail to allocate for each function call. Usually within the 9th addcreature to my scene.

Share this post


Link to post
Share on other sites
Advertisement
According to the standard, spellbook should never be null at that point - new throws an exception (std::bad_alloc IIRC) when you have an allocation failure, it does NOT return NULL/0 unless you specifically call the nothrow version, which would look like so:

spellbook = new (std::nothrow) cSpellBook;

Thus, either:

1) your debugger is wrong
2) the program is paused in a different spot that you think it is (most debuggers highlight the line before they execute it I think - really, they should put a line between lines, but oh well...)
3) you're using your debugger wrong
4) you've got an old, crappy, far from standards complaint compiler that misbehaves
5) Something's gotten seriously screwed up (read: you've corrupted memory somehow, or are accessing unallocated memory).

Compiler version and debugger, etc? Given that adding that int array causes the problem, I'm guessing you've probably got #5, possibly your cSpellBook constructor is accessing unallocated memory...?

Share this post


Link to post
Share on other sites
I'm using Visual C++ 6.0. I know I’m using the debugger correctly. Could you elaborate on number 5? What do you mean by corrupted memory?

This is everything in the spellbook Constructor


cSpellBook::cSpellBook()
{
//set the number of spells to 0
numSpells=0;

}




The runtime error occurs when it tries to set the spellbook’s spell pointer by getting it
the spellList. Because for some reason the new isn’t working, it is setting invalid memory.


Share this post


Link to post
Share on other sites
MSVC6 has the following behaviour (copied from MSVC6 MSDN):

If there is insufficient memory for the allocation request, by default operator new returns NULL.


That's against the standard, but it is what happens.

Put a breakpoint inside the constructor (or make it pop up a message box, print text to the console, beep the speaker -- anything) to make sure it is actually being called. If it is not being called, you're probably experiencing an out-of-memory condition. (Either by actually not having enough free address space, or because of memory corruption).

Share this post


Link to post
Share on other sites
Quote:
Original post by Kippesoep
MSVC6 has the following behaviour (copied from MSVC6 MSDN):

If there is insufficient memory for the allocation request, by default operator new returns NULL.


That's against the standard, but it is what happens.


Thats because VC6 is broken wrt the standard, if you've got it you'll be better off downloading and installing the free VC7.1 optimising compiler and any support headers needede (might have to download and install STLPort)

Share this post


Link to post
Share on other sites
Quote:
Original post by Kippesoep
MSVC6 has the following behaviour (copied from MSVC6 MSDN):

If there is insufficient memory for the allocation request, by default operator new returns NULL.


That's against the standard, but it is what happens.

Put a breakpoint inside the constructor (or make it pop up a message box, print text to the console, beep the speaker -- anything) to make sure it is actually being called. If it is not being called, you're probably experiencing an out-of-memory condition. (Either by actually not having enough free address space, or because of memory corruption).


It is calling the constructor, I'm sure of that. Also I just found out that the fatal error occurs in a different place when it is not in the dubugger. The error happens then when it is trying to delete from a linked list.

Share this post


Link to post
Share on other sites
Phantom: we know. That's why I mentioned it's against the standard. The toolkit isn't very useful, though, if you're used to the IDE. (I know the toolkit can be made to work with the IDE, but it doesn't support source-level-debugging anymore).

Manastone: this reeks of memory corruption, which may also be throwing the debugger off. You could simply put in an "assert (spellbook != NULL);" after the "spellbook = new cSpellbook;" and be sure. However, you'll likely find that the assertion won't be thrown and this is happening because of a bug elsewhere in your code, possibly having nothing to do with cSpellbook or its constructor.

Share this post


Link to post
Share on other sites
Well, thanks for all of your help. I'm going to have to do a lot of code searching to find out where I mismanaged the memory. There aren't any tools that can help me do this are there?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!