Skip to content

Commit f89dffd

Browse files
authored
[FIX] Fixed double dereferencing of mesh (playcanvas#2027)
* [FIX] Mesh ref counting fix
1 parent f591bbe commit f89dffd

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/scene/mesh-instance.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Object.assign(pc, function () {
6161

6262
this.node = node; // The node that defines the transform of the mesh instance
6363
this._mesh = mesh; // The mesh that this instance renders
64-
mesh._refCount++;
64+
mesh.incReference();
6565
this.material = material; // The material with which to render this instance
6666

6767
this._shaderDefs = pc.MASK_DYNAMIC << 16; // 2 byte toggles, 2 bytes light mask; Default value is no toggles and mask = pc.MASK_DYNAMIC
@@ -118,9 +118,9 @@ Object.assign(pc, function () {
118118
return this._mesh;
119119
},
120120
set: function (mesh) {
121-
if (this._mesh) this._mesh._refCount--;
121+
if (this._mesh) this._mesh.decReference();
122122
this._mesh = mesh;
123-
if (mesh) mesh._refCount++;
123+
if (mesh) mesh.incReference();
124124
}
125125
});
126126

src/scene/mesh.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,22 @@ Object.assign(pc, function () {
177177
}
178178
});
179179

180+
Object.defineProperty(Mesh.prototype, 'refCount', {
181+
get: function () {
182+
return this._refCount;
183+
}
184+
});
185+
180186
Object.assign(Mesh.prototype, {
187+
188+
incReference: function () {
189+
this._refCount++;
190+
},
191+
192+
decReference: function () {
193+
this._refCount--;
194+
},
195+
181196
/**
182197
* @function
183198
* @name pc.Mesh#destroy

src/scene/model.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,11 @@ Object.assign(pc, function () {
171171

172172
mesh = meshInstance.mesh;
173173
if (mesh) {
174-
mesh._refCount--;
175-
if (mesh._refCount < 1) {
174+
meshInstance.mesh = null; // this calls decReference on mesh
175+
if (mesh.refCount < 1) {
176176
mesh.destroy();
177177
}
178178
}
179-
meshInstance.mesh = null;
180179

181180
skin = meshInstance.skinInstance;
182181
if (skin) {

0 commit comments

Comments
 (0)