Problem with Ttree and TApplication

Hello,

I have a problem using Ttree objects and Tapplication in a standalone program.
I call in my main.cpp a function test() which call the following command : tree->StartViewer();
My codes look like this:

int main(int argc, char *argv[])
{
	TApplication app("App", &argc, argv);
	test();
	app.Run();
	
        return 0;
}

and the function test is like

int test()
{

	int tec_all;
	TTree *tree = new TTree("tree","tree");
	
	tree->Branch("Data_all",&tec_all);
	
	for(unsigned int i(0);i<10;++i)
	{
		tec_all=i;
		tree->Fill();
	}
	tree->StartViewer();
	
	return 0;
}

I can compile the previous codes and it works fine. However, if I add some variables in test(), (for example variables of standard types such as double, int…) after double clicking on the leaf “data_all” I get a segmentation violation:

*** Break *** segmentation violation



===========================================================
There was a crash (kSigSegmentationViolation).
This is the entire stack trace of all threads:
===========================================================
#0  0xb77b2424 in __kernel_vsyscall ()
#1  0xb4d7e2f3 in waitpid () from /lib/tls/i686/cmov/libc.so.6
#2  0xb4d26669 in strtold_l () from /lib/tls/i686/cmov/libc.so.6
#3  0xb4e2dadd in system () from /lib/tls/i686/cmov/libpthread.so.0
#4  0xb723df41 in TUnixSystem::Exec (this=0x9f2a098,
    shellcmd=0xa797840 "/usr/local/pub/debian4.0/i686/gcc411-21/rootmgr/527-06/etc/gdb-backtrace.sh 20705 1>&2") at core/unix/src/TUnixSystem.cxx:1982
#5  0xb723d180 in TUnixSystem::StackTrace (this=0x9f2a098) at core/unix/src/TUnixSystem.cxx:2192
#6  0xb7240476 in TUnixSystem::DispatchSignals (this=0x9f2a098, sig=kSigSegmentationViolation) at core/unix/src/TUnixSystem.cxx:1108
#7  0xb72405ad in SigHandler (sig=kSigSegmentationViolation) at core/unix/src/TUnixSystem.cxx:352
#8  0xb7235fa8 in sighandler (sig=11) at core/unix/src/TUnixSystem.cxx:3461
#9  <signal handler called>
#10 0xb723ff16 in TUnixSystem::DispatchOneEvent (this=0x14, pendingOnly=false) at core/unix/src/TUnixSystem.cxx:908
#11 0xb7191441 in TSystem::InnerLoop (this=0x9f2a098) at core/base/src/TSystem.cxx:406
#12 0xb71a0210 in TSystem::Run (this=0x9f2a098) at core/base/src/TSystem.cxx:356
#13 0xb7116dfe in TApplication::Run (this=0xbff671b0, retrn=false) at core/base/src/TApplication.cxx:1047
#14 0x0806c133 in main (argc=0, argv=0xbff672bc) at main.cpp:124
===========================================================

I probably do something wrong but I don’t know what. Any idea?

Thank you for your help.

Hi,

After TTree::Branch, the tree remembers the addresses being passed. In particular this means that if you are passing the address of a local variable, at the end of the function, those addresses becomes invalid. To fix the problem add:tree->ResetBranchAddresses();at the end of your test() routine.

Cheers,
Philippe.

Hi Philippe,

thank you for your explanation. This is working fine now!