# C++ code crashes when trying to run root files

**URL:** <https://root-forum.cern.ch/t/c-code-crashes-when-trying-to-run-root-files/56513>\
**Category:** Newbie\
**Tags:** hist, root\
**Created:** [September 27, 2023, 5:47pm UTC](https://root-forum.cern.ch/t/c-code-crashes-when-trying-to-run-root-files/56513 "2023-09-27T17:47:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xavier\_James](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/xavier_james/32/32932_2.png) [@Xavier\_James](https://root-forum.cern.ch/u/Xavier_James)\
**Post date:** [September 27, 2023, 5:47pm UTC](https://root-forum.cern.ch/t/c-code-crashes-when-trying-to-run-root-files/56513/1 "2023-09-27T17:47:16Z")

</div>

Dear All,

I keep receiving an error message below when trying to run the attached script. It has worked on a co-worker’s computer but having trouble understanding why I am having these difficulties.

The script in question is an addback script for nuclear structure.

Much appreciated,

Xavier

[fill\_hists\_new.cpp](https://root-forum.cern.ch/uploads/short-url/qGeGPjUxZah1kGDEX8ygTcEdTti.cpp) (16.0 KB)

```plaintext
Processing fill_hists.cpp("root_data_mvmelst_070.bin_tree.root")...
1329550
Finished creating histograms
Filling histograms for 1329550 events.

 ***Break*** segmentation violation

===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0 0x00007fcb820ea45a in __GI___ wait4 (pid=3970, stat_loc=stat_loc
entry=0x7ffde4f78358, options=options
entry=0, usage=usage
entry=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:30
#1 0x00007fcb820ea41b in __GI___ waitpid (pid=<optimized out>, stat_loc=stat_loc
entry=0x7ffde4f78358, options=options
entry=0) at ./posix/waitpid.c:38
#2 0x00007fcb82050bcb in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:171
#3 0x00007fcb82b17494 in TUnixSystem::StackTrace() () from /usr/local/lib/libCore.so
#4 0x00007fcb82b14795 in TUnixSystem::DispatchSignals(ESignals) () from /usr/local/lib/libCore.so
#5 <signal handler called>
#6 0x00007fcb81a9f546 in ?? ()
#7 0x0000000000000000 in ?? ()
===========================================================

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 0x00007fcb81a9f546 in ?? ()
#7 0x0000000000000000 in ?? ()
===========================================================

```

_ROOT Version:_ 6.24/06  
_Platform:_ Ubuntu  
_Compiler:_

* * *

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [September 27, 2023, 6:29pm UTC](https://root-forum.cern.ch/t/c-code-crashes-when-trying-to-run-root-files/56513/2 "2023-09-27T18:29:01Z")

</div>

Hi @Xavier_James!

Which ROOT version does your coworker use? You’re using 6.24/06, which is already a few years old. Could it be that the problem went away with newer ROOT versions? The most recent one is 6.28/06.

Besides that, I can’t give you any advice unfortunately. The stack trace is not very telling. If you want us to debug further, can you please share the `"root_data_mvmelst_070.bin_tree.root"` file so we can try to find the error?

Cheers!  
Jonas

---

<div class="post-metadata">

**Author:** ![Xavier\_James](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/xavier_james/32/32932_2.png) [@Xavier\_James](https://root-forum.cern.ch/u/Xavier_James)\
**Post date:** [September 27, 2023, 6:51pm UTC](https://root-forum.cern.ch/t/c-code-crashes-when-trying-to-run-root-files/56513/3 "2023-09-27T18:51:53Z")

</div>

Hi @jonas ,

We are both using the same version of ROOT. I will attach the file in this message. Thank you!

Unfortunately, it is a couple gigs and will not allow me to attach it to this message.

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [September 27, 2023, 7:01pm UTC](https://root-forum.cern.ch/t/c-code-crashes-when-trying-to-run-root-files/56513/4 "2023-09-27T19:01:01Z")

</div>

Okay, that’s a problem then. Maybe you find another way of sharing the file?

But if it’s so much data maybe that could be a hint to the problem. Is it possible that the memory usage grows so large that this causes a crash, and you maybe have less RAM than your colleague? Did you monitor the memory usage with `top` or some other system tool while the ROOT macro is running?

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [September 27, 2023, 9:01pm UTC](https://root-forum.cern.ch/t/c-code-crashes-when-trying-to-run-root-files/56513/5 "2023-09-27T21:01:37Z")

</div>

Dear @Xavier_James ,

On top of what Jonas already suggested and his comments, which are all correct and on point, may I also strongly suggest you to revisit your code in order to avoid all the dynamic allocations with no memory management? That means, every time there is a call to `new` in your program there should be a corresponding `delete` call. Currently, your program has zero `delete`. Since this approach is quite cumbersome, you should simply avoid creating objects on the heap whenever possible and just declare them on the stack (e.g. `TH1D myHisto;` instead of `auto myHisto = new TH1D()`). Or, if you really need to create objects on the heap, wrap them in `std::unique_ptr`.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![Xavier\_James](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/xavier_james/32/32932_2.png) [@Xavier\_James](https://root-forum.cern.ch/u/Xavier_James)\
**Post date:** [September 28, 2023, 1:03pm UTC](https://root-forum.cern.ch/t/c-code-crashes-when-trying-to-run-root-files/56513/6 "2023-09-28T13:03:21Z")

</div>

The data files are supposed to be a few gigs since it contains experimental data. I have monitored the memory usage and have been editing line by line.

---

<div class="post-metadata">

**Author:** ![Xavier\_James](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/xavier_james/32/32932_2.png) [@Xavier\_James](https://root-forum.cern.ch/u/Xavier_James)\
**Post date:** [September 28, 2023, 1:04pm UTC](https://root-forum.cern.ch/t/c-code-crashes-when-trying-to-run-root-files/56513/7 "2023-09-28T13:04:05Z")

</div>

Thank you! I will try that and see if that will work.
