CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

Leaving field in an undefined state when passing thermo.mu() to lvalue

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   September 16, 2020, 04:21
Default Leaving field in an undefined state when passing thermo.mu() to lvalue
  #1
Member
 
Join Date: Dec 2018
Location: Darmstadt, Germany
Posts: 87
Rep Power: 7
raumpolizei is on a distinguished road
Hey everyone,
I found something interesting that I wanted to discuss before reporting it as a bug (?). In the thermophysicalModels library, more precisely in the psiThermo class, there is the following function:
Code:
Foam::tmp<Foam::volScalarField> Foam::psiThermo::mu() const
{
    return mu_; // mu_ is psiThermo member
}
I was wondering why the shared Foam::tmp is used here. The combination of the function with the GeometricField:perator= makes it possible to steal mu from the class and I don't think that this is what was intended. So basically, I cannot assign an lvalue to thermo.mu() because after the assignment the mu_ is now in an undefined state. This can be tested like this:
Code:
// thermo is of type psiThermo
 //muSteal has been initialzed
muSteal = thermo.mu();
Info << "muSteal[0] = " << muSteal[0] << nl;
Info << "thermo.mu()()[0] = "<<thermo.mu()()[0] << nl;
Note that using thermo.mu() as temporary is fine, like this because now we are taking the field from the temporary:
Code:
 //muCopy has been initialzed
 muCopy= dimensionedScalar("zero",dimless,1.)*thermo.mu();
Info << "muCopy[0] = " << muCopy[0] << nl;
Info << "thermo.mu()()[0] = "<<thermo.mu()()[0] << nl;
So what do you think about this guys? I would suggest returning a const reference instead to make the encapsulation more complete:
Code:
const Foam::volScalarField& Foam::psiThermo::mu() const
{
    return mu_;
}
Thanks for taking a look!
Cheers RP
raumpolizei is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Second Derivative Zero - Boundary Condition fu-ki-pa OpenFOAM 11 March 27, 2021 04:28
[OpenFOAM.com] An Error in recompiling of openFoam-4.0 alimea OpenFOAM Installation 4 April 8, 2020 14:44
can not complie fluentDataToFoam in OF2.1.1 hewei OpenFOAM Pre-Processing 20 September 8, 2018 09:19
OpenFOAM-2.1.x on fedora 16 x86_64 anand_30 OpenFOAM Installation 3 December 26, 2011 02:09
OpenFOAM on MinGW crosscompiler hosted on Linux allenzhao OpenFOAM Installation 127 January 30, 2009 19:08


All times are GMT -4. The time now is 13:36.