Skip to content

Exception when using CMAKE_CXX_VISIBILITY_PRESET=hidden #1919

@darbyjohnston

Description

@darbyjohnston

When using the option CMAKE_CXX_VISIBILITY_PRESET=hidden, an exception can be thrown when using metadata in an application. It seems that classes created in the application can have different type information than the classes in OTIO, causing an exception when serializing the OTIO file:

libc++abi: terminating due to uncaught exception of type std::bad_any_cast: bad any cast

This only seems to happen with some compilers (clang), and when OTIO is linked as a shared library.

Example:

#include <opentimelineio/marker.h>
#include <opentimelineio/timeline.h>

int main(int argc, char** argv)
{
    OTIO_NS::SerializableObject::Retainer<OTIO_NS::Timeline> timeline(new OTIO_NS::Timeline);
    OTIO_NS::SerializableObject::Retainer<OTIO_NS::Track> track(new OTIO_NS::Track);
    timeline->tracks()->append_child(track);
    OTIO_NS::SerializableObject::Retainer<OTIO_NS::Marker> marker(new OTIO_NS::Marker);

    //
    // This AnyDictionary will have different type information than OTIO.
    //
    OTIO_NS::AnyDictionary md;
    md["type"] = static_cast<int64_t>(23);
    marker->metadata()["kdenlive"] = md;
    track->markers().push_back(marker);

    //
    // std::bad_any_cast will be thrown when serializing the file with -DCMAKE_CXX_VISIBILITY_PRESET=hidden
    //
    if (!timeline->to_json_file("test.otio"))
    {
        return 1;
    }
    return 0;
}

Example CMake file:

cmake_minimum_required(VERSION 3.3)
project(test)

set(CMAKE_CXX_STANDARD 17)

find_package(OpenTimelineIO REQUIRED)

set(CMAKE_CXX_VISIBILITY_PRESET hidden)

add_executable(test test.cpp)
target_link_libraries(test OTIO::opentimelineio)

Adding __attribute__((__visibility__("default"))) to the AnyDictionary class seems to fix the example.

Related:
https://developers.redhat.com/articles/2021/10/27/compiler-option-hidden-visibility-and-weak-symbol-walk-bar#the_llvm_package_build
https://cmake.org/cmake/help/latest/module/GenerateExportHeader.html

Thanks to @jlskuz for troubleshooting the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA problem, flaw, or broken functionality.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions