Solved

0xC0000005:Access Violation error when using malloc.

Posted on 2003-11-06
9
1,803 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
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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
 
LVL 11

Expert Comment

by:KurtVon
ID: 9704336
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

821 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