Solved

0xC0000005:Access Violation error when using malloc.

Posted on 2003-11-06
9
1,818 Views
Last Modified: 2011-09-20
Hi,
I am experiencing this strange problem which I have never come across before. Please see the code snippet below:

Pixel=(PIXEL *)malloc(sizeof(PIXEL));

PIXEL is a strucutre which al has a pointer pointing to itself.
The problem I am facing is I am getting an access violation error when executing this statement. I traced the problem to the function __sbh_alloc_block (int intSize) line number 621 in the file sbheap.c Please see the statement below :
        //  unlink entry from list
        pEntry->pEntryPrev->pEntryNext = pEntry->pEntryNext;

I really dont know whats causing this errror. Even malloc() is not returning a NULL value so I think its not really related to insufficient memory. Looks like the program is trying to allocate a corrupted memory block, but I cant figure exactly what the problem is. Also, just before the call to malloc, I am incrementing a long int variable. Everytime the value is 13. I have already wasted 2 days over this problem. It will be really helpful for me if anybody can throw some light on this. You can also send comments/suggestion/solution on abhimj@lycos.com
Thanks and best regards.
0
Comment
Question by:Abhimj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9693412
problem is that you are trying to access memory whihc has not been allocated to you...
it is not possible to pin point the reason until you post the type as well as details (struct) of pEntry and its intialization

most likely cause is that you have some junk value in pEntry or pEntryPrev or pEntryNext
0
 

Author Comment

by:Abhimj
ID: 9693472
Well, sunnycoder, heres the declaration :

typedef struct PIXEL_{
      INT32 col;
      INT32 row;

      struct PIXEL_ *pPixel;
}PIXEL;

>>problem is that you are trying to access memory whihc has not been allocated to you...
I am not trying to access the memory loacation without first allocating it. malloc() is not returning.

>>most likely cause is that you have some junk value in pEntry or pEntryPrev or pEntryNext
Thats right, pEntry has value 0x016fe00c whereas pEntryPrev and  pEntryNext both have value 0.
But I dont get the reason behind it.

The troublesome statement is inside a while loop. And it is executing exacly for 12 times before giving this error. Why do you think its working fine for first 12 iterations and failing on the 13th?
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9693484
may be it is overstepping some array boundary ... and if its a linked list (looks doubly linked to me), may be you did not maintain NULL in the last node or something similar

It is very hard to pin point unless I get to see the code


>>>post the type as well as details (struct) of pEntry and its intialization

also post the code leading upto the loop and the loop itself
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Abhimj
ID: 9693494
BTW, my code is calling realloc() multiple times before this particular code. Could this be the culprit?
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 125 total points
ID: 9693503
>BTW, my code is calling realloc() multiple times before this particular code. Could this be the culprit?
anything could be... but from what you said in the last post, I am more inclined towards believing that either you messed up the loop or you messed up initializing your list
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 9693808

Hi Abhimj,

I think that Sunnycoder is right -- you're corrupting the heap.


Two things to incorporate:

After you malloc() a PIXEL, make sure that pPixel is set to NULL so that any garbage that was in the heap won't be interpretted as a pointer.

After:  Pixel=(PIXEL *)malloc(sizeof(PIXEL));

Pixel->pPixel = NULL;

or

memset (Pixel, 0, sizeof (PIXEL));


You're already testing that malloc() returns a non-NULL value.  You should also test the value returned by realloc().  If malloc() fails to move the block it returns NULL but leaves the block alone so at best you'll "lose" the item that you tried to move.  At worst, the NULL pointer will be deferenced and your program will target random data.

Ptr = realloc (Ptr);    /*  Bad thing to do!  */

Ptr1 = realloc (Ptr);  /*  Better thing to do  */
if (Ptr1)
  Ptr = Ptr1;


Kent
0
 

Author Comment

by:Abhimj
ID: 9706046
Hi all,
I finally found out the problem. sunnycoder, Kent, thanks for the clues.you were right, the was actually overstepping an array boundary and thus corrupting the heap. Thanks for the link KurtVon. I had already seen the page. That wasnt the problem. Because I even tried not using realloc(). But that didnt help. Anyways, thanks to all once again.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9706210
Hi Abhimj,

Thanks for the points but your choice of grade suprises me... C for a correct estimation of fault without looking at the code !!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question