-1

I am trying to overload the operator delete. The signature of this delete is as follows. Now the memory p is basically a heap spot that was created using HeapCreate/HeapAlloc

void operator delete(void* p)
{
   HANDLE hndl =//How to get handle of p ?
   HeapDestroy(hndl); 
}

Is there a better way that I can approach this ? Will I need to keep a track of the handle every time I use HeapCreate so I can use it in HeapDestroy

MistyD
  • 16,373
  • 40
  • 138
  • 240
  • 1
    How about `HANDLE hndl = HeapCreate(0, 0, 0);`? Here is a MSDN example on [Enumerating Heap](https://msdn.microsoft.com/en-us/library/windows/desktop/ee175819(v=vs.85).aspx). That being said I doubt it will be useful as-is. – Ron Feb 14 '18 at 19:41
  • i would like to get the handle to p – MistyD Feb 14 '18 at 19:44
  • Seems like a pretty reasonable question why the downvotes ? – MistyD Feb 14 '18 at 19:45
  • @MistyD Why not use a [smart pointer with a custom deleter](https://stackoverflow.com/questions/19053351/how-do-i-use-a-custom-deleter-with-a-stdunique-ptr-member). Then there is no need to remember the handle. – PaulMcKenzie Feb 14 '18 at 19:45
  • @MistyD I didn't downvote so I wouldn't know. It could be that what you are trying to achieve is somewhat questionable. WinAPI != C++. – Ron Feb 14 '18 at 19:46
  • I do not understand where you are coming from. You should know your own heap, shouldn't you? – SergeyA Feb 14 '18 at 19:46
  • yes @SergeyA initially i did not overload new or delete operator now i have overloaded the new operator and now i would like to overload the delete operator. – MistyD Feb 14 '18 at 19:48
  • @MistyD with your overloaded operator `new`, how do you know which heap to allocate from? – SergeyA Feb 14 '18 at 19:52
  • @sergey let me add how my new works – MistyD Feb 14 '18 at 19:53
  • You should keep track of the `HANDLE` and use `HeapAlloc` to get chunks of the heap as memory. See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa366599(v=vs.85).aspx – Richard Critten Feb 14 '18 at 19:53
  • @RichardCritten yeah that would be my last option – MistyD Feb 14 '18 at 19:55
  • 4
    @MistyD You are creating new heap for every allocation???? This is terrible!!! – SergeyA Feb 14 '18 at 19:55
  • @SergeyA are you suggesting use HeapCreate only when HeapAlloc is out heapSpace ? – MistyD Feb 14 '18 at 19:56
  • @MistyD from my link above _"...The `HeapCreate` function creates a private heap object from which the calling process can allocate memory __blocks__ by using the `HeapAlloc` function. ..."_ So 1 heap giving you lots of blocks. – Richard Critten Feb 14 '18 at 19:57
  • 1
    @MistyD from what I learned so far, there is no need for you to even think about the `HeapCreate` and friends, or override new. Use library-provided `new` and `delete` and keep cool. – SergeyA Feb 14 '18 at 20:20
  • What is the motivation for this? You are using Visual Studio, and Visual Studio's C++ Library implementation does exactly what you are trying to accomplish already. `operator new` and `operator delete` call into `malloc` and `free`, where the latter call `HeapAlloc` and `HeapFree`, respectively. What are you trying to accomplish by essentially re-inventing the wheel? – IInspectable Feb 15 '18 at 09:29

1 Answers1

4

The normal use of Heap functions is to use HeapCreate when your app starts up, and to store the HANDLE in a global place.

Or just use the process default heap by using GetProcessHeap()

HeapAlloc and HeapFree are used to actually allocate and deallocate memory from that heap.

And, finally, assuming you created a heap and arn't using the process heap, HeapDestroy on app shutdown to delete the whole heap and release any remaining leaked allocations.


As the process heap that exists by default is unlimited, there is no real benefit to creating your own heap over just using the GetProcessHeap heap.

If you have some specific allocation intensive task, then you could use a user heap configured for no serialization, and/or low fragmentation by overriding new and delete operators for that specific class (rather than generally).

The possibilities are, well, the opposite of endless.

Chris Becke
  • 34,244
  • 12
  • 79
  • 148
  • Thanks for explaining that Chris . What if HeapAlloc does not have anymore memory to allocate will I have to call HeapCreate again ? – MistyD Feb 14 '18 at 20:02
  • 2
    @MistyD from the link _"... If `dwMaximumSize` is 0, the heap can grow in size. The heap's size is limited only by the available memory...."_ – Richard Critten Feb 14 '18 at 20:03
  • Ah that makes sense – MistyD Feb 14 '18 at 20:04
  • 2
    Unless you limit the heap, the heaps created will grow to consume "all available memory". So if HeapAlloc fails, the process will probably have so little memory that you could not allocate an additional heap anyway. – Chris Becke Feb 14 '18 at 20:04
  • Thanks for mentioning that. I fixed my new method and Ill remove my old code. Also I think ill probably have to keep a track of the handles probably in a map – MistyD Feb 14 '18 at 20:12
  • You don't need to do any of this. If HeapAlloc fails with out of memory, HeapCreate won't save you. You'll get out of memory too with the new heap. You should remove all of this code and use the standard allocator. – David Heffernan Feb 15 '18 at 08:30
  • *"a user heap configured for no serialization, and/or low fragmentation"* - An LFH and a non-serialized heap are mutually exclusive. You cannot create an LFH that doesn't serialize access, as [documented](https://msdn.microsoft.com/en-us/library/windows/desktop/aa366599.aspx). – IInspectable Feb 15 '18 at 09:53