Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 61fbbc52 authored by Anton Ivanov's avatar Anton Ivanov
Browse files

Use refcounts to manage lifetime of AChoreographer*.

This is more robust then existing new/delete.  In particular, current
code has a leak in AChoreographer_create in case initialize() is not
successful.

Bug: 393217449
Test: presubmit
Flag: EXEMPT_refactor
Change-Id: Ib3d84ee58d953a332b14fd852b53b3507defe74f
parent 78a579cb
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -238,13 +238,17 @@ int64_t AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos(
}

AChoreographer* AChoreographer_create() {
    Choreographer* choreographer = new Choreographer(nullptr);
    // Increments default strongRef count on construction, will be decremented on
    // function exit.
    auto choreographer = sp<Choreographer>::make(nullptr);
    status_t result = choreographer->initialize();
    if (result != OK) {
        ALOGW("Failed to initialize");
        return nullptr;
    }
    return Choreographer_to_AChoreographer(choreographer);
    // Will be decremented and destroyed by AChoreographer_destroy
    choreographer->incStrong((void*)AChoreographer_create);
    return Choreographer_to_AChoreographer(choreographer.get());
}

void AChoreographer_destroy(AChoreographer* choreographer) {
@@ -252,7 +256,7 @@ void AChoreographer_destroy(AChoreographer* choreographer) {
        return;
    }

    delete AChoreographer_to_Choreographer(choreographer);
    AChoreographer_to_Choreographer(choreographer)->decStrong((void*)AChoreographer_create);
}

int AChoreographer_getFd(const AChoreographer* choreographer) {