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/)
-   -   Seg Fault using DynamicList (https://www.cfd-online.com/Forums/openfoam-programming-development/229229-seg-fault-using-dynamiclist.html)

Gerry Kan July 31, 2020 10:12

Seg Fault using DynamicList
 
Howdy Foamers:

I have a very simple OF application using OF 7.

Code:

#include "fvCFD.H"
#include "uniformDimensionedFields.H"
#include "turbulentFluidThermoModel.H"
#include "rhoReactionThermo.H"
#include "ReactionModel.H"
#include "multivariateScheme.H"
#include "pimpleControl.H"
#include "pressureControl.H"
#include "fvOptions.H"
#include "localEulerDdtScheme.H"
#include "fvcSmooth.H"

DynamicList<Foam::word> wordDynList(0);

int main ( int argc, char *argv[] )  {
    wordDynList.append ( "one" );
    wordDynList.append ( "two" );
    Info << wordDynList << endl;
}

The code compiled but at the end of execution it gave me a seg fault:

Code:

2
(
one
two
)

Segmentation fault

Looking at test suite (Test-DynamicList.C) I could not do anything obvious that would cause the code to crash, nor did I see any any dynamic list being explicitly deallocated (not that it solved the problem). What else did I miss?

Thanks in advance, Gerry.

Gerry Kan July 31, 2020 10:29

It looks like the culprit is in the .append() function. I don't know why, though.

Gerry Kan July 31, 2020 10:47

Okay, it turns out I needed to deallocate the DynamicList afterall ...

Code:

#include "fvCFD.H"
#include "uniformDimensionedFields.H"
#include "turbulentFluidThermoModel.H"
#include "rhoReactionThermo.H"
#include "ReactionModel.H"
#include "multivariateScheme.H"
#include "pimpleControl.H"
#include "pressureControl.H"
#include "fvOptions.H"
#include "localEulerDdtScheme.H"
#include "fvcSmooth.H"

DynamicList<Foam::word> wordDynList(0);

int main ( int argc, char *argv[] )  {
    wordDynList.append ( "one" );
    wordDynList.append ( "two" );
    Info << wordDynList << endl;
    wordDynList.clearStorage();
}

Note that calling DynamicList::clear() is not enough, DynamicList::clearStorage() resets the list size to zero and free its current contents.

Gerry.


All times are GMT -4. The time now is 14:43.