Skip to content

Commit

Permalink
Fix ResourceManager Has not Destoryed All Cache (#621)
Browse files Browse the repository at this point in the history
* fix: resourceManager has't destory all cache
  • Loading branch information
gz65555 authored Dec 29, 2021
1 parent 3be72b7 commit cc8f05f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export class Engine extends EventDispatcher {
this._animate = null;

this._sceneManager._activeScene.destroy();
this._resourceManager.gc();
this._resourceManager._destroy();
// If engine destroy, callComponentDestroy() maybe will not call anymore.
this._componentsManager.callComponentDestroy();
this._sceneManager = null;
Expand Down
28 changes: 22 additions & 6 deletions packages/core/src/asset/ResourceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,7 @@ export class ResourceManager {
* @remarks The release principle is that it is not referenced by the components, including direct and indirect reference.
*/
gc(): void {
const objects = ObjectValues(this._refObjectPool);
for (let i = 0, len = objects.length; i < len; i++) {
if (!objects[i].isGCIgnored) {
objects[i].destroy();
}
}
this._gc(false);
}

/**
Expand Down Expand Up @@ -176,6 +171,18 @@ export class ResourceManager {
delete this._refObjectPool[id];
}

/**
* @internal
*/
_destroy(): void {
this.cancelNotLoaded();
this._gc(true);
this._assetPool = null;
this._assetUrlPool = null;
this._refObjectPool = null;
this._loadingPromises = null;
}

private _assignDefaultOptions(assetInfo: LoadItem): LoadItem | never {
assetInfo.type = assetInfo.type ?? ResourceManager._getTypeByUrl(assetInfo.url);
if (assetInfo.type === undefined) {
Expand Down Expand Up @@ -212,6 +219,15 @@ export class ResourceManager {
.catch(() => {});
return promise;
}

private _gc(forceDestroy: boolean): void {
const objects = ObjectValues(this._refObjectPool);
for (let i = 0, len = objects.length; i < len; i++) {
if (!objects[i].isGCIgnored || forceDestroy) {
objects[i].destroy();
}
}
}
}

/**
Expand Down

0 comments on commit cc8f05f

Please sign in to comment.