This is a sample code of my program. Here I am dynamically allocating memory using std::auto_ptr and entering values( in function) after that I am again allocation memory for the same variable. So Do the previously allocated memory will be deallocated when allocated a new memory for the same. I am doubtful about this since I am using std::auto_ptr. Thanks in advance.
#include "stdafx.h"
#include <iostream>
#include <memory>
#include <windows.h>
std::auto_ptr<HANDLE> *eventHandle;
void function()
{
eventHandle = new std::auto_ptr<HANDLE>[5];
std::auto_ptr<HANDLE> handle(new HANDLE);
*handle = CreateEvent(NULL, false, false, NULL);
eventHandle[0] = handle;
}
void f2()
{
if(NULL == eventHandle)
{
std::cout<<" HANDLE NULL";
}
}
int _tmain(int argc, _TCHAR* argv[])
{
function();
f2();
function();
return 0;
}
Here
you have a raw pointer, so when you reassign it the previous value is just overwritten and the array of objects of type
auto_ptr
previously pointed to is leaked.