Issue with root object memory leaks


_ROOT Version: 6.24/00
_Platform: Ubuntu 20.04.2 LTS
_Compiler: G++

So I have a program I am working on for my research and the main root objects I use mainly consists TF1, TH1D, TCanvas, TGraph, TGraphErrors. I think there are major memory leaks in my program because of random crashes. For example, whenever I do .q to exit root I get the error, “double free or corruption (out)” or “corrupted double linked list” (I always get some error when exiting). Sometime when I try to run the program I get a break segment violation
sometimes i get malloc_consolidate(): invalid chunk size
sometimes i get corrupted size vs. prev_size
sometimes i get corrupted double linked list
sometimes i get malloc(): largebin double linked list corrupted (bk)
or sometimes I get no errors and the program runs fine and without ever crashing during runtime. I was wondering if someone could tell me what objects(listed above) I need to explicitly delete and which ones root deletes for me. I have looked at the object ownership document and I did not find a clear answer(I think this is because I don’t understand the inner workings of root that well). I tried to just delete everything I dynamically allocated, but I cant delete the histograms or the canvases because I need them to display, and if I delete them they do not display.

There is too much source code to post here, I also don’t know what source code I would post. If anyone want to look at the code here is the git hub

all the code is in the multi file. If anyone wants to try to go though that, just let me know and i’ll try to explain everything as best I can.

Hi @Mitchem ,
and welcome to the ROOT forum!
From the error messages it would look more like a double free or a nullptr dereference than a memory leak.
The most likely cause is a bug in your code’s object lifetime management – possibly due to interactions with ROOT’s own memory management (e.g. TH1Ds are owned by their TFiles and closing a TFile also deletes the TH1Ds. If you then also delete the TH1D, that’s a double free).

If this is a C++ compiled program, you can compile it against a version of ROOT with debug symbols (several are available on CVMFS, for example) and then run it inside valgrind --suppressions=$ROOTSYS/etc/valgrind-root.supp to check for errors. It should tell you exactly where things are going wrong and how.

Cheers,
Enrico

I will check out what you recommended but if its a bug in the code. Why would I only get these error messages sometimes, then sometimes it works correctly?

That can happen with C++ programs: these kind of errors are caused by an access to freed memory or similar. That’s “undefined behavior” in C++, meaning that formally anything could happen (and the compiler doesn’t have to warn you).

In practice what error you will see (or whether you will se an error at all) will depend exactly on what happened to the memory region that the program is wrongly accessing in the meanwhile. If it was reused for something else, you will corrupt some program data, or maybe there will be no visible effect at all. If it was returned to the system, that’s an invalid memory access and you’ll get a segmentation fault.

Cheers,
Enrico

So I managed to run my program in valgrind using the command
valgrind --num-callers=30 --suppressions=$ROOTSYS/etc/valgrind-root.supp root.exe -l -b -q fitMultiple.cc
and got the output
`==3983== Memcheck, a memory error detector
==3983== Copyright (C) 2002-2017, and GNU GPL’d, by Julian Seward et al.
==3983== Using Valgrind-3.17.0 and LibVEX; rerun with -h for copyright info
==3983== Command: root.exe -l -b -q fitMultiple.cc
==3983==

Processing fitMultiple.cc…
==3983== Invalid write of size 8
==3983== at 0x1EC3ECF8: ???
==3983== by 0x521B04D: ???
==3983== by 0x680DEDE: cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6782C3E: cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x67845AC: cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x68774C4: cling::MetaSema::actOnxCommand(llvm::StringRef, llvm::StringRef, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6885BD4: cling::MetaParser::isXCommand(cling::MetaSema::ActionResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x688707D: cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6870828: cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x667E89B: HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x669727D: TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6697639: TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x4E317B9: TApplication::ExecuteFile(char const*, int*, bool) (in /home/jamal/root/root-6.24.00-install/lib/libCore.so)
==3983== by 0x4E324F2: TApplication::ProcessLine(char const*, bool, int*) (in /home/jamal/root/root-6.24.00-install/lib/libCore.so)
==3983== by 0x4C5D1B5: TRint::ProcessLineNr(char const*, char const*, int*) (in /home/jamal/root/root-6.24.00-install/lib/libRint.so)
==3983== by 0x4C5EABC: TRint::Run(bool) (in /home/jamal/root/root-6.24.00-install/lib/libRint.so)
==3983== by 0x10917F: main (in /home/jamal/root/root-6.24.00-install/bin/root.exe)
==3983== Address 0xc21d180 is 0 bytes after a block of size 0 alloc’d
==3983== at 0x4A3A99F: operator new[](unsigned long) (vg_replace_malloc.c:579)
==3983== by 0x1EC3ECCF: ???
==3983== by 0x521B04D: ???
==3983== by 0x680DEDE: cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6782C3E: cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x67845AC: cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x68774C4: cling::MetaSema::actOnxCommand(llvm::StringRef, llvm::StringRef, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6885BD4: cling::MetaParser::isXCommand(cling::MetaSema::ActionResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x688707D: cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6870828: cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x667E89B: HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x669727D: TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6697639: TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x4E317B9: TApplication::ExecuteFile(char const*, int*, bool) (in /home/jamal/root/root-6.24.00-install/lib/libCore.so)
==3983== by 0x4E324F2: TApplication::ProcessLine(char const*, bool, int*) (in /home/jamal/root/root-6.24.00-install/lib/libCore.so)
==3983== by 0x4C5D1B5: TRint::ProcessLineNr(char const*, char const*, int*) (in /home/jamal/root/root-6.24.00-install/lib/libRint.so)
==3983== by 0x4C5EABC: TRint::Run(bool) (in /home/jamal/root/root-6.24.00-install/lib/libRint.so)
==3983== by 0x10917F: main (in /home/jamal/root/root-6.24.00-install/bin/root.exe)
==3983==
==3983== Invalid write of size 8
==3983== at 0x1EC3ED0C: ???
==3983== by 0x521B04D: ???
==3983== by 0x680DEDE: cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6782C3E: cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x67845AC: cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x68774C4: cling::MetaSema::actOnxCommand(llvm::StringRef, llvm::StringRef, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6885BD4: cling::MetaParser::isXCommand(cling::MetaSema::ActionResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x688707D: cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6870828: cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x667E89B: HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x669727D: TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6697639: TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x4E317B9: TApplication::ExecuteFile(char const*, int*, bool) (in /home/jamal/root/root-6.24.00-install/lib/libCore.so)
==3983== by 0x4E324F2: TApplication::ProcessLine(char const*, bool, int*) (in /home/jamal/root/root-6.24.00-install/lib/libCore.so)
==3983== by 0x4C5D1B5: TRint::ProcessLineNr(char const*, char const*, int*) (in /home/jamal/root/root-6.24.00-install/lib/libRint.so)
==3983== by 0x4C5EABC: TRint::Run(bool) (in /home/jamal/root/root-6.24.00-install/lib/libRint.so)
==3983== by 0x10917F: main (in /home/jamal/root/root-6.24.00-install/bin/root.exe)
==3983== Address 0xc21d188 is 8 bytes after a block of size 0 alloc’d
==3983== at 0x4A3A99F: operator new[](unsigned long) (vg_replace_malloc.c:579)
==3983== by 0x1EC3ECCF: ???
==3983== by 0x521B04D: ???
==3983== by 0x680DEDE: cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6782C3E: cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x67845AC: cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x68774C4: cling::MetaSema::actOnxCommand(llvm::StringRef, llvm::StringRef, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6885BD4: cling::MetaParser::isXCommand(cling::MetaSema::ActionResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x688707D: cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6870828: cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x667E89B: HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x669727D: TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6697639: TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x4E317B9: TApplication::ExecuteFile(char const*, int*, bool) (in /home/jamal/root/root-6.24.00-install/lib/libCore.so)
==3983== by 0x4E324F2: TApplication::ProcessLine(char const*, bool, int*) (in /home/jamal/root/root-6.24.00-install/lib/libCore.so)
==3983== by 0x4C5D1B5: TRint::ProcessLineNr(char const*, char const*, int*) (in /home/jamal/root/root-6.24.00-install/lib/libRint.so)
==3983== by 0x4C5EABC: TRint::Run(bool) (in /home/jamal/root/root-6.24.00-install/lib/libRint.so)
==3983== by 0x10917F: main (in /home/jamal/root/root-6.24.00-install/bin/root.exe)
==3983==
==3983== Invalid write of size 8
==3983== at 0x1EC3ED21: ???
==3983== by 0x521B04D: ???
==3983== by 0x680DEDE: cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6782C3E: cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x67845AC: cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x68774C4: cling::MetaSema::actOnxCommand(llvm::StringRef, llvm::StringRef, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6885BD4: cling::MetaParser::isXCommand(cling::MetaSema::ActionResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x688707D: cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6870828: cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x667E89B: HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x669727D: TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6697639: TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x4E317B9: TApplication::ExecuteFile(char const*, int*, bool) (in /home/jamal/root/root-6.24.00-install/lib/libCore.so)
==3983== by 0x4E324F2: TApplication::ProcessLine(char const*, bool, int*) (in /home/jamal/root/root-6.24.00-install/lib/libCore.so)
==3983== by 0x4C5D1B5: TRint::ProcessLineNr(char const*, char const*, int*) (in /home/jamal/root/root-6.24.00-install/lib/libRint.so)
==3983== by 0x4C5EABC: TRint::Run(bool) (in /home/jamal/root/root-6.24.00-install/lib/libRint.so)
==3983== by 0x10917F: main (in /home/jamal/root/root-6.24.00-install/bin/root.exe)
==3983== Address 0xc21d190 is 16 bytes after a block of size 0 alloc’d
==3983== at 0x4A3A99F: operator new[](unsigned long) (vg_replace_malloc.c:579)
==3983== by 0x1EC3ECCF: ???
==3983== by 0x521B04D: ???
==3983== by 0x680DEDE: cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6782C3E: cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x67845AC: cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x68774C4: cling::MetaSema::actOnxCommand(llvm::StringRef, llvm::StringRef, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6885BD4: cling::MetaParser::isXCommand(cling::MetaSema::ActionResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x688707D: cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6870828: cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x667E89B: HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x669727D: TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6697639: TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x4E317B9: TApplication::ExecuteFile(char const*, int*, bool) (in /home/jamal/root/root-6.24.00-install/lib/libCore.so)
==3983== by 0x4E324F2: TApplication::ProcessLine(char const*, bool, int*) (in /home/jamal/root/root-6.24.00-install/lib/libCore.so)
==3983== by 0x4C5D1B5: TRint::ProcessLineNr(char const*, char const*, int*) (in /home/jamal/root/root-6.24.00-install/lib/libRint.so)
==3983== by 0x4C5EABC: TRint::Run(bool) (in /home/jamal/root/root-6.24.00-install/lib/libRint.so)
==3983== by 0x10917F: main (in /home/jamal/root/root-6.24.00-install/bin/root.exe)
==3983==
==3983== Invalid write of size 8
==3983== at 0x1EC3ED36: ???
==3983== by 0x521B04D: ???
==3983== by 0x680DEDE: cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6782C3E: cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x67845AC: cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x68774C4: cling::MetaSema::actOnxCommand(llvm::StringRef, llvm::StringRef, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6885BD4: cling::MetaParser::isXCommand(cling::MetaSema::ActionResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x688707D: cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6870828: cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x667E89B: HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x669727D: TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6697639: TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x4E317B9: TApplication::ExecuteFile(char const*, int*, bool) (in /home/jamal/root/root-6.24.00-install/lib/libCore.so)
==3983== by 0x4E324F2: TApplication::ProcessLine(char const*, bool, int*) (in /home/jamal/root/root-6.24.00-install/lib/libCore.so)
==3983== by 0x4C5D1B5: TRint::ProcessLineNr(char const*, char const*, int*) (in /home/jamal/root/root-6.24.00-install/lib/libRint.so)
==3983== by 0x4C5EABC: TRint::Run(bool) (in /home/jamal/root/root-6.24.00-install/lib/libRint.so)
==3983== by 0x10917F: main (in /home/jamal/root/root-6.24.00-install/bin/root.exe)
==3983== Address 0xc21d198 is 24 bytes after a block of size 0 in arena “client”
==3983==

valgrind: m_mallocfree.c:305 (get_bszB_as_is): Assertion ‘bszB_lo == bszB_hi’ failed.
valgrind: Heap block lo/hi size mismatch: lo = 64, hi = 516180624.
This is probably caused by your program erroneously writing past the
end of a heap block and corrupting heap metadata. If you fix any
invalid writes reported by Memcheck, this assertion failure will
probably go away. Please try that before reporting this as a bug.

host stacktrace:
==3983== at 0x5810495A: show_sched_status_wrk (m_libcassert.c:385)
==3983== by 0x580E6107: report_and_quit (m_libcassert.c:474)
==3983== by 0x580E6287: vgPlain_assert_fail (m_libcassert.c:497)
==3983== by 0x580F3E2A: get_bszB_as_is (m_mallocfree.c:297)
==3983== by 0x5818ECBD: vgPlain_describe_arena_addr (m_mallocfree.c:341)
==3983== by 0x5818ECBD: vgPlain_describe_addr (m_addrinfo.c:87)
==3983== by 0x581C26DE: vgMemCheck_update_Error_extra (mc_errors.c:1143)
==3983== by 0x581C9006: vgPlain_maybe_record_error (m_errormgr.c:683)
==3983== by 0x581E051D: vgMemCheck_record_address_error (mc_errors.c:735)
==3983== by 0x10128619A2: ???
==3983== by 0x1003455E9F: ???
==3983== by 0x1003455EDF: ???

sched status:
running_tid=1

Thread 1: status = VgTs_Runnable (lwpid 3983)
==3983== at 0x1EC3ED4B: ???
==3983== by 0x521B04D: ???
==3983== by 0x680DEDE: cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6782C3E: cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x67845AC: cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x68774C4: cling::MetaSema::actOnxCommand(llvm::StringRef, llvm::StringRef, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6885BD4: cling::MetaParser::isXCommand(cling::MetaSema::ActionResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x688707D: cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6870828: cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x667E89B: HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x669727D: TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x6697639: TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (in /home/jamal/root/root-6.24.00-install/lib/libCling.so)
==3983== by 0x4E317B9: TApplication::ExecuteFile(char const*, int*, bool) (in /home/jamal/root/root-6.24.00-install/lib/libCore.so)
==3983== by 0x4E324F2: TApplication::ProcessLine(char const*, bool, int*) (in /home/jamal/root/root-6.24.00-install/lib/libCore.so)
==3983== by 0x4C5D1B5: TRint::ProcessLineNr(char const*, char const*, int*) (in /home/jamal/root/root-6.24.00-install/lib/libRint.so)
==3983== by 0x4C5EABC: TRint::Run(bool) (in /home/jamal/root/root-6.24.00-install/lib/libRint.so)
==3983== by 0x10917F: main (in /home/jamal/root/root-6.24.00-install/bin/root.exe)
client stack range: [0x1FFEFE8000 0x1FFF000FFF] client SP: 0x1FFEFFAD30
valgrind stack range: [0x1003356000 0x1003455FFF] top usage: 12064 of 1048576

Note: see also the FAQ in the source distribution.
It contains workarounds to several common problems.
In particular, if Valgrind aborted or crashed after
identifying problems in your program, there’s a good chance
that fixing those problems will prevent Valgrind aborting or
crashing, especially if it happened in m_mallocfree.c.

If that doesn’t help, please report this bug to: www.valgrind.org

In the bug report, send all the above text, the valgrind
version, and what OS and version you are using. Thanks.
`
I get the ??? becuase I am not running a version of root in a debug more to where I have access to debug symbols. How do I install/set root to go to a debug mode and use that with valgrind?

Hi @Mitchem ,
ROOT builds with debug symbols are available from CVMFS (easily accessible e.g. from LXPLUS). Otherwise you can build ROOT with cmake option -DCMAKE_BUILD_TYPE=Debug.

Another thing that might help is to add the necessary #include and then compile the macro with debug symbols (e.g. with g++ -g -o program program.cpp $(root-config --libs --cflags) rather than executing it through the ROOT interpreter.

I can also take a look if you can provide step by step instructions to reproduce the problem.
Cheers,
Enrico

So im having trouble setting up a root environment with debug symbols, if I could do this I should be able to find my issues easily. I followed this page under quick start Building ROOT from source - ROOT
git clone --branch latest-stable https://github.com/root-project/root.git root_src
then made a directory called build_root and install_root, then went into the build_root directory and ran cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=install_root ../root_src
then I just ran cmake --build . from there.
Then I sourced the build in my .bashrc

This did not seem to get debug symbols working. Did I do something wrong in the installation or is there some setting I need to set. I have really never used cmake before and am kind of foggy on what it does so I might have messed up somewhere.

Also I can’t find resources over what CVMFS is or how to get a root install working with it.

Hi @Mitchem ,
if the build finished correctly, you have a ROOT build with debug symbols. If you still have “???” in the stacktraces it’s probably because of interpreted (just-in-time compiled) code – you can try running the macro as root macro.C+g instead of root macro.C – the +g at the end tells ROOT to compile the code in a shared library (+) with debug symbols (g) before executing it. That might help valgrind recover debug information for the parts of the code that are still missing it.

Or if you can, you can also add a main function at the end of the macro and compile it as a normal C++ program with e.g. g++ -g -o macro macro.C $(root-config --libs --cflags).

Let me know if I can help further.
Cheers,
Enrico

So can I use a compiled program with valgrind? I thought valgrind would only work with the source program. Also my main file im using is a .cc and not a .C, im not sure what the difference is or if it matters. Also if you want to try it our you can go to the github page GitHub - mitcharoni12/IntegralMethod at work and all you need in the work branch is the multi folder. That has all the code im working on(main file is fitMultiple.cc), all you should need to do to run it is just launch root and do .x fitMultiple.cc. To run it with valgrind I have been doing Valgrind --num-callers=30 --suppressions=$ROOTSYS/etc/valgrind-root.supp root.exe -l -b -q fitMultiple.cc but I still get the ???.

Absolutely, valgrind works with any executable. If the executable has debug symbols (-g flag to g++, I just updated the command I suggested above) valgrind makes use of those to give better diagnostics (but it also works without).

No difference, suffixes are just a convention and they don’t change the contents of the files at all. A text file is a text file.

Yes see my previous post for the likely explanation and possible solutions.

Alright, thanks for the instructions, I’ll try it out as soon as I can.

It can be tedious to figure out how to get good debugging output the first time, but then it makes it super easy to solve any future issues, so it’s worth it.

Cheers,
Enrico

Hi @Mitchem ,
what did I do wrong?

~/S/w/f/I/multi (master=) root -l fitMultiple.cc
root [0]
Processing fitMultiple.cc...
In file included from input_line_8:1:
/home/blue/Scratchpad/work/forum_double_free/IntegralMethod/multi/fitMultiple.cc:8:10: fatal error: 'Cycle.h' file not found
#include "Cycle.h"
         ^~~~~~~~~

sorry, I forgot when you pull from the github it pulls the master branch and not the work branch. When you are in the directory that you pulled the directory into just run a git switch work then try it again.

1 Like

Ok, there is a number of “shady-looking” things in the source code :smiley:

I just added these lines at the end of fitMultiple.cc:

int main() {
   fitMultiple();
}

then compiled with debug symbols -g compiler flag, and ran the program through gdb.

The first crash I get is due to the code here, coming from fitMultiple.cc:

int numElements;

//RUN(N)- process that happens N times in which histograms of the decay is generated and the fi
//CYCLE(M)- process that happens M time in which the mean values for the histograms of the RUN
void fitMultiple()
{
    Int_t eventDecrement, numBins, rebinDifference;
    Double_t timeRun;
    bool rangeOption;
    Double_t valueHolder;
    int histoSimulateChoice, rangeValueChoice, displayIndividualFitsChoice, rebinChoice;
    ElementFit* element;
    ifstream inFile;

    //fitFunctions array passed in and used in element object
    Int_t numFitFunction = numElements*2;
    decayFunction* fitFunctions = new decayFunction[numFitFunction];
    fitFunctions[0] = CSDecaybyActivity;
    fitFunctions[1] = CSDecaybyActivityIntegral;
    fitFunctions[2] = BADecaybyActivity;
    fitFunctions[3] = BADecaybyActivityIntegral;
    fitFunctions[4] = LADecaybyActivity;
    fitFunctions[5] = LADecaybyActivityIntegral;

numElements is never initialized (but being a global variable I think it’s implicitly initialized to 0), and then new decayFunction[numFitFunction] allocates 0 elements (but then you access up to number 5).

thanks for pointing that out, can’t believe I didn’t see that. I’ve fixed that now. Also I compiled with this command g++ -g -o fitMultiple fitMultiple.cc $(root-config --libs --cflags), then ran Valgrind --num-callers=30 --suppressions=$ROOTSYS/etc/valgrind-root.supp root.exe -l -b -q fitMultiple and just got a stack and heap summary and it said there were no errors. However, when I now run Valgrind --num-callers=30 --suppressions=$ROOTSYS/etc/valgrind-root.supp root.exe -l -b -q fitMultiple.cc, I get different errors, but the ??? are still there. Ill keep trying the other things you recommended to try to make the ??? go away.

You can just run valgrind --num-callers=30 --suppressions=$ROOTSYS/etc/valgrind-root.supp ./fitMultiple.

1 Like

that worked, thank you so much. Now I have a lot of errors to go fix but at least I know where they are now.

You might also want to compile with -Wall -Wextra -Wpedantic which tells the compiler to complain about a bunch of not-ok-but-not-errors things (e.g. uninitialized variables).

In my opinion ROOT macros are great for quick prototyping but for complex applications it’s often simpler to switch to compiled code for the reasons we saw above.

Cheers,
Enrico

thank you for the recommendations, I will definitely do that. One more question, im still foggy on object ownership for root objects. Would you recommend I just implement code to take ownership of all the objects myself and then delete them? I looked at the object ownership page for root and it confused me a bit. For example, if an object is owned by gDirectory, do I need to delete that gDirectory object at the end of the program so that everything owned by gDirectory is deleted or does root automatically delete gDirectory at the end of the program? Also it looks like canvases are owned by gRoot, do I need to delete gRoot or is that deletion also handled by root?

Yes it’s a bit complicated (and we are simplifying the behavior in all new interfaces, but old interfaces will stay like that).

In general you don’t have to delete stuff that is owned by ROOT (like TCanvas) nor global ROOT things like gROOT or gDirectory.

You can delete ROOT-owned objects if you delete them before ROOT does: e.g. you can delete a TCanvas before application exit and it will be de-registered from ROOT’s “list of things to delete”, avoid a double delete.

Sorry @eguiraud, one more question

This program im working on involves a lot of fitting. I noticed in my histograms bin 0 is always 0 and bin 1 is regular. When I call the Fit() method on a histogram, does it start fitting from bin 0 or bin 1?