Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition of private helper debug only function to help guard against destroyed objects being used. #5194

Merged
merged 1 commit into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ const moduleOptions = buildType => ({
const stripFunctions = [
'Debug.assert',
'Debug.assertDeprecated',
'Debug.assertDestroyed',
'Debug.call',
'Debug.deprecated',
'Debug.warn',
Expand All @@ -265,6 +266,7 @@ const stripFunctions = [
'Debug.trace',
'DebugHelper.setName',
'DebugHelper.setLabel',
`DebugHelper.setDestroyed`,
'DebugGraphics.toString',
'DebugGraphics.clearGpuMarkers',
'DebugGraphics.pushGpuMarker',
Expand Down
27 changes: 27 additions & 0 deletions src/core/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ class Debug {
}
}

/**
* Assertion error message that writes an error message to the log if the object has already
* been destroyed. To be used along setDestroyed.
*
* @param {object} object - The object to check.
*/
static assertDestroyed(object) {
if (object?.__alreadyDestroyed) {
const message = `[${object.constructor?.name}] with name [${object.name}] has already been destroyed, and cannot be used.`;
if (!Debug._loggedMessages.has(message)) {
Debug._loggedMessages.add(message);
console.error('ASSERT FAILED: ', message, object);
}
}
}

/**
* Executes a function in debug mode only.
*
Expand Down Expand Up @@ -169,6 +185,17 @@ class DebugHelper {
object.label = label;
}
}

/**
* Marks object as destroyed. Executes only in the debug build. To be used along assertDestroyed.
*
* @param {object} object - The object to mark as destroyed.
*/
static setDestroyed(object) {
if (object) {
object.__alreadyDestroyed = true;
}
}
}

export { Debug, DebugHelper };
5 changes: 4 additions & 1 deletion src/platform/graphics/render-target.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Debug } from '../../core/debug.js';
import { Debug, DebugHelper } from '../../core/debug.js';
import { TRACEID_RENDER_TARGET_ALLOC } from '../../core/constants.js';
import { PIXELFORMAT_DEPTH, PIXELFORMAT_DEPTHSTENCIL } from './constants.js';
import { DebugGraphics } from './debug-graphics.js';
Expand Down Expand Up @@ -167,6 +167,8 @@ class RenderTarget {

this.destroyFrameBuffers();
}

DebugHelper.setDestroyed(this);
}

/**
Expand Down Expand Up @@ -206,6 +208,7 @@ class RenderTarget {
* @ignore
*/
init() {
Debug.assertDestroyed(this);
this.impl.init(this._device, this);
}

Expand Down