none
HeapFree() and HeapAlloc() RRS feed

  • Question

  • Given the following code :

    HeapFree(hHeap, 0, lpBlock1);

    lpBlock2 = HeapAlloc(hHeap, 0, BLOCKSIZE);

    where BLOCKSIZE is equal to the size of lpBlock1 freed on hHeap.

    Question:  Is it possible to asume that the allocation for lpBlock2 will always be successful ?

     

    Tuesday, November 30, 2010 12:18 PM

Answers

  • My question is very simple, assuming there is just one thread in the program :

    I want to make sure, if I destroy a block in a heap (process heap, or a private heap) and immediately create another block with the same size as the block destroyed, can I assume the allocation will be successful ?

    No you cannot. The physical memory backing your heap can be allocated to someone else, and depending on how greedy other processes are, there may not be enough physical memory or page file to accommodate your new request for memory.
    Microsoft Test - http://tester.poleyland.com/
    • Marked as answer by Ayrosa Tuesday, November 30, 2010 9:50 PM
    Tuesday, November 30, 2010 9:19 PM

All replies

  • No. certainly not unless you have a private heap per thread.
    Tuesday, November 30, 2010 12:27 PM
  • It's just one thread. Why can't hHeap be the process heap ?
    Tuesday, November 30, 2010 4:17 PM
  • In general never assume that an allocation will succeed. In between your free and your allocation, some other app can come along and eat up all the memory.
    Microsoft Test - http://tester.poleyland.com/
    Tuesday, November 30, 2010 4:28 PM
  • But hHeap is the allocation process heap obtained by GetProcessHeap(), or it's a private heap obtained by HeapCreate() , which means no other application has access to hHeap !!!

    Tuesday, November 30, 2010 4:45 PM
  • Just because you didn't create any worker threads, doesn't mean no one else did.

     

    Tuesday, November 30, 2010 8:17 PM
  • What do you mean by "doesn't mean no one else did" ?

    The code has just one thread of execution. There is no other thread and there will never exist another thread !

     

    My question is very simple, assuming there is just one thread in the program :

    I want to make sure, if I destroy a block in a heap (process heap, or a private heap) and immediately create another block with the same size as the block destroyed, can I assume the allocation will be successful ?

     

     

    Tuesday, November 30, 2010 8:59 PM
  • My question is very simple, assuming there is just one thread in the program :

    I want to make sure, if I destroy a block in a heap (process heap, or a private heap) and immediately create another block with the same size as the block destroyed, can I assume the allocation will be successful ?

    No you cannot. The physical memory backing your heap can be allocated to someone else, and depending on how greedy other processes are, there may not be enough physical memory or page file to accommodate your new request for memory.
    Microsoft Test - http://tester.poleyland.com/
    • Marked as answer by Ayrosa Tuesday, November 30, 2010 9:50 PM
    Tuesday, November 30, 2010 9:19 PM
  • Good point ! Thanks a lot.
    Tuesday, November 30, 2010 9:51 PM