Skip to content

Commit

Permalink
Implement placement operator delete
Browse files Browse the repository at this point in the history
"If the object is being created as part of a new expression, and an exception
is thrown, the object’s memory is deallocated by calling the appropriate
deallocation function. If the object is being created with a placement new
operator, the corresponding placement delete operator is called—that is, the
delete function that takes the same additional parameters as the placement new
operator. If no matching placement delete is found, no deallocation takes
place."

So to avoid memory leaks, we need to implement placement delete operators that
correspond to our placement new, which we use for leak detection (ironically)
in debug builds.
  • Loading branch information
qris committed Nov 11, 2017
1 parent bde5cba commit 7ccb056
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/common/DebugMemLeakFinder.cpp
Expand Up @@ -754,4 +754,15 @@ void operator delete(void *ptr) throw ()
internal_delete(ptr);
}

void operator delete(void *ptr, const char *file, int line)
{
internal_delete(ptr);
}

void operator delete[](void *ptr, const char *file, int line)
{
internal_delete(ptr);
}


#endif // BOX_MEMORY_LEAK_TESTING
8 changes: 8 additions & 0 deletions lib/common/MemLeakFinder.h
Expand Up @@ -53,6 +53,14 @@ void memleakfinder_notaleak(void *ptr);

void *operator new (size_t size, const char *file, int line);
void *operator new[](size_t size, const char *file, int line);
// "If the object is being created as part of a new expression, and an exception is thrown, the
// object’s memory is deallocated by calling the appropriate deallocation function. If the object
// is being created with a placement new operator, the corresponding placement delete operator is
// called—that is, the delete function that takes the same additional parameters as the placement
// new operator. If no matching placement delete is found, no deallocation takes place."
// So to avoid memory leaks, we need to implement placement delete operators too.
void operator delete (void *ptr, const char *file, int line);
void operator delete[](void *ptr, const char *file, int line);

// Define the malloc functions now, if required. These should match the definitions
// in MemLeakFindOn.h.
Expand Down

0 comments on commit 7ccb056

Please sign in to comment.