If I implement this code, is it producing a memory leak? If it is not a memory leak, when will the memory be deleted?
class MyClass
{
public:
void foo() {
static MyClass *element = new MyClass();
}
};
If I implement this code, is it producing a memory leak? If it is not a memory leak, when will the memory be deleted?
class MyClass
{
public:
void foo() {
static MyClass *element = new MyClass();
}
};
Well, a little of both.
Yes; there is no code that deletes it (the associated delete element).
No; there will only be a single instance of it and the OS will reclaim the memory when the process exits anyway. The memory usage will not grow unbounded.
Taking into account possible future maintenance and changes; I would be inclined to use a std::unique_ptr and avoid the risk of any future memory leaks.