LLNL / libROM

Model reduction library with an emphasis on large scale parallelism and linear subspace methods
https://www.librom.net
Other
198 stars 36 forks source link

Basis reader scope issue #215

Closed JacobLotz closed 1 year ago

JacobLotz commented 1 year ago

Problem In my code I want to read several basis and modify them in a certain way. I want to do this in a structured manner to be sure that I do the same for every basis. I have created the function ReadBasis for this. Unfortunately if I read a basis using this function, I get a seg fault uppon using the basis outside the function. This might be scope related. I have created an example script below.

The returned pointer to a basis in Matrix* BasisReader::getSpatialBasis(double time) is created in the function itself.

Matrix*
BasisReader::getSpatialBasis(
    double time)
{
...
    Matrix* spatial_basis_vectors = new Matrix(num_rows, num_cols, true);
    sprintf(tmp, "spatial_basis_%06d", i);
    d_database->getDoubleArray(tmp,
                               &spatial_basis_vectors->item(0, 0),
                               num_rows*num_cols);
    return spatial_basis_vectors;
}

I think that moving out of the function ReadBasis makes you move out of scope of this pointer and therefore it does not exist outside ReadBasis, but I am not sure about this.

Possible solutions 1) Make the pointer Matrix* spatial_basis_vectors a member variable of BasisReader. 2) Use the pointer Matrix* spatial_basis_vectors as an input of the function such that Matrix* BasisReader::getSpatialBasis( double time, Matrix* spatial_basis_vectors) 3) Others?

Do you agree that this is undesired behavior? If so, is there a preference for the solution?

Minimal example

void ReadBasis(CAROM::BasisReader *reader, const CAROM::Matrix* basis)
{
   basis = reader->getSpatialBasis(0.0);

   // This works
   printf("Basis dim. %d x %d\n", basis->numRows(), basis->numColumns());

   // This works
   basis->print("print_basis_in_scope");
}

int main()
{
      const CAROM::Matrix* V; // Spatial basis 
      CAROM::BasisReader *reader;

      reader = new CAROM::BasisReader(basisName);

      ReadBasis(reader, V);

      // This has gives a seg fault
      V->print("print_basis_out_scope");

      delete reader;
      delete V;
}
JacobLotz commented 1 year ago

My bad, using a reference to a pointer fixes this issue... void ReadBasis(CAROM::BasisReader *reader, const CAROM::Matrix* &basis)