Problem using STL vector<TCut> in ROOT script

Hello,

I am trying to use a STL vector of TCut in
a function in a ROOT script:

  TCut cut_ZEl="abs(ZId)==11";

  vector<TCut> cuts;
  cuts.push_back(cut_ZEl);

Upon leaving that function, root crashes with the
message:

the vector is located a function in the file utils.C

From other postings, I conclude that is possible to use
stl vectors in ROOT. Any idea why this crashes?

Thanks,
Vuko

Could you post the shortest possible RUNNING script reproducing this problem?

Rene

Hi Rene,

[quote=“brun”]Could you post the shortest possible RUNNING script reproducing this problem?

Rene[/quote]

Here we go:


#include "TCut.h"
#include <vector>

TCut gimeFinalCut()
{
  
  TCut cut_WZ_found="WlId!=0 && ZId!=0";  // W and Z candidates are there
  TCut cut_Wl_pt="WlPt>20.";

  std::vector<TCut> cuts;
  cuts.push_back(cut_WZ_found);

  return cuts[0];

}

void test_stl()
{
  TCut c = gimeFinalCut();
  cout << "back \n";

}

Actually, the error i s not the same, but it still crashes:

root [0] .x macro/test_stl.C();
*** glibc detected *** double free or corruption (fasttop): 0x091b44d0 ***

Why is that?

Cheers,
Vuko

You are making a basic C++ mistake. When exiting from the function gimeFinalCut thestd::vector that you created inside is automatically deleted.
You should use new instead.

Rene

[quote=“brun”]Could you post the shortest possible RUNNING script reproducing this problem?

Rene[/quote]
It’s strange, but I don’t see any fatal mistake in the code :frowning:

[code]#include

test()
{
TCut cut_ZEl(“abs(ZId)==11”);
vector cuts;
cuts.push_back(cut_ZEl);
}[/code]

[quote]root [0] .x /tmp/test.C
*** glibc detected *** /home/anar/ROOT/5.19/bin/root.exe: double free or corruption (!prev): 0x099b1cd0 ***[/quote]

Unfortunately I don’t have currently ROOT debug libraries and can’t really play with the debugger. So maybe I am mistaking, but it seems, a double deletion happens…

[quote=“brun”]You are making a basic C++ mistake. When exiting from the function gimeFinalCut thestd::vector that you created inside is automatically deleted.
You should use new instead.

Rene[/quote]
He doesn’t use a reference, so I think it is a copy, which will be returned and even RVO will do its job (copy operation will not be performed - optimized out). So there should be no mistake here, maybe only design things.
Or I’ve just overlooked something?

Hi Rene,

I was aware of this, but I don’t see why it is a mistake. The vector
I created was intended only for temporary use within that function.
I knew it would be deleted by leaving the function but why should that
cause a crash? I assume the destructor of vector will delete all its
elements, which is fine with me as well, leaving aside the issue
of performance/speed.

I actually tried to compile and run the very same code, just
replacing TCut with a basic type, e.g. int, or some other simple
class, and it works.

Oh, and in case it matters, I am using root v5.18

Thanks!

Vuko

Hi,

CINT has problems with interpreting vector. Please create a dictionary for it by putting the following lines into the file vecTCutDict.C:

[code]#include “TCut.h”
#include

#ifdef MAKECINT
#pragma link C++ class vector+;
#endif
[/code]
and run .L vecTCutDict.C+ .x test.C You can also put the line gROOT->ProcessLine(".L vecTCutDict.C+"); into your test() / gimeFinalCut() function.

Cheers, Axel.