Segmentation fault when building example

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.

#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.

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++


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

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

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.

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

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.

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…

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

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:

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?

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.

1 Like