Repository: 4ian/GDevelop
File: GDJS/Runtime/pixi-renderers/pixi-image-manager.ts (TypeScript)

Code before:
      this._loadedTextures.clear();

      const threeTextures: THREE.Texture[] = [];
      this._loadedThreeTextures.values(threeTextures);
      this._loadedThreeTextures.clear();
      for (const threeTexture of threeTextures) {
        threeTexture.dispose();
      }

      const threeMaterials: THREE.Material[] = [];
      this._loadedThreeMaterials.values(threeMaterials);
      this._loadedThreeMaterials.clear();
      for (const threeMaterial of threeMaterials) {
        threeMaterial.dispose();
      }

      const diskPixiTextures = [...this._diskTextures.values()];
      this._diskTextures.clear();
      for (const pixiTexture of diskPixiTextures) {
        if (pixiTexture.destroyed) {
          continue;
        }

        pixiTexture.destroy();
      }

      const rectanglePixiTextures = [...this._rectangleTextures.values()];
      this._rectangleTextures.clear();
      for (const pixiTexture of rectanglePixiTextures) {
        if (pixiTexture.destroyed) {
          continue;
        }

        pixiTexture.destroy();
      }

      const scaledPixiTextures = [...this._scaledTextures.values()];
      this._scaledTextures.clear();
      for (const pixiTexture of scaledPixiTextures) {
        if (pixiTexture.destroyed) {
          continue;
        }

        pixiTexture.destroy();
      }
    }
  }

  //Register the class to let the engine use it.
  export const ImageManager = gdjs.PixiImageManager;
  export type ImageManager = gdjs.PixiImageManager;

Review comment (suggestion):
Will this work?
```
      for (const pixiTexture of this._diskTextures.values()) {
        if (pixiTexture.destroyed) {
          continue;
        }
        pixiTexture.destroy();
      }
      this._diskTextures.clear();
```
Minus new array creation.

Code after:
     * To be called when the game is disposed.
     * Clear caches of loaded textures and materials.
     */
    dispose(): void {
      this._loadedTextures.clear();

      const threeTextures: THREE.Texture[] = [];
      this._loadedThreeTextures.values(threeTextures);
      this._loadedThreeTextures.clear();
      for (const threeTexture of threeTextures) {
        threeTexture.dispose();
      }

      const threeMaterials: THREE.Material[] = [];
      this._loadedThreeMaterials.values(threeMaterials);
      this._loadedThreeMaterials.clear();
      for (const threeMaterial of threeMaterials) {
        threeMaterial.dispose();
      }

      for (const pixiTexture of this._diskTextures.values()) {
        if (pixiTexture.destroyed) {
          continue;
        }

        pixiTexture.destroy();
      }
      this._diskTextures.clear();

      for (const pixiTexture of this._rectangleTextures.values()) {
        if (pixiTexture.destroyed) {
          continue;
        }

        pixiTexture.destroy();
      }
      this._rectangleTextures.clear();

      for (const pixiTexture of this._scaledTextures.values()) {
        if (pixiTexture.destroyed) {
          continue;
        }

        pixiTexture.destroy();
      }
      this._scaledTextures.clear();
    }
  }

  //Register the class to let the engine use it.
  export const ImageManager = gdjs.PixiImageManager;