Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

memory exceptions 1

Status
Not open for further replies.

peterworth

Programmer
Aug 18, 2004
80
GB
for what reasons might a CMemoryException be thrown at a line like this?

*lpBuffer = new char[dwCurBufSize];

not much memory is being used and i'm sure there are no memory leaks, so surely it can't only mean memory has run out?
 
CMemoryException? Shouldn't new throw a std::bad_alloc exception?

I heard that Microsoft's malloc() function may corrupt memory if you pass in an array size that is too large because it will allocate the memory as [arraySize * sizeof(yourVar)]. This may wrap around and allocate something like 500MB instead of 4.5GB, but then it will call constructors for the whole 4.5GB...
 
I suppose, you wanted to do
lpBuffer = new char[dwCurBufSize];

In your case lpBuffer contains garbage, probably NULL, so *lpBuffer throws an exception of course.
 
no, lpbuffer is a pointer to an array - char** basically. this line of code works perfectly well most of the time, but occasionally it throws this CMemoryException..
 
I heard that Microsoft's malloc() function may corrupt memory if you pass in an array size that is too large because it will allocate the memory as [arraySize * sizeof(yourVar)]. This may wrap around and allocate something like 500MB instead of 4.5GB, but then it will call constructors for the whole 4.5GB...

if this is the case, what can i do about it?
 
if it is a char**, try doing

lpBuff[0] = new char[dwCurBufSize];

The error might be more descriptive. Also, put a try catch block arround it, break in the catch and follow the call stack backwards to make sure that the buffer being passed is indeed a 2d array.

Matt
 
thanks for your help, why does using the array notation give more descriptive errors? it's not really a 2d array - just a pointer to a 1d array (and that is definately what is passed), so would the array notation work?

as i say, it works most of the time, it just arbitrarily throws the exception when dwCurBufSize is 9110. and this is not the largest size that it takes - it works with 48000 and 8544 previous to the exception.
 
If it's a char**, then did you first do this
Code:
lpBuff = new char*[some_number];

Before trying to do
Code:
lpBuff[0] = new char[dwCurBufSize];

> not much memory is being used and i'm sure there are no memory leaks
It's beginning to sound like a classic memory corruption problem. At some point in the past, you overran a buffer somewhere. So when your allocator next tries to use the memory you should not have touched, it throws these unexpected exceptions.

--
 
by stepping into the new function i've found out that the problem is at the call to HeapAlloc - it returns 0 for this 9110 sized allocation but it returns an address for the other calls which actually work.

why might it return 0? maybe memory is too fragmented at this point? if so what can i do about that?

i'm completely stuck here, any help would be much appreciated.
 
>you overran a buffer somewhere.

ah, i hope this is the case, i'll check the rest of the code now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top