I haven't used a std::unique_ptr
before, so this is kind of my first attempt to trying to use it in recursion call as following:
#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <memory>
struct S {
S(int X = 0, int Y = 0):x(X), y(Y) {}
int x;
int y;
std::unique_ptr<S> p;
};
void Bar(int i, std::unique_ptr<S> &sp)
{
i--;
sp->p = std::unique_ptr<S>(new S(i, 0));
if (i > 0)
Bar(i, sp->p);
}
int main()
{
std::unique_ptr<S> b (new S());
Bar(5, b);
// Detects memory leaks
_CrtDumpMemoryLeaks();
}
At the end of program. I find that whatever memory I allocated wasn't freed according to _CrtDumpMemoryLeaks();
in Visual C++ 2012 x86 running on Windows 8 x64.