[Halld-offline] strange casting problem
Richard Jones
richard.t.jones at uconn.edu
Sat Feb 8 10:40:39 EST 2014
Hello Matt,
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.
-Richard Jones
On Thu, Feb 6, 2014 at 1:15 PM, Matthew Shepherd <mashephe at indiana.edu>wrote:
>
> Dear C++ gurus,
>
> I'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't know if that is relevant).
>
> The problem is that the following (relatively standard) code:
>
> TTree* m_inTree = static_cast<TTree*>( m_inFile->Get( inTreeName.c_str() )
> );
>
> Results in a segfault (ROOT-reported stack trace pasted below).
>
> However if I change:
>
> TTree* m_inTree = dynamic_cast<TTree*>( m_inFile->Get( inTreeName.c_str()
> ) );
>
> 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?
>
> 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?
>
> Matt
>
>
>
> There was a crash.
> This is the entire stack trace of all threads:
> ===========================================================
> #0 0x00007f8139a10c8e in __libc_waitpid (pid=<optimized out>,
> stat_loc=0x7fff918fa540, options=0) at
> ../sysdeps/unix/sysv/linux/waitpid.c:32
> #1 0x00007f813999629e in do_system (line=0x24f0ec0
> "/home/pooser/matt_work/root_v5.34.14/etc/gdb-backtrace.sh 32076 1>&2") at
> ../sysdeps/posix/system.c:149
> #2 0x00007f813b5f3337 in TUnixSystem::StackTrace() () from
> /home/pooser/matt_work/root_v5.34.14/lib/libCore.so
> #3 0x00007f813b5f5c23 in TUnixSystem::DispatchSignals(ESignals) () from
> /home/pooser/matt_work/root_v5.34.14/lib/libCore.so
> #4 <signal handler called>
> #5 0x00007f813b5aa983 in TObjArray::Init(int, int) () from
> /home/pooser/matt_work/root_v5.34.14/lib/libCore.so
> #6 0x00007f813b5aad82 in TObjArray::TObjArray(int, int) () from
> /home/pooser/matt_work/root_v5.34.14/lib/libCore.so
> #7 0x00007f813b85f4b5 in ROOT::new_TObjArray(void*) () from
> /home/pooser/matt_work/root_v5.34.14/lib/libCore.so
> #8 0x00007f813b5c5ee4 in TClass::New(void*, TClass::ENewType) const ()
> from /home/pooser/matt_work/root_v5.34.14/lib/libCore.so
> #9 0x00007f813b000908 in TStreamerInfo::New(void*) () from
> /home/pooser/matt_work/root_v5.34.14/lib/libRIO.so
> #10 0x00007f813b5c63a9 in TClass::New(TClass::ENewType) const () from
> /home/pooser/matt_work/root_v5.34.14/lib/libCore.so
> #11 0x00007f813af5b495 in TKey::ReadObj() () from
> /home/pooser/matt_work/root_v5.34.14/lib/libRIO.so
> #12 0x00007f813af271cc in TDirectoryFile::Get(char const*) () from
> /home/pooser/matt_work/root_v5.34.14/lib/libRIO.so
> #13 0x0000000000403e83 in
> DalitzDataReader::DalitzDataReader(std::vector<std::string,
> std::allocator<std::string> > const&) ()
> #14 0x0000000000402566 in main ()
>
>
>
>
> ---------------------------------------------------------------------
> Matthew Shepherd, Associate Professor
> Department of Physics, Indiana University, Swain West 265
> 727 East Third Street, Bloomington, IN 47405
>
> Office Phone: +1 812 856 5808
>
>
> _______________________________________________
> Halld-offline mailing list
> Halld-offline at jlab.org
> https://mailman.jlab.org/mailman/listinfo/halld-offline
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://mailman.jlab.org/pipermail/halld-offline/attachments/20140208/e0eb8a2b/attachment.html
More information about the Halld-offline
mailing list