Skip to content

Commit 61fbbc5

Browse files
author
Anton Ivanov
committed
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
1 parent 78a579c commit 61fbbc5

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

libs/nativedisplay/AChoreographer.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,21 +238,25 @@ int64_t AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos(
238238
}
239239

240240
AChoreographer* AChoreographer_create() {
241-
Choreographer* choreographer = new Choreographer(nullptr);
241+
// Increments default strongRef count on construction, will be decremented on
242+
// function exit.
243+
auto choreographer = sp<Choreographer>::make(nullptr);
242244
status_t result = choreographer->initialize();
243245
if (result != OK) {
244246
ALOGW("Failed to initialize");
245247
return nullptr;
246248
}
247-
return Choreographer_to_AChoreographer(choreographer);
249+
// Will be decremented and destroyed by AChoreographer_destroy
250+
choreographer->incStrong((void*)AChoreographer_create);
251+
return Choreographer_to_AChoreographer(choreographer.get());
248252
}
249253

250254
void AChoreographer_destroy(AChoreographer* choreographer) {
251255
if (choreographer == nullptr) {
252256
return;
253257
}
254258

255-
delete AChoreographer_to_Choreographer(choreographer);
259+
AChoreographer_to_Choreographer(choreographer)->decStrong((void*)AChoreographer_create);
256260
}
257261

258262
int AChoreographer_getFd(const AChoreographer* choreographer) {

0 commit comments

Comments
 (0)