SkRefCntBase how to obtain current ref count

15 views
Skip to first unread message

Clark Kent

unread,
Sep 8, 2022, 5:28:32 AM9/8/22
to skia-discuss
SkRefCntBase::getRefCount has silently been removed and has not been documented in release notes

how would one go about obtaining the current reference count?

Clark Kent

unread,
Sep 8, 2022, 5:34:58 AM9/8/22
to skia-discuss
as skiasharp uses the reference count when compiling as debug build to detect if a native object that has no references is about to be unref'd





if (GetInstanceNoLocks<TSkiaObject> (handle, out var instance)) {
    // some object get automatically referenced on the native side,
    // but managed code just has the same reference
    if (unrefExisting && instance is ISKReferenceCounted refcnt) {
#if THROW_OBJECT_EXCEPTIONS
        if (refcnt.GetReferenceCount () == 1)
            throw new InvalidOperationException (
                $"About to unreference an object that has no references. " +
                $"H: {handle.ToString ("x")} Type: {instance.GetType ()}");
#endif
        refcnt.SafeUnRef ();
    }

    return instance;

Clark Kent

unread,
Sep 8, 2022, 5:41:59 AM9/8/22
to skia-discuss
and it does not look like we can use SK_REF_CNT_MIXIN_INCLUDE to expose the fRefCnt since SkRefCntBase has fRefCnt as private

Michael Ludwig

unread,
Sep 8, 2022, 10:32:21 AM9/8/22
to skia-discuss
If I'm following their code correctly, they could just call SkRefCntBase::unique() instead of needing the actual ref count. Given the thread safety requirements of the ref count atomic, that seems like the safer function call than getRefCnt() anyways.

Greg Daniel

unread,
Sep 8, 2022, 10:35:08 AM9/8/22
to skia-discuss
Yes we do not want to expose getRefCnt to clients. In general clients should not be debugging Skia's ref counting or ever need to care what its value is. Clients should just correctly ref and unref things and trust that Skia will call the dtor/free things when refs hit zero. Clients should not be holding bare pointers to objects that they don't know if they own a ref to or not.

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/00999620-a800-4d36-9aae-1cab2958933dn%40googlegroups.com.
Message has been deleted

Clark Kent

unread,
Sep 8, 2022, 10:54:09 PM9/8/22
to skia-discuss
the comments in the code suggest that it will block until there are no more references to it



    /** May return true if the caller is the only owner.
     *  Ensures that all previous owner's actions are complete.
     */
    bool unique() const {
        if (1 == fRefCnt.load(std::memory_order_acquire)) {
            // The acquire barrier is only really needed if we return true.  It
            // prevents code conditioned on the result of unique() from running
            // until previous owners are all totally done calling unref().
            return true;
        }
        return false;
    }

Clark Kent

unread,
Sep 8, 2022, 10:55:05 PM9/8/22
to skia-discuss
futhermore skiasharp checks for zero ref, not 1 ref

Clark Kent

unread,
Sep 8, 2022, 10:55:34 PM9/8/22
to skia-discuss
alright :)
Reply all
Reply to author
Forward
0 new messages