Error in path/to/root/directory/bin/root.exe: free(): invalid size

I am testing a small chunk of my code (C++ macro), which looks like:

void Function() {

TFile *openFile = new TFile("somefile.root", "READ");

TTree *dataTree = (TTree*) openFile->Get("some data");

store data from dataTree into vectors

delete openFile;

//2D_PDF is a class that contains two coordinate vectors of double and one 2-index vector of double (technically, vector < vector >

create an instance of 2D_PDF from the vectors above

for (iterate over "a pair of pairs of indices," so 4 indices that are i_x, i_y and j_x, j_y)
    double xi = PDF.coord1[i_x], yi = PDF.coord2[i_y], wi = PDF.weight[i_x][i_y];
    double xj = PDF.coord1[j_x], yj = PDF.coord2[j_y], wj = PDF.weight[j_x][j_y];
    do some calculations with these variables

print some result statements

}

So, I am basically constructing a 2-dimensional probability density function out of data from a TTree in my .root data file and running some calculations for each pair of “square cells (i and j)” in the 2D PDF.

The code exits properly (successfully prints out the very last line of the code), but gives me:

*** Error in `/home/scheong/Desktop/root-6.06.02/bin/root.exe’: free(): invalid size: 0x0000000003fcc910 ***

in the end, and exits the root session (I am running the macro by .x Function.C).

This seems to be an error with the root built to my computer, not within the C++ macro itself, so I am not sure where to look to fix this. I just deleted root and re-installed it, yet the problem persists.

What is the problem? Is it something with the root build/setup, or is this an error with my code? I am suspecting it is the former, since the for loop I am running is fairly simple, and I cannot find any memory error (which is what free(): invalid size: seems to imply) in my loop.

BTW, the class definition of PDF is included in a different file PDF.cpp, and I have

#include “PDF.cpp”

at the beginning of the macro Function.C

Try to debug it using [url=https://root-forum.cern.ch/t/segmentation-fault-depending-on-linux-distribution/18020/6 and [url=https://root-forum.cern.ch/t/addresssanitizer-memory-error-detector/21046/1

[quote=“Pepe Le Pew”]Try to debug it using [url=https://root-forum.cern.ch/t/segmentation-fault-depending-on-linux-distribution/18020/6 and [url=https://root-forum.cern.ch/t/addresssanitizer-memory-error-detector/21046/1

I tried rewriting the iteration loop in many different ways, but it is giving me all sorts of different problems (mostly memory-related): segmentation violation, error with malloc.c in root.exe, etc.

Could you explain a little more in deal how to use AddressSanitizer? I read the link: AddressSanitizer memory error detector
but am still not sure how to apply it to my Function.C macro.

[url=https://root-forum.cern.ch/t/addresssanitizer-memory-error-detector/21046/1 complete AddressSanitizer example uses a simple “trial.cxx”[/url] (try it in order to learn how to use it), which you then need to replace with your own “Function.C”.

Will do. To do this, however, I will have to include the relevant root packages/headers (e.g. TMath.h, TH1.h) in my Function.C macro, correct? As of now, I run the macro in the root session, so I did not need to explicitly include the root packages.

In order to find source code problems, try to precompile your macro using ACLiC, e.g. try something like (inspect all reported warnings and errors and GET RID OF THEM ALL):
root [0] .x Function.C++

[quote=“Pepe Le Pew”]In order to find source code problems, try to precompile your macro using ACLiC, e.g. try something like (inspect all reported warnings and errors and GET RID OF THEM ALL):
root [0] .x Function.C++[/quote]

If I explicitly include statements like:
#include “TFile.h”
#include “TTree.h”

and do:
root [0] .x Function.C++

the code, again, runs and ends properly (prints the very last test statement) and then gives me:

*** Error in `/path/to/root/bin/root.exe’: free(): invalid size: 0x000000000446da90 ***

So it’s time for [url=https://root-forum.cern.ch/t/segmentation-fault-depending-on-linux-distribution/18020/6 and/or [url=https://root-forum.cern.ch/t/addresssanitizer-memory-error-detector/21046/1

BTW. Make sure that you use the latest ROOT version (e.g. either ROOT 6.06/04 or ROOT 5.34/36 or heads of the “patches” branches).

Running

ASAN_OPTIONS=“abort_on_error=1” gdb -ex run --args root-config --bindir/root.exe -l -q TestingLoop.C++

ran successfully, printed the very last print("TEST\n"); statement, and terminated with the following message returned:

...
Reading symbols from /home/scheong/Desktop/root-6.06.02/bin/root.exe...(no debugging symbols found)...done.
Starting program: /home/scheong/Desktop/root-6.06.02/bin/root.exe -l -q TestingLoop.C++
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
root [0] 
Processing TestingLoop.C++...
Info in <TUnixSystem::ACLiC>: creating shared library /home/scheong/gitcodes/bao/./TestingLoop_C.so
//bunch of print statements during the calculation & time logs for testing
Calculation: complete! Total time taken: 4.50s




Code: complete! Total time taken: 5.75s
TEST
*** Error in `/home/scheong/Desktop/root-6.06.02/bin/root.exe': free(): invalid size: 0x0000000003888370 ***

Program received signal SIGABRT, Aborted.
0x00007ffff6afcc37 in __GI_raise (sig=sig@entry=6)
    at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56	../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) 

[quote=“Pepe Le Pew”]So it’s time for [url=https://root-forum.cern.ch/t/segmentation-fault-depending-on-linux-distribution/18020/6 and/or [url=https://root-forum.cern.ch/t/addresssanitizer-memory-error-detector/21046/1

BTW. Make sure that you use the latest ROOT version (e.g. either ROOT 6.06/04 or ROOT 5.34/36 or heads of the “patches” branches).[/quote]

Posted above is the result from running AddressSanitizer as you mentioned.:
As I mentioned in the original post, I removed and reinstalled ROOT earlier today (root_v6.06.02), so I doubt if the version is the issue here.

Try ROOT 6.06/04 - 2016-05-03.

Note: [url=https://root-forum.cern.ch/t/addresssanitizer-memory-error-detector/21046/1 finds one kind of problems, [url=https://root-forum.cern.ch/t/segmentation-fault-depending-on-linux-distribution/18020/6 finds another kinds.

[quote=“Pepe Le Pew”]Try ROOT 6.06/04 - 2016-05-03.

Note: [url=https://root-forum.cern.ch/t/addresssanitizer-memory-error-detector/21046/1 finds one kind of problems, [url=https://root-forum.cern.ch/t/segmentation-fault-depending-on-linux-distribution/18020/6 finds another kinds.[/quote]

The exact same problem persists in ROOT 6.06.04.

Hi,

can you share a standalone macro that reproduces this behaviour?

Cheers,
Danilo

[quote=“dpiparo”]Hi,

can you share a standalone macro that reproduces this behaviour?

Cheers,
Danilo[/quote]

I will try to isolate all the dependencies and details as much as possible and write a short version of the code that reproduces the error. Thanks.

[quote=“dpiparo”]Hi,

can you share a standalone macro that reproduces this behaviour?

Cheers,
Danilo[/quote]

This is not quite the code, but roughly this is what is happening.


void Function() {
	gROOT->Reset();
	
	TFile *openFile = new TFile("data/somefile.root", "READ");
	
	double x, y, z, weight	//these are just placeholder variables
	
	TTree *treeA = (TTree*) openFile->Get("data_A");
	treeA->SetBranchAddress("x", &x);
	treeA->SetBranchAddress("y", &y);
	treeA->SetBranchAddress("weight", &weight);
	
	//store / download the data in tree to vectors
	int Amax = treeA->GetEntriesFast();
	vector<double> A_x (Amax, 0.);
	vector<double> A_y (Amax, 0.);
	vector<double> A_weight (Amax, 0.);
	for (int i = 0; i < Amax; ++i) {
		treeA->GetEntry(i);
		A_x[i] = x, A_y[i] = y, A_weight[i] = weight;
	}
	
	delete openFile;	//I can now simply refer to these vectors for data, the original file/trees are useless
	
	//some calculation codes that iterate over the vectors defined above
	//I have ran these codes with a fake randomly generated data set (pure C++ code, no ROOT whatsoever)
	//it ran completely fine, so the actual calculation code/algorithm do not seem to have any problem
	
	printf("Some calculation results");
	
	printf("TEST\n");
	
}

Since this seems to be a memory-related error, I am assuming that something bad is happening with the TFile or TTree…

Does this give enough context about what’s going on?

Remove the line “gROOT->Reset();”.
We need your “data/somefile.root” file in order to try it.

Hi,

thanks for producing a simple program to illustrate the behaviour. Unfortunately, without being able to run it, it is not possible to state anything. The input rootfile needs to be shared.

Danilo