CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   PtrList of surfaceScalarFields -> destructor problem? (https://www.cfd-online.com/Forums/openfoam-programming-development/93776-ptrlist-surfacescalarfields-destructor-problem.html)

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:
Code:


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:
Code:

End of run.
*** glibc detected *** lookupRiemannFoam: free(): invalid pointer: 0x00007fff5d62f9c8 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x73286)[0x7f712a62e286]
/lib64/libc.so.6(cfree+0x6c)[0x7f712a63302c]
lookupRiemannFoam[0x451dc3]
lookupRiemannFoam[0x4413f1]
/lib64/libc.so.6(__libc_start_main+0xfd)[0x7f712a5d9b7d]
lookupRiemannFoam[0x428639]
======= 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?

Code:

myClass::~myClass()
{
    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 02:42.