Advertisement

Memory allocation error?

Started by August 17, 2000 01:10 PM
10 comments, last by chippydip 24 years, 4 months ago
Arrgggg! This error is really bugging me and I haven''t been able to get to the bottom of it, so I decided to see if there is anyone out there who can figure out what I''m missing... When I run my program, I get the following error: The instruction at "0x77f6467a" referenced memory at "0xffffffff". The memory could not be "read". Now, I know this is a common error message usually associated with dangling pointers and the like, but when I use the debugger, I''ve found that the error is being generated from within a call to new. The exact call looks something like this: image = new byte[width*height*bpp/8]; Where byte is typedefed as unsigned char and bpp is set as 8 (so the integer rounding is not a problem here.) image is a byte* and width, height, and bpp are unsigned ints. The really wierd thing about this error is the memory address that the program is trying to read: "0xffffffff" Of course it can''t read there b/c its the end of its address space, but why is it even trying? From what I can tell in the debugger, the values passed to the new and then the various malloc functions are correct for the amount of memory I want (about 70k) At first i thought I might be out of memory, but I''m not allocating all that much... a couple megs at best... and i can confirm this with the task manager (I''m working on and NT system using VC++) Thanks for any help/ideas you can offer. I''ve been beating my head against the wall on this one for a while and would like to get on with my life!
Hi there,

Check to see if the memory has been allocated or not, and display a message accordingly.

e.g.

if ( image == NULL )
{
MessageBox(0, "Not enough memory", NULL, MB_OK);
}
else
{
MessageBox(0, "Memory has been allocated", "Information", MB_OK);
}

Hope this helps!

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..
Advertisement
Thanks for the idea, but I''m pretty sure that''s not the problem... The call to the new function never returns since the error is occuring within it, so I''m fairly certain that such a test would never even get executed

Any other ideas?
check your values for width height and bpp before calling the function. Make sure they are the correct values instead of just assuming they are. You might have messed them up somehow when you read them out of the bitmap (if that is what you are doin).


"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

Nope, that's not it either. I've checked the values in the debugger, and when I make the call, the values are correct. Also, the value passed into the new function is consistent with the width, height, and bpp that I'm using (I did the math on a calculator and got the same number...)

Here's some additional debug info if it will help anyone. The message I get when i hit cancel to debug is:

Unhandled exception in 3D Ultrasound.exe (NTDLL.DLL): 0xC0000005: Access Violation.

and the call stack is as follows:

NTDLL! 77f6467a()
_heap_alloc_base(unsigned int 72624) line 161
_heap_alloc_dbg(unsigned int 72576, int 1, const char * 0x00000000, int 0) line 367 + 9 bytes
_nh_malloc_dbg(unsigned int 72576, int 1, int 1, const char * 0x00000000, int 0) line 242 + 21 bytes
_nh_malloc(unsigned int 72576, int 1) line 194 + 19 bytes
operator new(unsigned int 72576) line 24 + 11 bytes
c_image::operator=(const c_image & {...}) line 60 + 28 bytes
main(int 1, char * * 0x00f70e80) line 133
mainCRTStartup() line 206 + 25 bytes

Thanks for the effort, though, and keep the suggestions coming!


Edited by - chippydip on August 17, 2000 4:57:54 PM
quote: Original post by chippydip

When I run my program, I get the following error:

The instruction at "0x77f6467a" referenced memory at "0xffffffff". The memory could not be "read".


On NT (win2k as well) vc the error reporting is a bit different than that of a win98 system. Basically this is a fancy 0x00000005 access violation error.

quote:
I''ve found that the error is being generated from within a call to new. The exact call looks something like this:

image = new byte[width*height*bpp/8];


Hmm, I doubt that this is causing an access violoation like you said (as long as values are correct). You get an access violation from accessing parts of image when image isn''t the size you expect. I would bet if you commented out all routines accessing "image" you wouldn''t get the access violation. I would double check all the code accessing image then.
___________________________Freeware development:ruinedsoft.com
Advertisement
Hi there!
Okay, lets me see what i can do...This line of code is probably not the problem, since what you are doing is not unlegal at all.

image = new byte[width*height*bpp/8];

Maybe you could try to replace width*height*bpp/8 with 10 or something to see what happens then. But as i said this Should be perfectly legal. This is what i mean:

instead of:
image = new byte[width*height*bpp/8];

try:
image = new byte[10];

Then you are completely sure that the calculation is not the problem.
The problem could also be earlier in your code. Maybe you are corrupting memory earlier. And for some reason, that corruption is not causing an error immediately but later on in the code. I have tried this myself a couple of times, and it''s really annoying cause the debugger reports the error one place in the code, but it might be caused somewhere else.
So try and look for memory corruption, is all i can say now.
Hope this helps...

quote: Original post by iwasbiggs

Hmm, I doubt that this is causing an access violoation like you said (as long as values are correct). You get an access violation from accessing parts of image when image isn''t the size you expect. I would bet if you commented out all routines accessing "image" you wouldn''t get the access violation. I would double check all the code accessing image then.


I know! That''s what''s really bugging me... You''d think that an access violation would be caused by actually trying to access part some part of memory, but the exception seems to be generated from within the "new" call (just look at the call stack.) It seems like the memory manager is looking for memory past the end of my program''s virtual address space and causing an exception. I have no idea why this is happening, though. Maybe I''m doing something else funny earlier in the program? I don''t know. I''ve never had this type of error before (and I''ve been programming for several years.) Usually, a memory access violation occurs at some random offset, but this error is consitently occuring at the end of the virtual address space

sigh... the trials and tribulations of a programmer

Hi there.
You are welcome to try to send me the code and i will see what i can do. (zygo_x2000@inbox.as). I am pretty sure that you are corrupting something earlier in your code.
If the code isnt too large you could post it so that everyone can take a look at it.
The project is quite large, so posting it won''t work. I did manage to fix the problem today, though! In a different part of the program I was copying an image and instead of copying byte by byte in a double loop, I used a single loop and a memcpy() to copy each line. I really don''t know why that was that was the problem (I wasn''t getting any access violations from that code and I''m 90% sure the copying addresses were correct) but oh, well... its working now!

If anyone has any idea why the memcpy was effecting the rest of the program the way it was I''d like to know. Otherwise I guess I''ll just have to assume that my copy addresses weren''t correct afterall (but I know I checked them several times! oh well...)

This topic is closed to new replies.

Advertisement