[Halld-offline] strange casting problem
Matthew Shepherd
mashephe at indiana.edu
Thu Mar 6 14:58:06 EST 2014
To revive this thread and add another wrinkle...
Yes, the dynamic_cast clearly is the more proper way to go as Richard points out, and it fixes Eric's problem.
However, now when I switch to dynamic_cast, which I try to comple and run the code on my Mac using gcc 4.6.2, the dynamic_cast fails with a segfault and gdb trace says:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000010
0x00007fff932047e4 in __cxxabiv1::__vmi_class_type_info::has_unambiguous_public_base ()
(gdb) where
#0 0x00007fff932047e4 in __cxxabiv1::__vmi_class_type_info::has_unambiguous_public_base ()
#1 0x000000010325d232 in __dynamic_cast ()
Then, switching back to static_cast works perfectly fine (but would presumably regenerate the segfault problem on Eric's platform). Something strange is going on here..... I'm not sure how to win this one.
Matt
---------------------------------------------------------------------
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
On Feb 8, 2014, at 10:40 AM, Richard Jones <richard.t.jones at uconn.edu> wrote:
> 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
>
More information about the Halld-offline
mailing list