fixed a memory leak

This commit is contained in:
aap 2020-04-24 22:00:23 +02:00
parent a0a3b077fd
commit 0ddce08c94
5 changed files with 46 additions and 18 deletions

View File

@ -480,7 +480,7 @@ Atomic::setGeometry(Geometry *geo, uint32 flags)
if(this->geometry) if(this->geometry)
this->geometry->destroy(); this->geometry->destroy();
if(geo) if(geo)
geo->refCount++; geo->addRef();
this->geometry = geo; this->geometry = geo;
if(flags & SAMEBOUNDINGSPHERE) if(flags & SAMEBOUNDINGSPHERE)
return; return;

View File

@ -641,7 +641,7 @@ Geometry::removeUnusedMaterials(void)
if(m[i].numIndices <= 0) if(m[i].numIndices <= 0)
continue; continue;
materials[numMaterials] = m[i].material; materials[numMaterials] = m[i].material;
m[i].material->refCount++; m[i].material->addRef();
int32 oldid = this->matList.findIndex(m[i].material); int32 oldid = this->matList.findIndex(m[i].material);
map[oldid] = numMaterials; map[oldid] = numMaterials;
numMaterials++; numMaterials++;
@ -748,7 +748,7 @@ MaterialList::appendMaterial(Material *mat)
this->materials = ml; this->materials = ml;
} }
this->materials[this->numMaterials++] = mat; this->materials[this->numMaterials++] = mat;
mat->refCount++; mat->addRef();
return this->numMaterials-1; return this->numMaterials-1;
} }
@ -786,7 +786,7 @@ MaterialList::streamRead(Stream *stream, MaterialList *matlist)
for(int32 i = 0; i < numMat; i++){ for(int32 i = 0; i < numMat; i++){
if(indices[i] >= 0){ if(indices[i] >= 0){
m = matlist->materials[indices[i]]; m = matlist->materials[indices[i]];
m->refCount++; m->addRef();
}else{ }else{
if(!findChunk(stream, ID_MATERIAL, nil, nil)){ if(!findChunk(stream, ID_MATERIAL, nil, nil)){
RWERROR((ERR_CHUNK, "MATERIAL")); RWERROR((ERR_CHUNK, "MATERIAL"));
@ -907,7 +907,7 @@ Material::setTexture(Texture *tex)
if(this->texture) if(this->texture)
this->texture->destroy(); this->texture->destroy();
if(tex) if(tex)
tex->refCount++; tex->addRef();
this->texture = tex; this->texture = tex;
} }
@ -945,8 +945,7 @@ Material::streamRead(Stream *stream)
RWERROR((ERR_CHUNK, "TEXTURE")); RWERROR((ERR_CHUNK, "TEXTURE"));
goto fail; goto fail;
} }
Texture *t = Texture::streamRead(stream); mat->texture = Texture::streamRead(stream);
mat->setTexture(t);
} }
materialRights[0] = 0; materialRights[0] = 0;

View File

@ -166,7 +166,7 @@ MatFX::setBumpTexture(Texture *t)
this->fx[i].bump.tex->destroy(); this->fx[i].bump.tex->destroy();
this->fx[i].bump.tex = t; this->fx[i].bump.tex = t;
if(t) if(t)
t->refCount++; t->addRef();
} }
} }
@ -205,7 +205,7 @@ MatFX::setEnvTexture(Texture *t)
this->fx[i].env.tex->destroy(); this->fx[i].env.tex->destroy();
this->fx[i].env.tex = t; this->fx[i].env.tex = t;
if(t) if(t)
t->refCount++; t->addRef();
} }
} }
@ -262,7 +262,7 @@ MatFX::setDualTexture(Texture *t)
this->fx[i].dual.tex->destroy(); this->fx[i].dual.tex->destroy();
this->fx[i].dual.tex = t; this->fx[i].dual.tex = t;
if(t) if(t)
t->refCount++; t->addRef();
} }
} }
@ -363,19 +363,19 @@ copyMaterialMatFX(void *dst, void *src, int32 offset, int32)
switch(dstfx->fx[i].type){ switch(dstfx->fx[i].type){
case MatFX::BUMPMAP: case MatFX::BUMPMAP:
if(dstfx->fx[i].bump.bumpedTex) if(dstfx->fx[i].bump.bumpedTex)
dstfx->fx[i].bump.bumpedTex->refCount++; dstfx->fx[i].bump.bumpedTex->addRef();
if(dstfx->fx[i].bump.tex) if(dstfx->fx[i].bump.tex)
dstfx->fx[i].bump.tex->refCount++; dstfx->fx[i].bump.tex->addRef();
break; break;
case MatFX::ENVMAP: case MatFX::ENVMAP:
if(dstfx->fx[i].env.tex) if(dstfx->fx[i].env.tex)
dstfx->fx[i].env.tex->refCount++; dstfx->fx[i].env.tex->addRef();
break; break;
case MatFX::DUAL: case MatFX::DUAL:
if(dstfx->fx[i].dual.tex) if(dstfx->fx[i].dual.tex)
dstfx->fx[i].dual.tex->refCount++; dstfx->fx[i].dual.tex->addRef();
break; break;
} }
return dst; return dst;

View File

@ -282,9 +282,12 @@ struct Texture
uint32 filterAddressing; // VVVVUUUU FFFFFFFF uint32 filterAddressing; // VVVVUUUU FFFFFFFF
int32 refCount; int32 refCount;
LLLink inGlobalList; // actually not in RW
static int32 numAllocated; static int32 numAllocated;
static Texture *create(Raster *raster); static Texture *create(Raster *raster);
void addRef(void) { this->refCount++; }
void destroy(void); void destroy(void);
static Texture *fromDict(LLLink *lnk){ static Texture *fromDict(LLLink *lnk){
return LLLinkGetData(lnk, Texture, inDict); } return LLLinkGetData(lnk, Texture, inDict); }
@ -332,6 +335,7 @@ struct Material
static int32 numAllocated; static int32 numAllocated;
static Material *create(void); static Material *create(void);
void addRef(void) { this->refCount++; }
Material *clone(void); Material *clone(void);
void destroy(void); void destroy(void);
void setTexture(Texture *tex); void setTexture(Texture *tex);
@ -431,6 +435,7 @@ struct Geometry
static int32 numAllocated; static int32 numAllocated;
static Geometry *create(int32 numVerts, int32 numTris, uint32 flags); static Geometry *create(int32 numVerts, int32 numTris, uint32 flags);
void addRef(void) { this->refCount++; }
void destroy(void); void destroy(void);
void lock(int32 lockFlags); void lock(int32 lockFlags);
void unlock(void); void unlock(void);
@ -785,6 +790,7 @@ struct TexDictionary
int32 count(void) { return this->textures.count(); } int32 count(void) { return this->textures.count(); }
void add(Texture *t); void add(Texture *t);
void addFront(Texture *t); void addFront(Texture *t);
void remove(Texture *t);
Texture *find(const char *name); Texture *find(const char *name);
static TexDictionary *streamRead(Stream *stream); static TexDictionary *streamRead(Stream *stream);
void streamWrite(Stream *stream); void streamWrite(Stream *stream);

View File

@ -35,6 +35,8 @@ struct TextureGlobals
// create dummy textures to store just names // create dummy textures to store just names
bool32 makeDummies; bool32 makeDummies;
LinkList texDicts; LinkList texDicts;
LinkList textures;
}; };
int32 textureModuleOffset; int32 textureModuleOffset;
@ -46,6 +48,7 @@ textureOpen(void *object, int32 offset, int32 size)
TexDictionary *texdict; TexDictionary *texdict;
textureModuleOffset = offset; textureModuleOffset = offset;
TEXTUREGLOBAL(texDicts).init(); TEXTUREGLOBAL(texDicts).init();
TEXTUREGLOBAL(textures).init();
texdict = TexDictionary::create(); texdict = TexDictionary::create();
TEXTUREGLOBAL(initialTexDict) = texdict; TEXTUREGLOBAL(initialTexDict) = texdict;
TexDictionary::setCurrent(texdict); TexDictionary::setCurrent(texdict);
@ -60,6 +63,13 @@ textureClose(void *object, int32 offset, int32 size)
TexDictionary::fromLink(lnk)->destroy(); TexDictionary::fromLink(lnk)->destroy();
TEXTUREGLOBAL(initialTexDict) = nil; TEXTUREGLOBAL(initialTexDict) = nil;
TEXTUREGLOBAL(currentTexDict) = nil; TEXTUREGLOBAL(currentTexDict) = nil;
FORLIST(lnk, TEXTUREGLOBAL(textures)){
Texture *tex = LLLinkGetData(lnk, Texture, inGlobalList);
printf("Tex still allocated: %d %s %s\n", tex->refCount, tex->name, tex->mask);
assert(tex->dict == nil);
tex->destroy();
}
return object; return object;
} }
@ -106,10 +116,13 @@ TexDictionary::destroy(void)
{ {
if(TEXTUREGLOBAL(currentTexDict) == this) if(TEXTUREGLOBAL(currentTexDict) == this)
TEXTUREGLOBAL(currentTexDict) = nil; TEXTUREGLOBAL(currentTexDict) = nil;
FORLIST(lnk, this->textures) FORLIST(lnk, this->textures){
Texture::fromDict(lnk)->destroy(); Texture *tex = Texture::fromDict(lnk);
this->inGlobalList.remove(); this->remove(tex);
tex->destroy();
}
s_plglist.destruct(this); s_plglist.destruct(this);
this->inGlobalList.remove();
rwFree(this); rwFree(this);
numAllocated--; numAllocated--;
} }
@ -123,6 +136,14 @@ TexDictionary::add(Texture *t)
this->textures.append(&t->inDict); this->textures.append(&t->inDict);
} }
void
TexDictionary::remove(Texture *t)
{
assert(t->dict == this);
t->inDict.remove();
t->dict = nil;
}
void void
TexDictionary::addFront(Texture *t) TexDictionary::addFront(Texture *t)
{ {
@ -246,6 +267,7 @@ Texture::create(Raster *raster)
tex->filterAddressing = (WRAP << 12) | (WRAP << 8) | NEAREST; tex->filterAddressing = (WRAP << 12) | (WRAP << 8) | NEAREST;
tex->raster = raster; tex->raster = raster;
tex->refCount = 1; tex->refCount = 1;
TEXTUREGLOBAL(textures).add(&tex->inGlobalList);
s_plglist.construct(tex); s_plglist.construct(tex);
return tex; return tex;
} }
@ -260,6 +282,7 @@ Texture::destroy(void)
this->inDict.remove(); this->inDict.remove();
if(this->raster) if(this->raster)
this->raster->destroy(); this->raster->destroy();
this->inGlobalList.remove();
rwFree(this); rwFree(this);
numAllocated--; numAllocated--;
} }
@ -311,7 +334,7 @@ Texture::read(const char *name, const char *mask)
Texture *tex; Texture *tex;
if(tex = Texture::findCB(name), tex){ if(tex = Texture::findCB(name), tex){
tex->refCount++; tex->addRef();
return tex; return tex;
} }
if(TEXTUREGLOBAL(loadTextures)){ if(TEXTUREGLOBAL(loadTextures)){