The same program works well in VSCODE but break in the terminal

My ROOT program works well in VSCODE (wsl extension).
But when I run it in the terminal, it breaks

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f23efacfc3a in __GI___wait4 (pid=10473, stat_loc=stat_loc
entry=0x7ffd5c0001e8, options=options
entry=0, usage=usage
entry=0x0) at ../sysdeps/unix/sysv/linux/wait4.c:27
#1  0x00007f23efacfbfb in __GI___waitpid (pid=<optimized out>, stat_loc=stat_loc
entry=0x7ffd5c0001e8, options=options
entry=0) at waitpid.c:38
#2  0x00007f23efa3ef67 in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:172
#3  0x00007f23f00ec6de in TUnixSystem::StackTrace() () from /home/llm/software/root/lib/libCore.so.6.26
#4  0x00007f23f00e9565 in TUnixSystem::DispatchSignals(ESignals) () from /home/llm/software/root/lib/libCore.so.6.26
#5  <signal handler called>
#6  0x00007f23ef5309f0 in TBufferFile::ReadUInt(unsigned int&) () from /home/llm/software/root/lib/libRIO.so
#7  0x00007f23da065f35 in TBranchElement::ReadLeavesCustomStreamer(TBuffer&) () from /home/llm/software/root/lib/libTree.so.6.26.10
#8  0x00007f23da05f420 in TBranch::GetEntry(long long, int) () from /home/llm/software/root/lib/libTree.so.6.26.10
#9  0x00007f23da079ec0 in TBranchElement::GetEntry(long long, int) () from /home/llm/software/root/lib/libTree.so.6.26.10
#10 0x00007f23da0d8197 in TTree::GetEntry(long long, int) () from /home/llm/software/root/lib/libTree.so.6.26.10
#11 0x00007f23e9cf5c66 in ?? ()
#12 0x000055c54993f8c0 in ?? ()
#13 0x000055c549aa3100 in ?? ()
#14 0x00007f23eb7606a0 in ?? () from /home/llm/software/root/lib/libCling.so
#15 0x00007f23e9cf8000 in ?? ()
#16 0x000055c54a6a2b10 in ?? ()
#17 0x00007f23ec9bfe5a in llvm::RuntimeDyldImpl::resolveLocalRelocations() () from /home/llm/software/root/lib/libCling.so
#18 0x000055c547d784d0 in ?? ()
#19 0x00007f23ec9bc290 in ?? () from /home/llm/software/root/lib/libCling.so
#20 0x0000000000000003 in ?? ()
#21 0x000055c549744210 in ?? ()
#22 0x000055c547d5ea00 in ?? ()
#23 0x4014000000000000 in ?? ()
#24 0x408f400000000000 in ?? ()
#25 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/forum
Only if you are really convinced it is a bug in ROOT then please submit a
report at https://root.cern/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  0x00007f23ef5309f0 in TBufferFile::ReadUInt(unsigned int&) () from /home/llm/software/root/lib/libRIO.so
#7  0x00007f23da065f35 in TBranchElement::ReadLeavesCustomStreamer(TBuffer&) () from /home/llm/software/root/lib/libTree.so.6.26.10
#8  0x00007f23da05f420 in TBranch::GetEntry(long long, int) () from /home/llm/software/root/lib/libTree.so.6.26.10
#9  0x00007f23da079ec0 in TBranchElement::GetEntry(long long, int) () from /home/llm/software/root/lib/libTree.so.6.26.10
#10 0x00007f23da0d8197 in TTree::GetEntry(long long, int) () from /home/llm/software/root/lib/libTree.so.6.26.10
#11 0x00007f23e9cf5c66 in ?? ()
#12 0x000055c54993f8c0 in ?? ()
#13 0x000055c549aa3100 in ?? ()
#14 0x00007f23eb7606a0 in ?? () from /home/llm/software/root/lib/libCling.so
#15 0x00007f23e9cf8000 in ?? ()
#16 0x000055c54a6a2b10 in ?? ()
#17 0x00007f23ec9bfe5a in llvm::RuntimeDyldImpl::resolveLocalRelocations() () from /home/llm/software/root/lib/libCling.so
#18 0x000055c547d784d0 in ?? ()
#19 0x00007f23ec9bc290 in ?? () from /home/llm/software/root/lib/libCling.so
#20 0x0000000000000003 in ?? ()
#21 0x000055c549744210 in ?? ()
#22 0x000055c547d5ea00 in ?? ()
#23 0x4014000000000000 in ?? ()
#24 0x408f400000000000 in ?? ()
#25 0x0000000000000000 in ?? ()
===========================================================


Root > %

Could anyone tell me what happened?

_ROOT Version: 6.26/10
_Platform: WSL2
_Compiler: linuxx8664gcc


Hi,

Very hard to say what is going on from the stacktrace, besides the fact you seem to be opening a file. Could you please share a minimal reproducer of the issue?

Best,
D

I have uploaded a reproducer.
test.zip (307.3 KB)
Run test.cpp, then test1.cpp
There are several problems:

  1. test.cpp works well in wsl terminal but breaks in VSCODE
  2. test1.cpp works well inVSCODE but breaks in wsl terminal
  3. Both of them need a piece of “meanless” code including a “cout” as I comment in the code. Otherwise, they will break.

And this is the “launch.json” of my VSCODE project

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(gdb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "/home/llm/software/root/bin/root.exe",
      "args": [
        "-l",
        "-q",
        "test.cpp+g"
      ],
      "stopAtEntry": false,
      "cwd": "${fileDirname}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ]
}

and “c_cpp_properties.json”

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/home/llm/software/root/include/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "linux-clang-x64"
        }
    ],
    "version": 4
}

Best regards,
llm

Both of them need a piece of “meanless” code including a “cout” as I comment in the code. Otherwise, they will break

This is a clear indication that the code has some serious memory errors. Using valgrind and the suppression file $ROOTSYS/etc/valgrind-root.supp usually lead to the actual problem.

The code contains:

void read_a_txt(TString filefullpath, TTree* tree)
{
	Int_t ev_num;
	TDatime* datime;
	Float_t temperature, rh;
	auto waves = new Float_t[8][1024]{};
	tree->SetBranchAddress("ev", &ev_num);
	tree->SetBranchAddress("datime", &datime);
	tree->SetBranchAddress("temp", &temperature);
	tree->SetBranchAddress("rh", &rh);
	tree->SetBranchAddress("waves", waves);
        ...
	delete[] waves;
}

At the end of the function the TTree still exist (and continue to be used) BUT also hold pointers to non-invalid (and soon to be reused for a different purpose) stack memory (all the value of expression like &ev_num and waves).

To solve the problem use the following at the end of the function:

	tree->ResetBranchAddresses();
	delete[] waves;
}

To solve the problem use the following at the end of the function:

	tree->ResetBranchAddresses();
	delete[] waves;
}

It appears that doesn’t solve the problem.
I will continue to learn how to use valgrind. Thanks for your suggestion.

It might not be sufficient but given the use of stack variable, it is necessary.