Reputation: 7594
The code below produces a memory leak when there is none. Is it safe to say that Microsoft missed the mark on this one?
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
template <typename T>
class class_leak
{
private:
T *p_;
public:
class_leak(T *p) :
p_(p)
{}
~class_leak()
{
delete p_;
}
};
int main(int /*argc*/, char * /*argv*/[]) {
int *x = new int(10);
class_leak<int> cl(x);
_CrtDumpMemoryLeaks();
}
Prodcues:
Detected memory leaks!
Dumping objects ->
{56} normal block at 0x000D1540, 4 bytes long.
Data: < > 0A 00 00 00
Object dump complete.
The program '[4584] unique_ptr.exe: Native' has exited with code 0 (0x0).
Upvotes: 0
Views: 69
Reputation: 179930
The leak detection is indeed a bit simplistic. It checks just the current balance of allocations, not what memory can still be freed. Doesn't need RAII or even C++ to demonstrate that:
int *p = malloc(10);
_CrtDumpMemoryLeaks();
free(p);
Upvotes: 2