This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

DirichletBC Crashing: Bug or feature?

+1 vote

I have asked before about a possible bug in my code, it turns out the problem is quite puzzling. I am running Fenics 1.5, from the DMG.

A minimal running example is available here

Let's say I need to create a DirichletBC, I could do as follows:

  bcs.push_back(new dolfin::DirichletBC(*V,
                    dolfin::Constant(0.0, 0.0),
                    edgedetect(dolfin::Point(0.0, 0.0), dolfin::Point(7.0, 0.0))));
  bcs.push_back(new dolfin::DirichletBC(*V,
                    dolfin::Constant(1.0, 16.0),
                    edgedetect(dolfin::Point(0.0, 16.0), dolfin::Point(7.0, 16.0))));

These lines use an temporary variable, if I run this code, when calling dolfin::assemble_system I get an exception and the program dies.

But if I use a variable with automatic storage, then the assembly call works perfectly and everybody's happy:

dolfin::Constant clamped(0.0, 0.0);
bcs.push_back(new dolfin::DirichletBC(*V,
                  clamped,
                  edgedetect(dolfin::Point(0.0, 0.0), dolfin::Point(7.0, 0.0))));
bcs.push_back(new dolfin::DirichletBC(*V,
                  clamped,
                  edgedetect(dolfin::Point(0.0, 16.0), dolfin::Point(7.0, 16.0))));

The happiness continues if I use a shared pointer like std::make_shared<const dolfin::Constant>(0.0, 0.0) in lieu of clamped.

It seems that the constructor that is called with a temporary won't really use the temporary, making a copy if needed, but instead it will hold the pointer to the location, and of course, it goes FUBAR afterwards.

The source code I posted will compile to a single executable. If you call it without parameters then you will run the code with variables, if you execute it with some arguments it will run with temporaries.

Am I missing something here?

Thanks!

asked Mar 21, 2016 by senseiwa FEniCS User (2,620 points)
edited Mar 22, 2016 by senseiwa
...