All code pass all map by value. Inefficient and easy to SIGSEGV. Return from getStripsMap() is temp. Should not pass to SetBranchAddress() etc., as will fill temp map, not m_stripsMap. Need to make sure to keep alive with python reference until end of use.
Okay, for good case, make example work. One, toto.h:[code]#include
#include
class RelationsObject { };
class something : RelationsObject {
public:
something(): m_var1(0), m_var2(0), m_stripsMap() {}
something( double var1, double var2,
std::map<unsigned int, float> StripsMap):
m_var1(var1), m_var2(var2),
m_stripsMap(StripsMap)
{}
const std::map<unsigned int, float> getStripsMap() const {
return m_stripsMap;}
double m_var1, m_var2;
std::map<unsigned int, float> m_stripsMap;
};[/code]
And two, toto.py:[code]from ROOT import *
gInterpreter.GenerateDictionary(“map<unsigned int, float>”,“map”);
gInterpreter.GenerateDictionary(“pair<unsigned int, float>”,“utility”);
gROOT.LoadMacro(“toto.h+”)
gROOT.ProcessLine(“struct EtaData {
std::map<unsigned int, float> stripsMap;
};”)
m = std.map(‘unsigned int, float’)()
for k, v in zip(xrange(10), xrange(10)):
m[k] = 3.14*v
s = something(0., 1., m)
e = EtaData()
e.stripsMap = s.getStripsMap()[/code]
with run like:$ python toto.py
and then SIGSGEV, yes? See, make reproducer is easy.
And now have trace, yes? Trace say broken memory of “e.stripsMap” (EtaData default ctor not generated and can not put in ProcessLine b/c “CINT limitation”). Can see by adding:"struct EtaData {\
std::map<unsigned int, float> stripsMap;\
};
to toto.h instead of ProcessLine(), then all work fine.
CINT no longer developed, need workaround use ACLIC (or use ROOT6, already said that). Example toto2.py have workaround:[code]from ROOT import *
gInterpreter.GenerateDictionary(“map<unsigned int, float>”,“map”);
gInterpreter.GenerateDictionary(“pair<unsigned int, float>”,“utility”);
gROOT.LoadMacro(“toto.h+”)
import os
fn = “cint_bricolage.C"
try:
f = os.open(fn, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
except OSError:
pass
else:
os.write(f, “””#include
struct EtaData {
std::map<unsigned int, float> stripsMap;
};""")
os.close(f)
gROOT.LoadMacro(fn+’+’)
m = std.map(‘unsigned int, float’)()
for k, v in zip(xrange(10), xrange(10)):
m[k] = 3.14*v
s = something(0., 1., m)
e = EtaData()
e.stripsMap = s.getStripsMap()[/code]
Now no SIGSEGV. Still argue fix all temp creates.
-Dom