|
[Sponsors] |
[Memory Leak] Is this a correct usage of autoPtr? |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
January 24, 2017, 12:09 |
[Memory Leak] Is this a correct usage of autoPtr?
|
#1 |
Senior Member
Kevin van As
Join Date: Sep 2014
Location: TU Delft, The Netherlands
Posts: 252
Rep Power: 20 |
If I have the following snippet in the constructor of my class:
Code:
Foam::foo::foo( const dictionary& dictName (...) ) { (...) // copy the input dictionary to ensure its existence // that is, it may go out-of-scope elsewhere and therefore seize to exist inside this class --> segmentation fault dict_ = autoPtr<dictionary>(new dictionary(dictName)); (...) } Code:
(...) autoPtr<dictionary> dict_; (...) Is there a memory leak if this class gets disposed of? Hypothesis: When this class is disposed of, the destructor of all its entities are automatically called (right?). This includes the entity "dict_". "dict_", being an autoPtr, should automatically take care of the data pointed at by the pointer it owns. It owns the pointer, because it was declared with the "new" statement inside its constructor argument. |
|
January 25, 2017, 07:33 |
Solved
|
#2 |
Senior Member
Kevin van As
Join Date: Sep 2014
Location: TU Delft, The Netherlands
Posts: 252
Rep Power: 20 |
Solved: Above hypothesis was correct.
I designed a way to test this, by writing a small new class: Code:
#include "dictionary.H" namespace Foam { class kvadictionary : public dictionary { public: kvadictionary(const dictionary& dict) : dictionary(dict) { Info << "kvadictionary constructor" << endl; } ~kvadictionary(){ Info << "kvadictionary destructor" << endl; } }; } Code:
dict_ = autoPtr<dictionary>(new kvadictionary(dictName)); Code:
foo destructor called kvadictionary destructor |
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Moving mesh | Niklas Wikstrom (Wikstrom) | OpenFOAM Running, Solving & CFD | 122 | June 15, 2014 06:20 |