Sign in to follow this  
ManaStone

What can cause memory allocation to fail?

Recommended Posts

ManaStone    148
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
MaulingMonkey    1728
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
ManaStone    148
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
Kippesoep    892
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
_the_phantom_    11250
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
ManaStone    148
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
Kippesoep    892
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
ManaStone    148
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
Guest Anonymous Poster   
Guest Anonymous Poster
As others have allready ponited out, VC6 means you have #4 (nonstandard new behavior) - but you've probably got #5 as well (memory corruption issues).

And there are tools for this kind of error - I don't have firsthand experience with it, but you might look into Electric Fence.

Personally, I'd compile the code under linux and use Valgrind to debug this sort of thing, simply because it's a freakin' badass multi-tool that can do much more than just over/underrun checking - but that's just me.

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by ManaStone
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?


The best tool for this is to adapt policies that make it harder to mess up in the first place, really.

- KISS
- Prefer standard library containers and similar things that wrap dynamic memory allocations (auto_ptr, smart pointers etc. as needed)
- Use constants appropriately for array dimensions etc., and be consistent about how you iterate over arrays
- And KIESS (keep it even simpler, stupid).

:)

Share this post


Link to post
Share on other sites
ManaStone    148
OK I "fixed" the problem. There was a structure that I saved and loaded that had a pointer in it that was never used. I deleted the pointer and now it APPEARS to work fine.

Edit: yeah, I just swept the problem under the rug. I added a test array to the class and it is acting up again.

Share this post


Link to post
Share on other sites
ManaStone    148
OK, now I’ve found another very weird thing. changing float to GLfloat (both are the same size on my compiler) on a structure determines whether it will run properly. Anyone able to explain this one?

Share this post


Link to post
Share on other sites
Dizzy_exe    160
regarding new returning NULL in Visual Studio > 7.0:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_the_new_and_delete_operators.asp

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this