CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Programming & Development (
-   -   PtrList of surfaceScalarFields -> destructor problem? (

dohnie October 26, 2011 08:51

PtrList of surfaceScalarFields -> destructor problem?
Hello everybody,
I have a class that contains several surfaceScalarFields which I want to loop over.
Therefore I set up a PtrList in the constructor:

myPtrList_(10);  // is a PtrList<surfaceScalarField>

myPtrList_.set(0, &FieldA_); // pointer 0 should point to surfaceScalarField FieldA_
myPtrList_.set(1, &FieldB_);
// and so on...
myPtrList_.set(9, &FieldJ_);

This throws an error at the end of the run:

End of run.
*** glibc detected *** lookupRiemannFoam: free(): invalid pointer: 0x00007fff5d62f9c8 ***
======= Backtrace: =========
======= Memory map: ========
00400000-004da000 r-xp 00000000 00:15 384262308

Invalid pointer? Actually I thought the PtrList had its own destructor that would take care of everything. If not, what does my constructor have to do?


    for (label i=9; i>=0; i--)
        delete[] myPtrList_(i);

does not work.

deepsterblue October 26, 2011 09:12

Some of your fields (FieldA_, etc) might've gone out of scope and destroyed themselves, in which case, you'd have an invalid pointer that PtrList is trying to free.

Your description lacks context - it's hard to judge where the problem is, because it's definitely not in the lines that you describe.

dohnie October 28, 2011 07:46

Thank you,
I have not found it yet, but I keep on searching....

cliffoi October 28, 2011 17:16

It does look like your surfaceScalarFields are going out of scope somewhere.
Important: If you are looking to reference fields from a PtrList, you need to be careful since PtrList automatically destructs the objects you assign to it with the ::set member function. Rather use UPtrList in your case since it will not destruct your fields when it goes out of scope.

All times are GMT -4. The time now is 10:16.