Skip to content

Commit

Permalink
Fix memory leak issues in PAGViewController and PAGImageViewControlle…
Browse files Browse the repository at this point in the history
…r on ohos. (#2667)
  • Loading branch information
Hparty authored Jan 17, 2025
1 parent 944cfd2 commit 594fec6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
10 changes: 6 additions & 4 deletions ohos/libpag/src/main/ets/PAGImageView.ets
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { PAGScaleMode } from './PAGScaleMode';
import { PAGFile } from './PAGFile';
import { PAGAnimatorState } from './private/PAGAnimatorState';
import { PAGUtils } from './private/PAGUtils';
import { PAGViewCallbackHandler } from './private/PAGViewCallbackHandler'

export interface PAGImageViewListener {
/**
Expand Down Expand Up @@ -274,8 +275,9 @@ export class PAGImageViewController {
}

constructor() {
this.jImageView.setStateChangeCallback(this.onAnimatorStateChange);
this.jImageView.setProgressUpdateCallback(this.onAnimatorProgressUpdate);
let handler = new PAGViewCallbackHandler(this);
this.jImageView.setStateChangeCallback(handler.onStateChange);
this.jImageView.setProgressUpdateCallback(handler.onProgressUpdate);
}

private onAnimationStart() {
Expand Down Expand Up @@ -338,7 +340,7 @@ export class PAGImageViewController {
private listeners: Array<WeakRef<PAGImageViewListener>> = [];
private view: WeakRef<PAGImageView> | null = null;
private jImageView: JPAGImageView = new JPAGImageView();
private onAnimatorStateChange = (state: number): void => {
readonly onAnimatorStateChange = (state: number): void => {
switch (state) {
case PAGAnimatorState.Start:
return this.onAnimationStart();
Expand All @@ -350,7 +352,7 @@ export class PAGImageViewController {
return this.onAnimationRepeat();
}
}
private onAnimatorProgressUpdate = (): void => {
readonly onAnimatorProgressUpdate = (): void => {
for (const weakListener of this.listeners) {
const listener = weakListener.deref();
if (listener && listener.onAnimationUpdate) {
Expand Down
10 changes: 6 additions & 4 deletions ohos/libpag/src/main/ets/PAGView.ets
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { Matrix4 } from '@ohos.arkui.node';
import { PAGAnimatorState } from './private/PAGAnimatorState';
import { PAGFile } from './PAGFile';
import { common2D } from '@kit.ArkGraphics2D';
import { PAGViewCallbackHandler } from './private/PAGViewCallbackHandler';

export interface PAGViewListener {
/**
Expand Down Expand Up @@ -392,8 +393,9 @@ export class PAGViewController {
}

constructor() {
this.jView.setStateChangeCallback(this.onAnimatorStateChange);
this.jView.setProgressUpdateCallback(this.onAnimatorProgressUpdate);
let handler = new PAGViewCallbackHandler(this);
this.jView.setStateChangeCallback(handler.onStateChange);
this.jView.setProgressUpdateCallback(handler.onProgressUpdate);
}

private onAnimationStart() {
Expand Down Expand Up @@ -432,7 +434,7 @@ export class PAGViewController {
}
}

private onAnimatorStateChange = (state: number): void => {
readonly onAnimatorStateChange = (state: number): void => {
switch (state) {
case PAGAnimatorState.Start:
return this.onAnimationStart();
Expand All @@ -444,7 +446,7 @@ export class PAGViewController {
return this.onAnimationRepeat();
}
}
private onAnimatorProgressUpdate = (): void => {
readonly onAnimatorProgressUpdate = (): void => {
for (const weakListener of this.listeners) {
const listener = weakListener.deref();
if (listener && listener.onAnimationUpdate) {
Expand Down
41 changes: 41 additions & 0 deletions ohos/libpag/src/main/ets/private/PAGViewCallbackHandler.ets
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making libpag available.
//
// Copyright (C) 2025 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// unless required by applicable law or agreed to in writing, software distributed under the
// license is distributed on an "as is" basis, without warranties or conditions of any kind,
// either express or implied. see the license for the specific language governing permissions
// and limitations under the license.
//
/////////////////////////////////////////////////////////////////////////////////////////////////

import { PAGImageViewController } from '../PAGImageView'
import { PAGViewController } from '../PAGView'

export class PAGViewCallbackHandler {
constructor(viewController: PAGViewController | PAGImageViewController) {
this.weakViewController = new WeakRef(viewController);
}

onProgressUpdate = () => {
let viewController = this.weakViewController.deref();
if (viewController) {
viewController.onAnimatorProgressUpdate();
}
}

onStateChange = (state: number) => {
let viewController = this.weakViewController.deref();
if (viewController) {
viewController.onAnimatorStateChange(state);
}
}
private weakViewController: WeakRef<PAGViewController | PAGImageViewController>;
}

0 comments on commit 594fec6

Please sign in to comment.