CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2003
    Location
    Chennai, India
    Posts
    76

    Problem with HeapFree

    Hi,
    I am allocating memory in the Heap.

    pV = HeapAlloc(Mem, 0, ulSize);

    I am freeing the memory using HeapFree function.

    HeapFree(SesMem, 0, pV);

    But the problem is when i tried to the free of the same memory again
    HeapFree(SesMem, 0, pV);,
    i am getting a Memory Leak. Is there a way to find that this heap has been freed already.

    i.e when i try to free the heap memory that is freed already, i shold be in a able to find that this heap memory has been freed already and free should not be done now.

    Thanks in Advance,
    Varadha

  2. #2
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Your question is unclear. When you HeapAlloc(...) you HeapFree(...) when you are done manipulating the Heap. To call HeapFree(...) twice on a pointer allocated by HeapAlloc(...) is undefined behavior, as your freeing memory that may be allocated for other reasons. When you call HeapFree, set the pointer to null. If you double free in a debug build you will breakpoint out of RtlpValidateHeapEntry(...)

  3. #3
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    also show the code your using..

  4. #4
    Join Date
    Sep 2003
    Location
    Chennai, India
    Posts
    76
    HI,
    I have problem with the allocation of memory in heap using HeapAlloc function.
    HeapAlloc function not allocating the memory.

    These are the list of events that i do.
    1. Pv1 = HeapAlloc(Mem, HEAP_ZERO_MEMORY , ulSize)
    2. HeapFree(Mem,0,Pv1)
    3. Pv2 = HeapAlloc(Mem, HEAP_ZERO_MEMORY , ulSize1)
    4. HeapFree(Mem,0,Pv2)

    If i follow the above steps i am not getting Pv2 allocated..i.e for Pv2 is assigned a value of 0x00000000

    But if i follow the following steps
    1. Pv1 = HeapAlloc(Mem, HEAP_ZERO_MEMORY , ulSize)
    2. Pv2 = HeapAlloc(Mem, HEAP_ZERO_MEMORY , ulSize1)
    3. HeapFree(Mem,0,Pv2)

    by skipping HeapFree for Pv1, then the Pv2 is properly allocated memory in the Heap.

    What may the reason behind this.

    Thanks in Advance,
    Varadha

  5. #5
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Mem I assume is a handle in a CreateHeap call, we need to see that and the ulsize and ulsize1 values. Break down your code into a simple console application and you will most likely find very quick your error. There is no problem with calling HeapCreate(...) and HeapAlloc(...)/HeapFree(...) etc, unless you set dwMaximumSize to be less than you are allocating, but that makes no sense in the context your describing, that one succedes when you don't Free....which brings me to memory corruption, but I can assume again your running under a debug build and if you have corruptied the heap it should raise a breakpoint...

  6. #6
    Join Date
    Sep 2003
    Location
    Chennai, India
    Posts
    76
    Thanks Mick,
    can you suggest me the way i can find that why this error occures..
    Can you suggest some tutorial to understand the heap allocation better.. is there any tool that i can you to oversome this problem

  7. #7
    Join Date
    Sep 2003
    Location
    Chennai, India
    Posts
    76
    Hi,
    When i use HeapAlloc to allocate the memory in the heap i am getting the following error.


    Code:
    pV = HeapAlloc(SesMem, HEAP_ZERO_MEMORY , ulSize);

    Error:

    First-chance exception in App.exe (NTDLL.DLL): 0xC0000005: Access Violation.

    What may be the reason behind this error.

    Thanks in Advance,
    Varadha

  8. #8
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Originally posted by Varadha
    Hi,
    When i use HeapAlloc to allocate the memory in the heap i am getting the following error.


    Code:
    pV = HeapAlloc(SesMem, HEAP_ZERO_MEMORY , ulSize);

    Error:

    First-chance exception in App.exe (NTDLL.DLL): 0xC0000005: Access Violation.

    What may be the reason behind this error.

    Thanks in Advance,
    Varadha
    in whatever debugger of your choice, I'll assume the VC debugger, break early on in your app, if it's a MFC app then in the initinstance for example, then hit the debug->exceptions and turn on the stop always for acess violation, run your code, and post the stack.

  9. #9
    Join Date
    Sep 2003
    Location
    Chennai, India
    Posts
    76
    HI,
    do i get the access violation error if there is no enough space in the heap for allocation.

    can i know what do you mean in the words "run your code, and post the stack." in the previous post

    Regards,
    Varadha

  10. #10
    Join Date
    Sep 2003
    Location
    Chennai, India
    Posts
    76
    When the following statement is executed

    pV = HeapAlloc(SesMem, HEAP_ZERO_MEMORY , 81000);

    I had pV as 0x00000000..i.e no memory allocated in the Heap

    But when i have this as very next statement,

    pV = HeapAlloc(SesMem, HEAP_ZERO_MEMORY , 1000);

    i have memory allocated in the above statement.

    What may be the reason behind this error

  11. #11
    Join Date
    Mar 2005
    Location
    Chennai
    Posts
    1

    Re: Problem with HeapFree

    when application exit in Debug mode.
    displaying error by opening free.c file
    in the following line
    .
    .
    #endif /* _WIN64 */
    {
    HeapFree(_crtheap, 0, pBlock);
    }
    }
    .
    .
    anyone can Explain how to solve this problem
    Thanks
    -Renugopal

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured