c++ - Will this result in a memory leak? -
my c++ pretty rusty started using hobby project got "level up"-again..
#include "stdafx.h" #include "stdlib.h" class { public: void call() { printf("call called\n"); } }; class b { public: b() { this->pointer = new a; } void call() { this->pointer->call(); } private: a* pointer; }; int _tmain(int argc, _tchar* argv[]) { b t; t.call(); system("pause"); return 0; }
will result in memory leak? , how can delete pointers if program decides not need them anymore?
would "delete t" enough or produces memory leak too?
pointer
in b
allocated never deleted. you'll want define destructor b
deletes a
, otherwise leak a
pointed pointer
every time b
goes out of scope:
b::~b() { delete pointer; }
Comments
Post a Comment