# Segmentation fault when building example

**URL:** <https://root-forum.cern.ch/t/segmentation-fault-when-building-example/51060>\
**Category:** Newbie\
**Created:** [August 4, 2022, 10:19am UTC](https://root-forum.cern.ch/t/segmentation-fault-when-building-example/51060 "2022-08-04T10:19:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![louisld](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/4bbf92/32.png) [@louisld](https://root-forum.cern.ch/u/louisld)\
**Post date:** [August 4, 2022, 10:19am UTC](https://root-forum.cern.ch/t/segmentation-fault-when-building-example/51060/1 "2022-08-04T10:19:49Z")

</div>

I built ROOT from sources using the following commands:

`git clone --branch latest-stable --depth=1 https://github.com/root-project/root.git root_src`

`cmake -G"Visual Studio 17 2022" -A x64 -Thost=x64 -DCMAKE_INSTALL_PREFIX=..\install_dir ..\root_src`

`cmake --build . --config Debug --target install`

I then tried to build an example to test if my setup worked.

```cpp
#include "TF1.h"
#include "TApplication.h"
#include "TCanvas.h"
#include "TRootCanvas.h"

int main(int argc, char** argv)
{
	TApplication app("app", &argc, argv); // Seg fault happens with this line
	TCanvas* c = new TCanvas("c", "Something", 0, 0, 800, 600);
	TF1* f1 = new TF1("f1", "sin(x)", -5, 5);
	f1->SetLineColor(kBlue + 1);
	f1->SetTitle("My graph;x; sin(x)");
	f1->Draw();
	c->Modified(); c->Update();
	TRootCanvas* rc = (TRootCanvas*)c->GetCanvasImp();
	rc->Connect("CloseWindow()", "TApplication", gApplication, "Terminate()");
	app.Run();
	return 0;
}

```

When I build this code and run it, I get a segmentation fault.

```auto
Critical error detected c0000374
Exception thrown at 0x00007FFF29FEF609 (ntdll.dll) in RootTest.exe: 0xC0000374: Un segment de mémoire a été endommagé (parameters: 0x00007FFF2A0597F0).
Unhandled exception at 0x00007FFF29FEF609 (ntdll.dll) in RootTest.exe: 0xC0000374: Un segment de mémoire a été endommagé (parameters: 0x00007FFF2A0597F0).

Exception thrown at 0x00007FFF29F0EC5C (ntdll.dll) in RootTest.exe: 0xC0000005: Access violation reading location 0x0000000000000008.

Unhandled exception at 0x00007FFF29F0EC5C (ntdll.dll) in RootTest.exe: 0xC0000005: Access violation reading location 0x0000000000000008.

Exception thrown at 0x00007FFF29F0EC5C (ntdll.dll) in RootTest.exe: 0xC0000005: Access violation reading location 0x0000000000000008.

Unhandled exception at 0x00007FFF29F0EC5C (ntdll.dll) in RootTest.exe: 0xC0000005: Access violation reading location 0x0000000000000008.

Exception thrown at 0x00007FFF29F0EC5C (ntdll.dll) in RootTest.exe: 0xC0000005: Access violation reading location 0x0000000000000008.

Unhandled exception at 0x00007FFF29F0EC5C (ntdll.dll) in RootTest.exe: 0xC0000005: Access violation reading location 0x0000000000000008.

Exception thrown at 0x00007FFF29F0EC5C (ntdll.dll) in RootTest.exe: 0xC0000005: Access violation reading location 0x0000000000000008.

The program '[28120] RootTest.exe' has exited with code 0 (0x0).

```

Visual Studio tells me that the error comes from `TWinNTSystem.cxx` line 1017

`while (buf[0] && GetFileAttributes(check_path.Data()) == INVALID_FILE_ATTRIBUTES) {`

I use `/sdl-`, `/permissive` and `/std=c++17`.

Do you have any idea why I am having this segmentation fault ?

\_ROOT Version: 6.26.06  
\_Platform: Windows  
\_Compiler: Visual C++

* * *

---

<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:** [August 4, 2022, 10:41am UTC](https://root-forum.cern.ch/t/segmentation-fault-when-building-example/51060/2 "2022-08-04T10:41:18Z")

</div>

I can’t reproduce the problem. How do you build your test application? I tried with:

```auto
cl -nologo -Z7 -MD -GR -EHsc -Zc:__cplusplus -std:c++17 RootTest.cxx -I %ROOTSYS%\include /link -LIBPATH:%ROOTSYS%\lib libCore.lib libHist.lib libGpad.lib libGui.lib

```

---

<div class="post-metadata">

**Author:** ![louisld](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/4bbf92/32.png) [@louisld](https://root-forum.cern.ch/u/louisld)\
**Post date:** [August 4, 2022, 11:26am UTC](https://root-forum.cern.ch/t/segmentation-fault-when-building-example/51060/3 "2022-08-04T11:26:49Z")

</div>

When I build the application with visual studio, I have the following build command:

`C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.32.31326\bin\HostX64\x64\CL.exe /c /IC:\Users\DELL\source\repos\install_dir\include /ZI /JMC /nologo /W3 /WX- /diagnostics:column /sdl- /Od /D _DEBUG /D _CONSOLE /D _MBCS /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /std:c++17 /permissive /Fo"x64\Debug\\" /Fd"x64\Debug\vc143.pdb" /external:W3 /Gd /TP /FC /errorReport:prompt RootTest.cpp`

`C:\Users\DELL\source\repos\install_dir` is the location of ROOT.

---

<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:** [August 4, 2022, 11:37am UTC](https://root-forum.cern.ch/t/segmentation-fault-when-building-example/51060/4 "2022-08-04T11:37:39Z")

</div>

OK, so FYI, you should use the same flags than the ones used to build ROOT. For example:

```auto
C:\Users\bellenot\rootdev>root-config --cflags
 -nologo -Zc:__cplusplus -std:c++17 -MD -GR -EHsc- -W3 -D_WIN32 -O2 -IC:\Users\bellenot\build\x86\release\include

```

But anyway, even with your flags (minus `/Fo"x64\Debug\\" /Fd"x64\Debug\vc143.pdb"`), it doesn’t crash for me.

---

<div class="post-metadata">

**Author:** ![louisld](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/4bbf92/32.png) [@louisld](https://root-forum.cern.ch/u/louisld)\
**Post date:** [August 4, 2022, 11:56am UTC](https://root-forum.cern.ch/t/segmentation-fault-when-building-example/51060/5 "2022-08-04T11:56:47Z")

</div>

It’s strange because I had the same problem with the binary distribution of ROOT. Do you have an idea where it can come from ?

Even with the same tags it does not work…

---

<div class="post-metadata">

**Author:** ![louisld](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/4bbf92/32.png) [@louisld](https://root-forum.cern.ch/u/louisld)\
**Post date:** [August 4, 2022, 12:18pm UTC](https://root-forum.cern.ch/t/segmentation-fault-when-building-example/51060/6 "2022-08-04T12:18:34Z")

</div>

I was able to build and run the example in visual studio prompt tool after I added the binary directory of ROOT to the PATH. I am still not able to run it from visual studio but at least I know it is possible.

Here is my compile command:

`cl -nologo -std:c++17 -MD -GR -EHsc -W3 -D_WIN32 -D -O2 /sdl- /permissive RootTest.cpp -I %ROOTSYS%\include /link -LIBPATH:%ROOTSYS%\lib libCore.lib libGpad.lib libHist.lib`

---

<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:** [August 4, 2022, 12:23pm UTC](https://root-forum.cern.ch/t/segmentation-fault-when-building-example/51060/7 "2022-08-04T12:23:51Z")

</div>

Well, since you build ROOT in debug mode, you should be able to track down the issue. Just start your application in the debugger and see where it crashes. If it is in:

> [@louisld](#):
>
> Visual Studio tells me that the error comes from `TWinNTSystem.cxx` line 1017
> 
> `while (buf[0] && GetFileAttributes(check_path.Data()) == INVALID_FILE_ATTRIBUTES) {`

Then put a breakpoint there and see what is the faulty value. Note that this is happening when trying to find the `ROOTSYS/etc` directory. Do you properly call `thisroot.bat` before starting your application?

---

<div class="post-metadata">

**Author:** ![louisld](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/4bbf92/32.png) [@louisld](https://root-forum.cern.ch/u/louisld)\
**Post date:** [August 4, 2022, 12:29pm UTC](https://root-forum.cern.ch/t/segmentation-fault-when-building-example/51060/8 "2022-08-04T12:29:34Z")

</div>

Now it works ! Thank you.  
You are right, I thought I ran thisroot.bat, but maybe it did not work. Setting %ROOTSYS% and updating the PATH solved the problem.
