amrex icon indicating copy to clipboard operation
amrex copied to clipboard

seg fault when main_main function is not used

Open hsitaram opened this issue 5 years ago • 6 comments

I am working with the amrex/Tutorials/Particles/NeighborList example where I made a minor change. I removed the use of the main_main function in main.cpp and directly used the code from it within. See modification in my fork - https://github.com/hsitaram/amrex/blob/development/Tutorials/Particles/NeighborList/main.cpp

The case ran the prescribed number of time steps but finished with a segfault. I also used gdb to inspect where it happens -

Program received signal SIGSEGV, Segmentation fault. free (pt=, this=0xa33e80) at ../../..//Src/Base/AMReX_BaseFab.H:194 194 arena()->free(pt);

I noticed the use a similar paradigm in other examples as well where a main_main or a scope separation is used away from amrex::initialize and finalize. I am curious to know why this happening? - is this unexpected or known behavior?

hsitaram avatar Aug 10 '20 03:08 hsitaram

Yes, this is known behavior. See the last paragraph in this section of the documentation: https://amrex-codes.github.io/amrex/docs_html/Basics.html#initialize-and-finalize

maximumcats avatar Aug 10 '20 04:08 maximumcats

Maybe we could do something to make it more friendly. Instead of completely deleting the Arenas, maybe we could leave them in a state that free can still be called without error.

WeiqunZhang avatar Aug 10 '20 14:08 WeiqunZhang

Thank you very much @maxpkatz and @WeiqunZhang for your replies. Missed that bit in the documentation.

hsitaram avatar Aug 10 '20 16:08 hsitaram

Document my thoughts here. amrex::Finalize could turn on a flag in Arena. After the flag is on, Arena::free will call delete this if it's supposed to be the last one. For CArena, we keep track of all the alloc and free calls. So it's easy to know if a call to free is the last or not. Bur BArena is currently a thin wrapper without any information on the number of calls to alloc and free. We will have to add some stats to it.

Note that delete this from a member function is legal. https://isocpp.org/wiki/faq/freestore-mgmt#delete-this

WeiqunZhang avatar Aug 11 '20 16:08 WeiqunZhang

Another way to do it would just be to assert if we're calling something in Arena after we have finalized (which could be checked with a static variable).

maximumcats avatar Aug 11 '20 16:08 maximumcats

Yes, we could do that too. Probably much simpler.

WeiqunZhang avatar Aug 11 '20 16:08 WeiqunZhang