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/)
-   -   Deletion of volScalarField object in PimpleFoam Solver (https://www.cfd-online.com/Forums/openfoam-programming-development/205059-deletion-volscalarfield-object-pimplefoam-solver.html)

m.arslansadiq August 8, 2018 11:53

Deletion of volScalarField object in PimpleFoam Solver
 
Hi,
I am rather new to OpenFOAM and currently I am working with PimpleFoam solver. For Testing purposes I have created a test field with the object named Test, besides pressure (p) and valocity (U) in createFields.H file. Now the thing is, after I finish my simulations I want to delete the test object of volScalarField. Now if I use the simple delete() function, the error which pops up, states that:

error: type ‘Foam::volScalarField {aka class Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh>}’ argument given to ‘delete’, expected pointer
delete(Test);

Any suggestions how would I take care of this memory leak?

It is not important for the task which I am trying to do, but it would be nice to know about efficient memory management in OpenFOAM.

Tobi August 9, 2018 17:01

Hi,


a normal variable such as

Code:

int myNumber;
double myDouble;
scalarField foo;

will be destroyed automatically after the validity of the variables is not valid anymore. That means, the memory allocation and deallocation is done automatically. The delete function is used with pointers such as:


Code:

double* myDouble;
A pointer does allocate memory to store the pointer but the pointer points to some memory. In order to prohibit errors, you first should initialize the pointer with NULL or direct allocate memory and tell the pointer to point to the new allocated memory.

To deallocate the memory, you can use the delete function. Again, the volScalarField is not a pointer. To make it a pointer, you have to do it in a different way such as:


Code:



volScalarField* myNewField = new volScalarField(...);


.
.
.
.


 myNewField->delete();


// Or

delete(myNewField);

The code might be wrong - its just out of the box, but somehow like that. However, it is better to use smartPointer in C++. FOAM has a pointer class called autoPtr<>.

Summing up. The problem you have is a pure C++ problem. Not related to FOAM.


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