• C

0xC0000005:Access Violation error when using malloc.

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.
AbhimjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
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
AbhimjAuthor Commented:
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
sunnycoderCommented:
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
Get Certified for a Job in Cybersecurity

Want an exciting career in an emerging field? Earn your MS in Cybersecurity and get certified in ethical hacking or computer forensic investigation. WGU’s MSCSIA degree program was designed to meet the most recent U.S. Department of Homeland Security (DHS) and NSA guidelines.  

AbhimjAuthor Commented:
BTW, my code is calling realloc() multiple times before this particular code. Could this be the culprit?
0
sunnycoderCommented:
>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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kent OlsenData Warehouse Architect / DBACommented:

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
AbhimjAuthor Commented:
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
sunnycoderCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.