Can a std::vector create problems with a TFile?

Hello,
I have written a program that reads a binary file ad creates two histograms. It then writes them to a file called tracks.root
When I try to do that, it creates an empty root file:

root [0] TFile asd("tracks.root" ); root [1] .ls TFile** tracks.root TFile* tracks.root root [2]

I thought that maybe is a problem of my code, so I ask to a friend to send me his program. I compiled it on my computer and it works. I then copied his lines of code that handle the TFile and the histograms. But my program still writes that empty file.
I also tried to compile it on two different distros: Slackware and Ubuntu.
The only difference between the two sources is that I store all events in a std::vector while my friend does not store them.

On the attachment there is the program that I wrote.
htfit.cpp (3.77 KB)

  1. If you want somebody to help you with your program, you should try to post minimal working ( == at least compilable) piece of code, reproducing your problem. What is WireHit? Who knows?

  2. You should give minimal input data files, if your program requires them.
    Should I generate input data myself?

  3. Your program. I skip many parts, but only some interesting moments:

a)
vector < WireHit > *temp = new vector < WireHit >;//MEMORY LEAK!!!

You produce memore leak here. You create vector in dynamic memory, add a copy of this vector into your ‘events’ vector, and … who will delete temp???

If you really want such a vector, why not simply events.push_back(vector()) ?

b)
WireHit *temp = new WireHit(hit_data[0], hit_data[1], hit_data[2]);
events[event_counter].push_back(*temp);

Again, memory leak here.

and events[event_counter].push_back(*temp);
You have a vector and a separate event_counter, to simplify your program, you can do

WireHit temp(hit_data[0], hit_data[1], hit_data[2]);
events.back().push_back(temp);

c) WOW!!!

for ( unsigned int i = 0; i < events.size(); i++) {
for ( unsigned int j = 0; j < events[i].size(); j++) {
delete & events[i][j];
}

  delete & events[i];

}

What do you think this code means? Do you understand what is your ‘events’?

events is:

vector of vectors of WireHit:
[vector of WireHits->[WireHit][WireHit][WireHit]][vector of WireHits->[WireHit][WireHit][WireHit]][vector of WireHits->[WireHit][WireHit][WireHit]]…

so, each such vector of WireHit holds WireHits by value, and this vector manages its memory itself, you cannot delete part of this memory like delete & events[i][j]; (I know, you think, you delete something that was created in temp = new … :slight_smile:) )