SEGMENTATION VIOLATION when re-compiling cpp file after running

In the following I post the minimal code (minimal.cpp) able to reproduce the segmentation violation error.
Once entered root I compiled writing:
.L minimal.cpp
Then I run function:
particles_decay(500, 1., 0.01)
Now if I try to re-compile the code I get the segmentation violation with the following stacktrace:

root [3] .L minimal.cpp

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007fe27703d83a in __GI___wait4 (pid=141049, stat_loc=stat_loc
entry=0x7fff3d27d368, options=options
entry=0, usage=usage
entry=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:30
#1  0x00007fe27703d7fb in __GI___waitpid (pid=<optimized out>, stat_loc=stat_loc
entry=0x7fff3d27d368, options=options
entry=0) at waitpid.c:38
#2  0x00007fe276faf6ab in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:172
#3  0x00007fe2776b216e in TUnixSystem::StackTrace() () from /home/niccolo/root/lib/libCore.so.6.24
#4  0x00007fe2776aefe5 in TUnixSystem::DispatchSignals(ESignals) () from /home/niccolo/root/lib/libCore.so.6.24
#5  <signal handler called>
#6  0x00007fe27591d5ad in llvm::LLVMContext::removeModule(llvm::Module*) () from /home/niccolo/root/lib/libCling.so
#7  0x00007fe275956854 in llvm::Module::~Module() () from /home/niccolo/root/lib/libCling.so
#8  0x00007fe272c48d77 in cling::Transaction::~Transaction() () from /home/niccolo/root/lib/libCling.so
#9  0x00007fe272cc972c in cling::IncrementalParser::deregisterTransaction(cling::Transaction&) () from /home/niccolo/root/lib/libCling.so
#10 0x00007fe272c2d105 in cling::Interpreter::unload(cling::Transaction&) () from /home/niccolo/root/lib/libCling.so
#11 0x00007fe272c2d274 in cling::Interpreter::unload(unsigned int) () from /home/niccolo/root/lib/libCling.so
#12 0x00007fe272d21e46 in cling::MetaSema::actOnUCommand(llvm::StringRef) () from /home/niccolo/root/lib/libCling.so
#13 0x00007fe272d224f6 in cling::MetaSema::actOnLCommand(llvm::StringRef, cling::Transaction**) () from /home/niccolo/root/lib/libCling.so
#14 0x00007fe272d338be in cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) () from /home/niccolo/root/lib/libCling.so
#15 0x00007fe272d1bf69 in cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) () from /home/niccolo/root/lib/libCling.so
#16 0x00007fe272b2e5ac in HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) () from /home/niccolo/root/lib/libCling.so
#17 0x00007fe272b4601e in TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) () from /home/niccolo/root/lib/libCling.so
#18 0x00007fe277557a88 in TApplication::ProcessLine(char const*, bool, int*) () from /home/niccolo/root/lib/libCore.so.6.24
#19 0x00007fe2778d31b6 in TRint::ProcessLineNr(char const*, char const*, int*) () from /home/niccolo/root/lib/libRint.so.6.24
#20 0x00007fe2778d35ba in TRint::HandleTermInput() () from /home/niccolo/root/lib/libRint.so.6.24
#21 0x00007fe2776ae0a2 in TUnixSystem::CheckDescriptors() () from /home/niccolo/root/lib/libCore.so.6.24
#22 0x00007fe2776afd88 in TUnixSystem::DispatchOneEvent(bool) () from /home/niccolo/root/lib/libCore.so.6.24
#23 0x00007fe2775bcec9 in TSystem::Run() () from /home/niccolo/root/lib/libCore.so.6.24
#24 0x00007fe277554b53 in TApplication::Run(bool) () from /home/niccolo/root/lib/libCore.so.6.24
#25 0x00007fe2778d4c5e in TRint::Run(bool) () from /home/niccolo/root/lib/libRint.so.6.24
#26 0x000056283c09f180 in main ()
===========================================================


The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum https://root.cern.ch/forum
Only if you are really convinced it is a bug in ROOT then please submit a
report at https://root.cern.ch/bugs Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#6  0x00007fe27591d5ad in llvm::LLVMContext::removeModule(llvm::Module*) () from /home/niccolo/root/lib/libCling.so
#7  0x00007fe275956854 in llvm::Module::~Module() () from /home/niccolo/root/lib/libCling.so
#8  0x00007fe272c48d77 in cling::Transaction::~Transaction() () from /home/niccolo/root/lib/libCling.so
#9  0x00007fe272cc972c in cling::IncrementalParser::deregisterTransaction(cling::Transaction&) () from /home/niccolo/root/lib/libCling.so
#10 0x00007fe272c2d105 in cling::Interpreter::unload(cling::Transaction&) () from /home/niccolo/root/lib/libCling.so
#11 0x00007fe272c2d274 in cling::Interpreter::unload(unsigned int) () from /home/niccolo/root/lib/libCling.so
#12 0x00007fe272d21e46 in cling::MetaSema::actOnUCommand(llvm::StringRef) () from /home/niccolo/root/lib/libCling.so
#13 0x00007fe272d224f6 in cling::MetaSema::actOnLCommand(llvm::StringRef, cling::Transaction**) () from /home/niccolo/root/lib/libCling.so
#14 0x00007fe272d338be in cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) () from /home/niccolo/root/lib/libCling.so
#15 0x00007fe272d1bf69 in cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) () from /home/niccolo/root/lib/libCling.so
#16 0x00007fe272b2e5ac in HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) () from /home/niccolo/root/lib/libCling.so
==========================================================

In the following minimal.cpp - sorry but root forum policies don’t allow new users to upload links on posts directly.

# include <TRandom3.h>
# include <TH1D.h>
# include <TFile.h>
# include <TCanvas.h>
# include <TStyle.h>

# include <Riostream.h>
# include <iostream>

void particles_decay(int N0, double delta_t, double alpha, int seed = 1) {
    double t=0.;
    double T = 300.;

    int N_survived = N0;
    int N_decayed;

    int N_steps = (int)(T/delta_t);
    int step = 0;

    double r;

    TRandom3 *gRandom = new TRandom3();
    gRandom -> SetSeed(seed);


    TFile file("particles_decay.root", "recreate");

    TCanvas* f = new TCanvas("f");

    //creo oggetto TH1D per disegnare la funzione di decadimento
    TH1D* N_vs_t = new TH1D("N_vs_t", "Number of particles in time", N_steps, 0, T);

    while((t < T) && (N_survived > 0)) {

        N_vs_t -> SetBinContent(step, N_survived);

        //simulo decadimento radioattivo sulle particelle sopravvisute
        N_decayed = 0;
        for(int j=0; j<N_survived; j++){
            r = gRandom -> Rndm();
            if (r < alpha*delta_t) N_decayed++;
        }

        if (N_survived <= N_decayed) break;
        N_survived -= N_decayed;

        t += delta_t;
        step++;
    }

    gStyle -> SetOptFit();
    N_vs_t -> Fit("expo");

    N_vs_t -> DrawCopy();

    //Metto sul file root tutte le robe sovrapposte, nota che f punta al TCanvas aperto sopra, 
    //mentre il file .root con quel nome era stato anche lui creato sopra.
    f -> Print("particles_decay.root");

    file.Close();
}

ROOT Version: 6.24/02
Platform: Ubuntu 21.04
Compiler: Root default


That doesn’t compile the macro; it loads it. Once loaded you don’t need to reload it between runs, unless you are editing it; in that case, try first unloading it (.U minimal.cpp), or even quitting ROOT, to be safer :slight_smile:

1 Like

@dastudillo Thanks for replying.
The problem is that the same error occurs also if:

  1. I compile with
.L minimal.cpp+

then I run, after that I edit the file and recompile with the command above.

  1. I compile with
.L minimal.cpp+

then I run, after that I unload the file with

.U minimal.cpp
  1. I load with
.L minimal.cpp

then I run, after that I unload the file with

.U minimal.cpp

In all these cases the error occurring is the same I posted at the beginning of the discussion.

With master (i.e. close to what you have) on Linux I tried

root [0] .L minimal.C
root [1] particles_decay(500, 1., 0.01)
 FCN=51.0153 FROM MIGRAD    STATUS=CONVERGED      40 CALLS          41 TOTAL
                     EDM=4.6871e-07    STRATEGY= 1      ERROR MATRIX ACCURATE 
  EXT PARAMETER                                   STEP         FIRST   
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE 
   1  Constant     6.20101e+00   7.29140e-03   1.63914e-05  -1.00223e-01
   2  Slope       -1.01929e-02   6.74151e-05   1.51443e-07   4.24243e+00
Info in <TCanvas::SaveAs>: ROOT file particles_decay.root has been created
root [2] .L minimal.C
root.exe: /home/axel/build/root/master/src/interpreter/cling/lib/Interpreter/Interpreter.cpp:1513: void cling::Interpreter::unload(cling::Transaction&): Assertion `!T.getTopmostParent()->getNext() && "Can not revert previous transactions"' failed.

That’s also a bug but not yours - which might be hit only after the assertion above. I have created Code unloading fails assert: "Can not revert previous transactions" · Issue #9187 · root-project/root · GitHub to track this. Thanks for reporting!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.