Skip to content

fix: balance Texture2D image dependency references - #267

Merged
star-e merged 5 commits into
cocos:v4.0.0from
Changdw:fix/texture2d-image-refcount
Aug 19, 2026
Merged

fix: balance Texture2D image dependency references#267
star-e merged 5 commits into
cocos:v4.0.0from
Changdw:fix/texture2d-image-refcount

Conversation

@Changdw

@Changdw Changdw commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Re: https://forum.cocos.org/t/topic/176993

Changelog

  • Fixed an ImageAsset reference-count leak when a Texture2D preload dependency and _mipmaps dependency reference the same image.
  • After texture upload, removes all dependency entries matching the image UUID and calls decRef() once for each removed dependency edge.
  • Kept the cleanup logic consistent between Web and JSB.
  • Added a focused Texture2D regression test to verify that duplicate dependency edges are fully released.

Continuous Integration

This pull request:

  • needs automatic test cases check.

    Manual trigger with @cocos-robot run test cases afterward.

  • does not change any runtime related code or build configuration

    If any reviewer thinks the CI checks are needed, please uncheck this option, then close and reopen the issue.


Compatibility Check

This pull request:

  • changes public API, and have ensured backward compatibility with deprecated features.
  • affects platform compatibility, e.g. system version, browser version, platform sdk version, platform toolchain, language version, hardware compatibility etc.
  • affects file structure of the build package or build configuration which requires user project upgrade.
  • introduces breaking changes, please list all changes, affected features and the scope of violation.

@Changdw
Changdw requested a review from star-e August 14, 2026 02:32
@github-actions

github-actions Bot commented Aug 14, 2026

Copy link
Copy Markdown

Code Size Check Report

Wechat (WASM) Before After Diff
2D Empty (legacy pipeline) 1015269 bytes 1015290 bytes ⚠️ +21 bytes
2D All (legacy pipeline) 2683000 bytes 2683021 bytes ⚠️ +21 bytes
2D All (new pipeline) 2774955 bytes 2774976 bytes ⚠️ +21 bytes
(2D + 3D) All 10032397 bytes 10032418 bytes ⚠️ +21 bytes
Web (WASM + ASMJS) Before After Diff
(2D + 3D) All 16868815 bytes 16868836 bytes ⚠️ +21 bytes

Interface Check Report

This pull request does not change any public interfaces !

@github-actions

Copy link
Copy Markdown

@Changdw, Please check the result of run test cases:

Task Details

@github-actions

Copy link
Copy Markdown

@Changdw, Please check the result of run test cases:

Task Details

@Changdw

Changdw commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

概述

本次修改修复了 Texture2D 的预加载依赖与 _mipmaps 依赖指向同一 ImageAsset 时,上传完成后引用计数残留的问题。

根因

setProperties() 会分别为预加载依赖和 _mipmaps[i] 的纹理依赖建立引用。
当两条依赖指向同一个 ImageAsset 时,依赖表中会存在重复 UUID。现有上传后的清理逻辑只移除第一条匹配依赖,并执行一次 decRef();另一条依赖边及其对应引用未被清理,导致 ImageAsset 引用计数残留。

修复方案

  • 在图片上传完成后的既有清理路径中,查找当前图片 UUID 的全部匹配依赖边。
  • 每移除一条匹配依赖边,执行一次对应的 image.decRef()。
  • 依赖边的移除次数与加载阶段对该图片建立的引用次数保持一致。
  • 同步 Web 与 JSB 的清理逻辑。
  • 添加聚焦的 Texture2D 回归测试,验证重复依赖边会被完整清理。
    对于两条重复依赖边,引用计数生命周期由原来的:
    N → N + 2 → N + 1
    修复为:
    N → N + 2 → N
    其中 N 为加载前已有的引用数。

兼容性

  • 改动仅处理同一图片 UUID 的重复依赖边,不引入额外的资源生命周期管理逻辑。

测试方法

以下是一个我做的冒烟测试 demo. 具体测试说明见demo中的文档.
微信小游戏中去测试, console log 最后输出
PASS 6 / WARN 1 / FAIL 0,且唯一 WARN 是 PNG fallback 时,可判定“微信开发者工具基础冒烟通过”。

texture2d-refcount-smoke-400.zip

@Changdw

Changdw commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

需要backport到3.8.9

@github-actions

Copy link
Copy Markdown

@Changdw, Please check the result of run test cases:

Task Details

@github-actions

Copy link
Copy Markdown

@Changdw, Please check the result of run test cases:

Task Details

@github-actions

Copy link
Copy Markdown

@Changdw, Please check the result of run test cases:

Task Details

@github-actions

Copy link
Copy Markdown

@Changdw, Please check the result of run test cases:

Task Details

@star-e
star-e merged commit ff436d6 into cocos:v4.0.0 Aug 19, 2026
14 checks passed
@Changdw

Changdw commented Aug 19, 2026

Copy link
Copy Markdown
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants