1

I've encountered a few cases in KDE software where a dynamic_cast of something that's a KPart fails on OS X. I'm not that much of a C++ expert, so I wouldn't even know where to begin to debug such a situation. I've seen this happen with ktimetracker, and the latest (and better discussed) case is with Okular: https://bugs.kde.org/show_bug.cgi?id=345765

In a nutshell: the Okular::Part class inherits the Okular::ViewInterface class (as the last parent in a list). Yet when the code retrieves an Okular::Part* instance (part), dynamic_cast<Okular::ViewInterface*>(part) returns NULL.

The last comment in the BKO ticket above is that using a static cast would be a poor man's fix in this case, but that I should try to figure out why the dynamic_cast fails. Which leads me to 2 questions:

  • what does a dynamic_cast do in addition to a traditional cast here?
  • how would I figure out why it fails? This would be an issue with the runtime, libc++, no?
2
  • To what extent can one reason like (as if) casting structures when casting classes? I suppose that what really counts is the class variables, not (or less so) the class functions?
    – RJVB
    Commented Apr 9, 2015 at 19:13
  • Quick hint that might help when debugging failure: I was having a terrible time debugging why my dynamic cast was failing even though I traced everything and it should have been valid. I also didn't try static_cast because I knew that to be less safe. However, when I switched to static_cast for debugging purposes, the compiler immediately gave me the info I needed to fix. It said that "base class is inaccessible" which I was able to quickly determine I forgot to add "public" keyword during inheritance.
    – mattgately
    Commented Jul 2, 2020 at 15:22

3 Answers 3

3

The answer that allowed me to resolve the issue was provided by Thiago Macieira on the Qt Interest ML. I'm copying it here:

the usual suspect in this kind of problem is the same: your virtual tables are not properly anchored in shared libraries.

Make sure that all classes participating in this party check all these boxes:
* have an export macro in the class declaration
* the primary virtual function is in a .cpp, NEVER inline

The primary virtual function is the first newly overriden function in the parent classes' order or, if none are overridden, the first new virtual. Usually, polymorphic classes have virtual destructors and destructors are always overridden, so this is your best bet.

In this case, the issue was due to the fact that the target class (Okular::ViewerInterface) was not exported on OS X (and probably by default or because of a global compiler switch on Linux). Idem for the KDocumentViewer class, but that might not have been relevant.

1

This means that the object that the pointer is pointing to does not have Okular::Part in its inheritance hierarchy (I'm ignoring certain edge cases involving multiple inheritance). Therefore, the dynamic cast fails.

That's what it means. It doesn't really help you in understanding the originally referenced bug, though.

12
  • Ahhh class Okular::Part : public KParts::ReadWritePart, public Okular::DocumentObserver, public KDocumentViewer, public Okular::ViewerInterface {} but the instance the code tries do dynamic_cast is a KParts::ReadWritePart* . Which is not in any way I can see a descendant of Okular::ViewInterface . If I understand you correctly that would explain the issue?
    – RJVB
    Commented Apr 6, 2015 at 21:50
  • If that's the case, the question (almost) becomes: why does this not fail on Linux? I didn't mention this in my OP, but it works there, with gcc and clang. Which also explains why I thought of libc++: it's the only relevant thing that's different I can think of. But then that's the runtime that does the checking, I presume?
    – RJVB
    Commented Apr 6, 2015 at 21:54
  • Yes, dynamic_cast is evaluated at runtime. However, note that one way this dynamic cast could succeed would be if a different class subclasses both Okular::ViewInterface, and the class the dynamic_cast is from. Commented Apr 6, 2015 at 22:13
  • Okular::Part would be such a class and I can see how you can cast it to either of the parent classes it is "merged from". But why would it be possible to cast between 2 unrelated parent classes?
    – RJVB
    Commented Apr 6, 2015 at 22:52
  • I didn't say "unrelated". But if some class inherits from both Okula::ViewInterface, and Okular::Part, and you have an instance of such class, you can dynamic_cast the instance from that class from one to another. In this case these two classes are not completely unrelated, they have a common subclass. Commented Apr 6, 2015 at 23:21
1

what does a dynamic_cast do in addition to a traditional cast here?

It performs a runtime check that the conversion is valid; that the pointer really does point to an object of the correct type.

how would I figure out why it fails?

If Part really is derived from ViewInterface, then it can't fail if the pointer points to a valid Part object. So the object must have been corrupted or destroyed. A dynamic analysis tool like Valgrind can help diagnose this kind of problem.

This would be an issue with the runtime, libc++, no?

Almost certainly not. It's most likely an issue with managing the lifetime of the object, ending up with a dangling pointer. Or it might be a bug somewhere else corrupting the object so it no longer contains valid RTTI information. Or, since there seem to be threads involved, perhaps it's being shared without adequate synchronisation.

static cast would be a poor man's fix

It wouldn't fix anything. You'd just get a different flavour of undefined behaviour from accessing the invalid object in a different way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.