none
HeapFree RRS feed

  • Question

  • I have a struct like this:

    typedef struct _charstruct { char c; struct _charstruct * next; } * charstructp;

    I use HeapCreate(0,0,0).

    Next I build a method to create a linked list by using HeapAlloc:

    charstructp p;

    p=(charstructp) HeapAlloc(heapHandle,0,sizeof(charstructp));

    And a method to Delete the linked list:

    charstructp pre;
    while(this->pFirst!=NULL)
    {
    	cout<<this->pFirst->c;
    	pre=this->pFirst;
    	this->pFirst=this->pFirst->next;
    	HeapFree(heapHandle,1,(LPVOID)pre);
    };

    So, I got this:

    [My File Name] has triggered a breakpoint.

    It stop at the line: HeapFree(heapHandle,1,(LPVOID)pre);

    Am I wrong about somethings. Help me. Thanks.


    • Edited by nghlong93 Sunday, November 11, 2012 12:11 PM
    Sunday, November 11, 2012 12:10 PM

Answers

  • I think that your allocations should be changed:

        p = (charstructp) HeapAlloc( heapHandle, 0, sizeof(_charstruct) );


    • Edited by Viorel_MVP Monday, November 12, 2012 6:52 AM
    • Marked as answer by nghlong93 Monday, November 12, 2012 8:31 AM
    Monday, November 12, 2012 6:51 AM

All replies

  • Your output window should explain a little more about why the breakpoint was triggered.  Next, I would use the debugger to look at the value of heapHandle and pre.  Are they both values you might expect? (non-null, valid pointer etc).

    Note that you do not check the results of HeapAlloc for error codes.  I cannot see if you check HeapCreate for error codes.  What if one is returning an error?

    If that still works right and you are allocating a sane number of list nodes, you might print out the value of each call to HeapAlloc and match them with the values you are passing to HeapFree.

    Sunday, November 11, 2012 2:17 PM
  • Thanks for your reply

    Everythings is OK.

    Just that code line is problem.

    I've captured the screen. If you have time, just take a look and help me.

    www.mediafire.com/view/?36jwwkyhb9d5k1x


    • Edited by nghlong93 Sunday, November 11, 2012 3:31 PM
    Sunday, November 11, 2012 3:30 PM
  • Does your call stack show that the code execution point is inside some DLL that isn't yours?

    I am guessing that somewhere buried in HeapFree, is a assert/debug break/int 3 which is causing Visual Studio to break at that line.  Because the heap free call is the last code on the call stack for which it has source it is showing that line of code (note the stylized arrow in the margin -- that indicates that it is a call return location, not the actual execution point).

    Almost certainly the cause is that you passed bad data to HeapFree.  However I don't have access to your debugger, so I cannot say exactly what is wrong with what you passed.  That is why my suggestions in the last post were geared to figuring out what you are passing to the function and why it is bad.

    Sunday, November 11, 2012 4:04 PM
  • I try to debug many times, but realize nothing.

    This's my source.

    Please take a look:

    http://www.mediafire.com/?nek1ba3fffco99l

    Monday, November 12, 2012 12:42 AM
  • I think that your allocations should be changed:

        p = (charstructp) HeapAlloc( heapHandle, 0, sizeof(_charstruct) );


    • Edited by Viorel_MVP Monday, November 12, 2012 6:52 AM
    • Marked as answer by nghlong93 Monday, November 12, 2012 8:31 AM
    Monday, November 12, 2012 6:51 AM
  • That's it. Thank you guys.

    That's my fault when use Intellisense without check up.

    I've read the code many times, but can't realize it.

    • Edited by nghlong93 Monday, November 12, 2012 8:33 AM
    Monday, November 12, 2012 8:31 AM