diff --git a/src/realm/object-store/c_api/types.hpp b/src/realm/object-store/c_api/types.hpp index 2cefc41b0db..a0749afd512 100644 --- a/src/realm/object-store/c_api/types.hpp +++ b/src/realm/object-store/c_api/types.hpp @@ -106,7 +106,16 @@ struct PropertyTypeMismatch : std::logic_error { //// FIXME: END EXCEPTIONS THAT SHOULD BE MOVED INTO OBJECT STORE struct WrapC { - virtual ~WrapC() {} + static constexpr uint64_t s_cookie_value = 0xdeadbeefdeadbeef; + uint64_t cookie; + WrapC() + : cookie(s_cookie_value) + { + } + virtual ~WrapC() + { + cookie = 0; + } virtual WrapC* clone() const { diff --git a/src/realm/object-store/c_api/util.cpp b/src/realm/object-store/c_api/util.cpp index f156424bc35..15eb0affddd 100644 --- a/src/realm/object-store/c_api/util.cpp +++ b/src/realm/object-store/c_api/util.cpp @@ -3,20 +3,18 @@ namespace realm::c_api { -template -inline T* cast_ptr(void* ptr) +inline WrapC* cast_ptr(void* ptr) { auto rptr = static_cast(ptr); - REALM_ASSERT(dynamic_cast(rptr) != nullptr); - return static_cast(rptr); + REALM_ASSERT(rptr->cookie == WrapC::s_cookie_value); + return rptr; } -template -inline const T* cast_ptr(const void* ptr) +inline const WrapC* cast_const_ptr(const void* ptr) { auto rptr = static_cast(ptr); - REALM_ASSERT(dynamic_cast(rptr) != nullptr); - return static_cast(rptr); + REALM_ASSERT(rptr->cookie == WrapC::s_cookie_value); + return rptr; } RLM_API void realm_free(void* buffer) @@ -30,19 +28,19 @@ RLM_API void realm_release(void* ptr) { if (!ptr) return; - delete cast_ptr(ptr); + delete cast_ptr(ptr); } RLM_API void* realm_clone(const void* ptr) { return wrap_err([=]() { - return cast_ptr(ptr)->clone(); + return cast_const_ptr(ptr)->clone(); }); } RLM_API bool realm_is_frozen(const void* ptr) { - return cast_ptr(ptr)->is_frozen(); + return cast_const_ptr(ptr)->is_frozen(); } RLM_API bool realm_equals(const void* a, const void* b)