Skip to content

Commit 48fbad4

Browse files
committed
fix(screenshot): destroy late texture in CaptureCompositedAfterFrame callback
If the editor spin loop times out before ScreenshotCapturer's coroutine fires the callback, the callback would still later assign the captured Texture2D to the (now-dead) local result variable. The texture itself would never reach the consumer's finally-block DestroyTexture, leaking a Unity object until the next domain reload. Track caller-returned state; if the callback runs after timeout, destroy the incoming texture immediately instead of assigning it. Flagged by CodeRabbit on the prior commit.
1 parent 0c277af commit 48fbad4

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

MCPForUnity/Runtime/Helpers/ScreenshotUtility.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,24 @@ private static Texture2D CaptureCompositedAfterFrame(int superSize, int timeoutS
243243
{
244244
Texture2D result = null;
245245
bool done = false;
246-
ScreenshotCapturer.Begin(superSize, tex => { result = tex; done = true; });
246+
bool callerReturned = false;
247+
ScreenshotCapturer.Begin(superSize, tex =>
248+
{
249+
// Late completion after the spin loop timed out: caller will never consume
250+
// the texture, so destroy it here to avoid leaking a Unity object.
251+
if (callerReturned)
252+
{
253+
if (tex != null) DestroyTexture(tex);
254+
return;
255+
}
256+
result = tex;
257+
done = true;
258+
});
247259
for (int i = 0; i < timeoutSteps && !done; i++)
248260
{
249261
UnityEditor.EditorApplication.Step();
250262
}
263+
callerReturned = true;
251264
return result;
252265
}
253266
#endif

0 commit comments

Comments
 (0)