<div dir="ltr">Hello Matt,<div><br></div><div>That static down-cast is valid c++ if the Get() returns a valid TTree*, but it is risky in that all you need is one condition where the Get() returns a non-null pointer to a non-TTree* object, and you will have undefined behavior.  The dynamic_cast is safer, in that it graciously returns a null if the pointer is invalid.</div>
<div><br></div><div>-Richard Jones</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Feb 6, 2014 at 1:15 PM, Matthew Shepherd &lt;<a href="mailto:mashephe@indiana.edu">mashephe@indiana.edu</a>&gt; wrote:<br>
<blockquote class="gmail_quote"><br>
Dear C++ gurus,<br>
<br>
I&#39;ve been trying to track down a problem that Eric Pooser was having using AmpTools on several machines, all running a new version of Ubuntu with gcc 4.6.3  (don&#39;t know if that is relevant).<br>
<br>
The problem is that the following (relatively standard) code:<br>
<br>
TTree* m_inTree = static_cast&lt;TTree*&gt;( m_inFile-&gt;Get( inTreeName.c_str() ) );<br>
<br>
Results in a segfault (ROOT-reported stack trace pasted below).<br>
<br>
However if I change:<br>
<br>
TTree* m_inTree = dynamic_cast&lt;TTree*&gt;( m_inFile-&gt;Get( inTreeName.c_str() ) );<br>
<br>
The code runs fine.  I have never seen anything like this.  Does anyone have any idea why a static_cast would generate code that results in a segfault but a dynamic_cast would work?  Is it the the difference between runtime and compile-time casting? but how?<br>

<br>
In this case, dynamic_cast is actually more appropriate, so I have no problem switching to that method; however, the solution to the problem leaves me a little unsatisfied.  Any ideas?<br>
<br>
Matt<br>
<br>
<br>
<br>
There was a crash.<br>
This is the entire stack trace of all threads:<br>
===========================================================<br>
#0  0x00007f8139a10c8e in __libc_waitpid (pid=&lt;optimized out&gt;, stat_loc=0x7fff918fa540, options=0) at ../sysdeps/unix/sysv/linux/waitpid.c:32<br>
#1  0x00007f813999629e in do_system (line=0x24f0ec0 &quot;/home/pooser/matt_work/root_v5.34.14/etc/gdb-backtrace.sh 32076 1&gt;&amp;2&quot;) at ../sysdeps/posix/system.c:149<br>
#2  0x00007f813b5f3337 in TUnixSystem::StackTrace() () from /home/pooser/matt_work/root_v5.34.14/lib/libCore.so<br>
#3  0x00007f813b5f5c23 in TUnixSystem::DispatchSignals(ESignals) () from /home/pooser/matt_work/root_v5.34.14/lib/libCore.so<br>
#4  &lt;signal handler called&gt;<br>
#5  0x00007f813b5aa983 in TObjArray::Init(int, int) () from /home/pooser/matt_work/root_v5.34.14/lib/libCore.so<br>
#6  0x00007f813b5aad82 in TObjArray::TObjArray(int, int) () from /home/pooser/matt_work/root_v5.34.14/lib/libCore.so<br>
#7  0x00007f813b85f4b5 in ROOT::new_TObjArray(void*) () from /home/pooser/matt_work/root_v5.34.14/lib/libCore.so<br>
#8  0x00007f813b5c5ee4 in TClass::New(void*, TClass::ENewType) const () from /home/pooser/matt_work/root_v5.34.14/lib/libCore.so<br>
#9  0x00007f813b000908 in TStreamerInfo::New(void*) () from /home/pooser/matt_work/root_v5.34.14/lib/libRIO.so<br>
#10 0x00007f813b5c63a9 in TClass::New(TClass::ENewType) const () from /home/pooser/matt_work/root_v5.34.14/lib/libCore.so<br>
#11 0x00007f813af5b495 in TKey::ReadObj() () from /home/pooser/matt_work/root_v5.34.14/lib/libRIO.so<br>
#12 0x00007f813af271cc in TDirectoryFile::Get(char const*) () from /home/pooser/matt_work/root_v5.34.14/lib/libRIO.so<br>
#13 0x0000000000403e83 in DalitzDataReader::DalitzDataReader(std::vector&lt;std::string, std::allocator&lt;std::string&gt; &gt; const&amp;) ()<br>
#14 0x0000000000402566 in main ()<br>
<br>
<br>
<br>
<br>
---------------------------------------------------------------------<br>
Matthew Shepherd, Associate Professor<br>
Department of Physics, Indiana University, Swain West 265<br>
727 East Third Street, Bloomington, IN 47405<br>
<br>
Office Phone:  +1 812 856 5808<br>
<br>
<br>
_______________________________________________<br>
Halld-offline mailing list<br>
<a href="mailto:Halld-offline@jlab.org">Halld-offline@jlab.org</a><br>
<a href="https://mailman.jlab.org/mailman/listinfo/halld-offline">https://mailman.jlab.org/mailman/listinfo/halld-offline</a><br>
</blockquote></div><br></div>