I was wondering which part of my code will free a dynamically allocated, but static class member when this is not needed anymore. See the following code: classPrinter
is shared among all A
-objects and created when the first instance of class A
will be created. Just to be sure: the classPrinter
-object will automatically be destructed when exiting my program, right?
a.h
class A {
static B* classPrinter;
}
a.cpp
#include "a.h"
B A::classPrinter = new B();
A::A() { ...}
Just to be sure: the
somePrinter
-object will automatically be destructed when exiting my program, right?
Since this is C++, the answer is "No." For everything allocated with new
the corresponding delete
must be called. If that doesn't happen, the object leaks. But why allocate this dynamically at all? This
class A {
static B classPrinter;
}
B A::classPrinter;
behaves like your code, except that classPrinter
will be destructed at the end of the program.
However, you write that
classPrinter
is shared among all A-objects and created when the first instance of classA
will be created.
The code in your question doesn't do this. If you want to do this, do something like this:
class A {
static std::shared_ptr<B> classPrinter;
}
std::shared_ptr<B> A::classPrinter;
A::A()
{
if(!classPrinter)
classPrinter.reset(new B());
}
The smart pointer will make sure that the object gets deleted.
No, objects created using new
are never automatically deleted. You should always provide a matching delete
to prevent memory leaks.
In this case, the simplest solution would be to have a static object, rather than a static pointer to a dynamic object - there is no reason at all for the extra level of indirection.
// header file
class A {
static B classPrinter;
};
// source file
B A::classPrinter;
or if you want to delay the construction until the object is needed (and avoid potential problems if static objects in other translation units need to access it from their constructors):
class A {
static B & classPrinter() {
static B instance;
return instance;
}
};
In other situations, when you actually need dynamic allocation, you should always manage the dynamic object using smart pointers or other RAII objects.
The destructor of the object of which A::classPrinter
points to will not be called.
If you think about it's nothing else but logic; where is the matching delete
-statement that, in normal cases, would call the dtor of the object?
The memory occupied will be flagged as free again and returned to the system, at least in all modern such.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With