keep the track of placement delete against placement new

c++

Solution

You want to pair allocation and deallocation:

- malloc / free

- new / delete (the "regular" forms)

- new[] / delete[]

But what do you pair with placement new? (Explicitly: the one that takes a void* and commonly called simply "placement new", instead of other placement forms of new.) It's not delete, but an explicit destructor call.

- `T *p = new(mem) T();` / `p->~T()`

Placement new doesn't actually allocate anything, it's just syntactic sugar for calling a constructor. You don't need to, and shouldn't, track it. It's even a bit weirder than other forms, as it's not unusual to call the "destroy" bit first, then replace the destroyed object with another (the opposite of the sequence for others):

{
  T some_object;

  some_object->~T(); // die! die! die!
  new(&some_object) T();  // didn't save the return value? memory leak..? nope.
} // some_object leaves scope and is destructed (again)

Problem

I am developing a tool like memory leak detector. I can track the placement new but how can i track the placement delete. I did a lot of R & D and i found that placement delete cant be called directly, it is called by constructor at the time of exception. So how can i keep the track of placement delete against placement new? Any help would be appreciated......

Original source