# \*\*\*Break\*\*\*\* Segmentation violation for a compiled program

**URL:** <https://root-forum.cern.ch/t/break-segmentation-violation-for-a-compiled-program/42022>\
**Category:** Newbie\
**Created:** [October 27, 2020, 7:11am UTC](https://root-forum.cern.ch/t/break-segmentation-violation-for-a-compiled-program/42022 "2020-10-27T07:11:15Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![boboc](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/c2a13f/32.png) [@boboc](https://root-forum.cern.ch/u/boboc)\
**Post date:** [October 27, 2020, 7:11am UTC](https://root-forum.cern.ch/t/break-segmentation-violation-for-a-compiled-program/42022/1 "2020-10-27T07:11:15Z")

</div>

Hello!

I am trying to compile a program based on `macro.C` (attached to this message); what I am trying to do is to write the classes into separate files, not in the main program like `macro.C` and get an executable. The program compiles without a problem but when I try to open it (`./task1`) I get ` ***Break**** Segmentation violation`. The main body of the program is `task1.C`.

For the compilation, I am using this commands:

> ‘root-config --cxx --cflags’ -fPIC -c clase.cxx  
> rootcint -f clase\_Dict.cxx -c -p clase.hxx clase\_LinkDef.h  
> ‘root-config --cxx --cflags’ -fPIC -c clase\_Dict.cxx  
> ‘root-config --cxx --cflags’ -fPIC -shared -o libclase.so clase.o clase\_Dict.o  
> g++ -o task1 task1.C clase.cxx clase\_Dict.o clase.o `root-config --cflags --libs`

Is there something else I need to do?

Thanks!

[task1.C](https://root-forum.cern.ch/uploads/short-url/gwGqXje3tc5UNhrAHErNmpsgIZ8.C) (1.2 KB) [macro.C](https://root-forum.cern.ch/uploads/short-url/eD9MKGUzBHsTsPuZje9aqvNMfjl.C) (1.3 KB) [clase\_LinkDef.h](https://root-forum.cern.ch/uploads/short-url/dWMJhv2lLUeSKtVy7mTCVV69pnM.h) (87 Bytes) [clase.hxx](https://root-forum.cern.ch/uploads/short-url/3eRAiAoamqTIFdzsQOwIBGEMSHb.hxx) (305 Bytes) [clase.cxx](https://root-forum.cern.ch/uploads/short-url/vIxN0xAOLmGRgYsymHnJq5SFw96.cxx) (38 Bytes)

* * *

_ROOT Version:_ 6.22/02  
_Platform:_ Xubuntu 20.04 LTS  
_Compiler:_ g++ 9.3.0

* * *

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [October 27, 2020, 7:52pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-for-a-compiled-program/42022/2 "2020-10-27T19:52:05Z")

</div>

Hello boboc,

I was not able to reproduce this in my environment with gcc-4.8.5 (CentOS 7) and ROOT v6-22-02.

What I can suggest is to add the `-g` compiler option to your command line, and running your program under `gdb` so that you are able to get a backtrace when the program SIGSEGV’s. Please, attach the generated backtrace so that we can diagnose the problem.

Cheers,  
J.

---

<div class="post-metadata">

**Author:** ![boboc](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/c2a13f/32.png) [@boboc](https://root-forum.cern.ch/u/boboc)\
**Post date:** [October 28, 2020, 7:38am UTC](https://root-forum.cern.ch/t/break-segmentation-violation-for-a-compiled-program/42022/3 "2020-10-28T07:38:12Z")

</div>

Hello, @jalopezg!

Here is the updated code:[task1.C](https://root-forum.cern.ch/uploads/short-url/2zdmt5itlSmgS6KseT1inm749dC.C) (1.2 KB) [macro.C](https://root-forum.cern.ch/uploads/short-url/9yOWSVKMzEMHlEBn1a6jGlst8Hi.C) (1.3 KB) [clase\_LinkDef.h](https://root-forum.cern.ch/uploads/short-url/zL56rtr2OHdY6jyE9NXTxFIUKeU.h) (89 Bytes) [clase.hxx](https://root-forum.cern.ch/uploads/short-url/9jvmQJ80xE8cp0Ilf3lJ3zLxSIv.hxx) (351 Bytes) [clase.cxx](https://root-forum.cern.ch/uploads/short-url/24MDXcNArMwmtkGLRAemzVahJ99.cxx) (58 Bytes)

When I comment lines 58 to 62 from `task1.C` the program works well and I can visualize the data with TBrowser. But when I uncomment those lines, `tree -> StartViewer()` gives me a segmentation fault. Here is the error:

```auto
> ***Break*** segmentation violation
>
>
>
>===========================================================
>There was a crash.
>This is the entire stack trace of all threads:
>===========================================================
>#0 0x00007f100c8f6dba in __GI___ wait4 (pid=10206, stat_loc=stat_loc
>entry=0x7ffe5be9d8e8, options=options
>entry=0, usage=usage
>entry=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:27
>#1 0x00007f100c8f6d7b in __GI___ waitpid (pid=<optimized out>, stat_loc=stat_loc
>entry=0x7ffe5be9d8e8, options=options
>entry=0) at waitpid.c:38
>#2 0x00007f100c8660e7 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:172
>#3 0x00007f100d90f49e in TUnixSystem::StackTrace() () from /home/petre/ROOT/lib/libCore.so.6.22
>#4 0x00007f100d90c325 in TUnixSystem::DispatchSignals(ESignals) () from /home/petre/ROOT/lib/libCore.so.6.22
>#5 <signal handler called>
>#6 0x0000561a1690bb57 in main () at task1.C:58
>===========================================================
>
>
>The lines below might hint at the cause of the crash.
>You may get help by asking at the ROOT forum http://root.cern.ch/forum
>Only if you are really convinced it is a bug in ROOT then please submit a
>report at http://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 0x0000561a1690bb57 in main () at task1.C:58
>===========================================================

```

Thank you,  
Petre Boboc

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [October 28, 2020, 12:49pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-for-a-compiled-program/42022/4 "2020-10-28T12:49:45Z")

</div>

Hello @boboc,

Lacking a ROOT debug build where I can inspect the complete backtrace, I will invite @jblomer and @eguiraud. Maybe they can help diagnosing this issue.

Cheers,  
J.

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [October 28, 2020, 1:25pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-for-a-compiled-program/42022/5 "2020-10-28T13:25:11Z")

</div>

In task1.C, shouldn’t it be `canv->cd(0)` (or simply `canv->cd()`) since no sub pads were defined?

---

<div class="post-metadata">

**Author:** ![boboc](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/c2a13f/32.png) [@boboc](https://root-forum.cern.ch/u/boboc)\
**Post date:** [October 28, 2020, 1:41pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-for-a-compiled-program/42022/6 "2020-10-28T13:41:13Z")

</div>

Hello @jblomer,

I just tried with both `canv->cd(0)` and `canv->cd()`; I still have the same problem.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 28, 2020, 2:31pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-for-a-compiled-program/42022/7 "2020-10-28T14:31:53Z")

</div>

I ran you macro on Mac with ROOT 6.23.  
It does not crash.

---

<div class="post-metadata">

**Author:** ![boboc](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/c2a13f/32.png) [@boboc](https://root-forum.cern.ch/u/boboc)\
**Post date:** [October 28, 2020, 2:33pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-for-a-compiled-program/42022/8 "2020-10-28T14:33:33Z")

</div>

The macro works well. The problem appears when I compile the program and I run it (when I run `./task1`). I don’t understand why this happens since the macro works without issues.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [October 28, 2020, 9:25pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-for-a-compiled-program/42022/9 "2020-10-28T21:25:02Z")

</div>

You could try adding:

```auto
int main(int argc, char **argv) {
   TApplication app(argc, argv);

```

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [November 2, 2020, 11:03am UTC](https://root-forum.cern.ch/t/break-segmentation-violation-for-a-compiled-program/42022/10 "2020-11-02T11:03:54Z")

</div>

Hi,  
the stacktrace mentions task1.C:58 as the offending line.  
At that line there is

```auto
tree->StartViewer();

```

but right before you close the file that contains `tree`, `outputFile->Close`: as a consequence, the TTree object `tree` points to is destroyed. Classic ROOT ownership shenanigans.

Moving `outputFile->Close();` to the end of `main` should fix the issue.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![boboc](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/c2a13f/32.png) [@boboc](https://root-forum.cern.ch/u/boboc)\
**Post date:** [November 2, 2020, 1:15pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-for-a-compiled-program/42022/11 "2020-11-02T13:15:02Z")

</div>

Hello, @eguiraud!

I still get “Segmentation fault” when I move `outputFile->Close();` to the end of `main`.

 ![Screenshot_2020-11-02_15-14-27](https://root-forum.cern.ch/uploads/default/original/3X/f/d/fd78d67ac4f949305cba68abca2a1faf1b2579f1.png)

Best regards,  
Petre Boboc

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [November 2, 2020, 2:23pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-for-a-compiled-program/42022/12 "2020-11-02T14:23:20Z")

</div>

Argh, yes, different crash – now at line 62.  
I can reproduce it, the best stacktrace I could get so far is

```auto
#8 0x00007f322e1ebb44 in TGCompositeFrame::TGCompositeFrame (this=0x3e378c0, p=0x0, w=10, h=10, options=3, back=0) at /workspace/build/projects/ROOT-v6.22.00/src/ROOT/v6.22.00/gui/gui/src/TGFrame.cxx:852
#9 0x00007f322e1ed6f2 in TGMainFrame::TGMainFrame (this=0x3e378c0, p=0x0, w=10, h=10, options=2) at /workspace/build/projects/ROOT-v6.22.00/src/ROOT/v6.22.00/gui/gui/src/TGFrame.cxx:1429
#10 0x00007f322e6224ce in TTreeViewer::TTreeViewer (this=0x3e378c0, tree=0x33531e0) at /workspace/build/projects/ROOT-v6.22.00/src/ROOT/v6.22.00/tree/treeviewer/src/TTreeViewer.cxx:346
#11 0x00007f3246283059 in ?? ()
#12 0x00000000017e45a0 in ?? ()
#13 0x00000000017e45a0 in ?? ()
#14 0x0000000003e378c0 in ?? ()
#15 0x0000000003e378c0 in ?? ()
#16 0x00007ffffd3f54e0 in ?? ()
#17 0x00007ffffd3f5688 in ?? ()
#18 0x00007ffffd3f5f50 in ?? ()
#19 0x00007ffffd3f56a8 in ?? ()
#20 0x00000001fd3f5690 in ?? ()
#21 0x0000000000000000 in ?? ()

```

Work in progress 😃

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [November 2, 2020, 2:49pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-for-a-compiled-program/42022/13 "2020-11-02T14:49:39Z")

</div>

Alright: to fix the `TGCompositeFrame` crash, you need to have a `TApplication` running, as mentioned above in the thread (it’s always needed to have ROOT graphics properly working in compiled ROOT programs).

You need to:

- `#include "TApplication.h"`
- add `TApplication app("app", nullptr, nullptr);` at the beginning of `main`
- add `app.Run()` at the end of `main`, before `outputfile->Close()`

The attached version of `task1.C` compiles and runs fine for me.

[task1.C](https://root-forum.cern.ch/uploads/short-url/dRxyxwpqxG4gKmmueY4FDuYjjA5.C) (1.3 KB)

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [November 2, 2020, 2:58pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-for-a-compiled-program/42022/14 "2020-11-02T14:58:46Z")

</div>

See also [Creating a user application with ROOT](https://root.cern/manual/creating_a_user_application/)

---

<div class="post-metadata">

**Author:** ![boboc](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/c2a13f/32.png) [@boboc](https://root-forum.cern.ch/u/boboc)\
**Post date:** [November 2, 2020, 7:29pm UTC](https://root-forum.cern.ch/t/break-segmentation-violation-for-a-compiled-program/42022/15 "2020-11-02T19:29:01Z")

</div>

It works!

Thank you very much!
