Skip to content

Commit

Permalink
Fix various memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Lactozilla committed Jul 5, 2024
1 parent fedebeb commit 0232e85
Show file tree
Hide file tree
Showing 15 changed files with 205 additions and 103 deletions.
7 changes: 1 addition & 6 deletions source/Engine/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,10 +513,7 @@ PRIVATE STATIC void Application::Restart() {

Scene::Dispose();
SceneInfo::Dispose();
Graphics::SpriteSheetTextureMap->WithAll([](Uint32, Texture* tex) -> void {
Graphics::DisposeTexture(tex);
});
Graphics::SpriteSheetTextureMap->Clear();
Graphics::DeleteSpriteSheetMap();

ScriptManager::LoadAllClasses = false;
ScriptEntity::DisableAutoAnimate = false;
Expand Down Expand Up @@ -893,8 +890,6 @@ PRIVATE STATIC void Application::RunFrame(void* p) {
if (!spriteSheet)
spriteSheet = DEBUG_fontSprite->AddSpriteSheet("Sprites/Fonts/DebugFont.png");
if (spriteSheet) {
DEBUG_fontSprite->Spritesheets.push_back(spriteSheet);

cols = spriteSheet->Width / 32;
rows = spriteSheet->Height / 32;

Expand Down
2 changes: 1 addition & 1 deletion source/Engine/Bytecode/ScriptManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ PUBLIC STATIC void ScriptManager::FreeValue(VMValue value) {

// Free keys
map->Keys->WithAll([](Uint32, char* ptr) -> void {
free(ptr);
Memory::Free(ptr);
});

// Free Keys table
Expand Down
8 changes: 5 additions & 3 deletions source/Engine/FontFace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,10 @@ PUBLIC STATIC ISprite* FontFace::SpriteFromFont(Stream* stream, int pixelSize, c
}

Texture* spriteSheet = Graphics::CreateTextureFromPixels(package->Width, package->Height, pixelData, package->Width * sizeof(Uint32));
if (spriteSheet)
if (spriteSheet) {
sprite->Spritesheets.push_back(spriteSheet);
sprite->SpritesheetFilenames.push_back(std::string(filename));
}

// Add preliminary chars
sprite->AddAnimation("Font", offsetBaseline & 0xFFFF, pixelSize, 0x100);
Expand All @@ -239,7 +241,7 @@ PUBLIC STATIC ISprite* FontFace::SpriteFromFont(Stream* stream, int pixelSize, c

bool exportFonts = false;
Application::Settings->GetBool("dev", "exportFonts", &exportFonts);
if (filename && exportFonts) {
if (exportFonts) {
char* filenameJustName = filename + strlen(filename) - 1;
for (; filenameJustName > filename; filenameJustName--) {
if (*filenameJustName == '/') {
Expand All @@ -250,7 +252,7 @@ PUBLIC STATIC ISprite* FontFace::SpriteFromFont(Stream* stream, int pixelSize, c

char testFilename[4096];
snprintf(testFilename, sizeof testFilename, "Fonts/%s_%d.bmp", filenameJustName, pixelSize);
sprite->SpritesheetsFilenames.push_back(StringUtils::Duplicate(testFilename));
sprite->SpritesheetsFilenames.push_back(std::string(testFilename));

SDL_Surface* surface = SDL_CreateRGBSurfaceFrom(pixelData, package->Width, package->Height, 32, package->Width * 4,
0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
Expand Down
53 changes: 45 additions & 8 deletions source/Engine/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <Engine/Rendering/GraphicsFunctions.h>
#include <Engine/Rendering/Scene3D.h>
#include <Engine/Rendering/VertexBuffer.h>
#include <Engine/Rendering/TextureReference.h>
#include <Engine/Utilities/ColorUtils.h>

need_t ISprite;
Expand All @@ -23,7 +24,7 @@ need_t IModel;
class Graphics {
public:
static HashMap<Texture*>* TextureMap;
static HashMap<Texture*>* SpriteSheetTextureMap;
static map<string, TextureReference*> SpriteSheetTextureMap;
static bool VsyncEnabled;
static int MultisamplingEnabled;
static int FontDPI;
Expand Down Expand Up @@ -113,7 +114,7 @@ class Graphics {
#include <Engine/Bytecode/ScriptManager.h>

HashMap<Texture*>* Graphics::TextureMap = NULL;
HashMap<Texture*>* Graphics::SpriteSheetTextureMap = NULL;
std::map<std::string, TextureReference*> Graphics::SpriteSheetTextureMap;
bool Graphics::VsyncEnabled = true;
int Graphics::MultisamplingEnabled = 0;
int Graphics::FontDPI = 1;
Expand Down Expand Up @@ -183,7 +184,6 @@ const char* Graphics::Renderer = "default";

PUBLIC STATIC void Graphics::Init() {
Graphics::TextureMap = new HashMap<Texture*>(NULL, 32);
Graphics::SpriteSheetTextureMap = new HashMap<Texture*>(NULL, 32);

Graphics::ModelViewMatrix = Matrix4x4::Create();
Graphics::MatrixStack.push(Graphics::ModelViewMatrix);
Expand Down Expand Up @@ -315,19 +315,18 @@ PUBLIC STATIC void Graphics::Dispose() {
for (Uint32 i = 0; i < MAX_3D_SCENES; i++)
Graphics::DeleteScene3D(i);

Graphics::DeleteSpriteSheetMap();

for (Texture* texture = Graphics::TextureHead, *next; texture != NULL; texture = next) {
next = texture->Next;
Graphics::DisposeTexture(texture);
}
Graphics::TextureHead = NULL;
Graphics::PaletteTexture = NULL;

Graphics::SpriteSheetTextureMap->Clear();

Graphics::GfxFunctions->Dispose();

delete Graphics::TextureMap;
delete Graphics::SpriteSheetTextureMap;
while (Graphics::MatrixStack.size()) {
delete Graphics::MatrixStack.top();
Graphics::MatrixStack.pop();
Expand Down Expand Up @@ -464,6 +463,44 @@ PUBLIC STATIC void Graphics::DisposeTexture(Texture* texture) {

Memory::Free(texture);
}
PUBLIC STATIC TextureReference* Graphics::GetSpriteSheet(string sheetPath) {
if (Graphics::SpriteSheetTextureMap.count(sheetPath) != 0) {
TextureReference* textureRef = Graphics::SpriteSheetTextureMap.at(sheetPath);
textureRef->AddRef();
return textureRef;
}

return nullptr;
}
PUBLIC STATIC TextureReference* Graphics::AddSpriteSheet(string sheetPath, Texture* texture) {
TextureReference *ref = Graphics::GetSpriteSheet(sheetPath);
if (ref == nullptr) {
ref = new TextureReference(texture);
Graphics::SpriteSheetTextureMap[sheetPath] = ref;
}
return ref;
}
PUBLIC STATIC void Graphics::DisposeSpriteSheet(string sheetPath) {
if (Graphics::SpriteSheetTextureMap.count(sheetPath) != 0) {
TextureReference* textureRef = Graphics::SpriteSheetTextureMap.at(sheetPath);
if (textureRef->TakeRef()) {
Graphics::DisposeTexture(textureRef->TexturePtr);
Graphics::SpriteSheetTextureMap.erase(sheetPath);
delete textureRef;
}
}
}
PUBLIC STATIC void Graphics::DeleteSpriteSheetMap() {
for (std::map<std::string, TextureReference*>::iterator it = Graphics::SpriteSheetTextureMap.begin();
it != Graphics::SpriteSheetTextureMap.end();
it++)
{
Graphics::DisposeTexture(it->second->TexturePtr);
delete it->second;
}

Graphics::SpriteSheetTextureMap.clear();
}

PUBLIC STATIC Uint32 Graphics::CreateVertexBuffer(Uint32 maxVertices, int unloadPolicy) {
Uint32 idx = 0xFFFFFFFF;
Expand Down Expand Up @@ -1313,8 +1350,8 @@ PUBLIC STATIC void Graphics::DeleteScene3D(Uint32 sceneIndex) {

Scene3D* scene = &Graphics::Scene3Ds[sceneIndex];
if (scene->Initialized) {
if (Graphics::GfxFunctions->DeleteVertexBuffer)
Graphics::GfxFunctions->DeleteVertexBuffer(scene->Buffer);
if (Graphics::Internal.DeleteVertexBuffer)
Graphics::Internal.DeleteVertexBuffer(scene->Buffer);
else
delete scene->Buffer;

Expand Down
38 changes: 22 additions & 16 deletions source/Engine/Rendering/GL/GLRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,11 @@ void GL_PrepareVertexBufferUpdate(Scene3D* scene, VertexBuffer* vertexBuffer, Ui
qsort(vertexBuffer->FaceInfoBuffer, vertexBuffer->FaceCount, sizeof(FaceInfo), PolygonRenderer::FaceSortFunction);
}
}
void GL_DeleteVertexIndexList(GL_VertexBuffer* driverData) {
for (size_t i = 0; i < driverData->VertexIndexList.size(); i++)
delete driverData->VertexIndexList[i];
driverData->VertexIndexList.clear();
}
void GL_UpdateVertexBuffer(Scene3D* scene, VertexBuffer* vertexBuffer, Uint32 drawMode, bool useBatching) {
GL_PrepareVertexBufferUpdate(scene, vertexBuffer, drawMode);

Expand All @@ -552,9 +557,7 @@ void GL_UpdateVertexBuffer(Scene3D* scene, VertexBuffer* vertexBuffer, Uint32 dr
if (useBatching) {
driverData->UseVertexIndices = true;

for (size_t i = 0; i < driverData->VertexIndexList.size(); i++)
delete driverData->VertexIndexList[i];
driverData->VertexIndexList.clear();
GL_DeleteVertexIndexList(driverData);

vertexIndices = new vector<Uint32>();
driverData->VertexIndexList.push_back(vertexIndices);
Expand Down Expand Up @@ -1366,7 +1369,9 @@ PUBLIC STATIC void GLRenderer::DisposeTexture(Texture* texture) {
glDeleteTextures(1, &textureData->TextureU); CHECK_GL();
glDeleteTextures(1, &textureData->TextureV); CHECK_GL();
}
glDeleteTextures(1, &textureData->TextureID); CHECK_GL();
if (textureData->TextureID) {
glDeleteTextures(1, &textureData->TextureID); CHECK_GL();
}
Memory::Free(textureData);
}

Expand Down Expand Up @@ -2059,19 +2064,20 @@ PUBLIC STATIC void* GLRenderer::CreateVertexBuffer(Uint32 maxVertices) {
PUBLIC STATIC void GLRenderer::DeleteVertexBuffer(void* vtxBuf) {
VertexBuffer* vertexBuffer = (VertexBuffer*)vtxBuf;
GL_VertexBuffer* driverData = (GL_VertexBuffer*)vertexBuffer->DriverData;
if (!driverData)
return;
if (driverData) {
GL_DeleteVertexIndexList(driverData);

for (size_t i = 0; i < driverData->VertexIndexList.size(); i++)
delete driverData->VertexIndexList[i];
for (size_t e = 0; e < driverData->Entries->size(); e++)
Memory::Free((*driverData->Entries)[e]);
if (driverData->VertexIndexBuffer)
Memory::Free(driverData->VertexIndexBuffer);
delete driverData->Entries;
delete driverData->Faces;

Memory::Free(driverData);
for (size_t e = 0; e < driverData->Entries->size(); e++)
Memory::Free((*driverData->Entries)[e]);

if (driverData->VertexIndexBuffer)
Memory::Free(driverData->VertexIndexBuffer);

delete driverData->Entries;
delete driverData->Faces;

Memory::Free(driverData);
}

delete vertexBuffer;
}
Expand Down
42 changes: 35 additions & 7 deletions source/Engine/Rendering/GL/GLShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

class GLShader {
public:
GLuint ProgramID;
GLuint VertexProgramID;
GLuint FragmentProgramID;
GLuint ProgramID = 0;
GLuint VertexProgramID = 0;
GLuint FragmentProgramID = 0;

GLint LocProjectionMatrix;
GLint LocModelViewMatrix;
Expand Down Expand Up @@ -112,6 +112,12 @@ PUBLIC GLShader::GLShader(Stream* streamVS, Stream* streamFS) {
CheckShaderError(VertexProgramID);
CheckGLError(__LINE__);

glDeleteProgram(ProgramID); CHECK_GL();
glDeleteShader(VertexProgramID); CHECK_GL();

ProgramID = 0;
VertexProgramID = 0;

free(sourceVS);
free(sourceFS);
return;
Expand All @@ -126,6 +132,14 @@ PUBLIC GLShader::GLShader(Stream* streamVS, Stream* streamFS) {
CheckShaderError(FragmentProgramID);
CheckGLError(__LINE__);

glDeleteProgram(ProgramID); CHECK_GL();
glDeleteShader(VertexProgramID); CHECK_GL();
glDeleteShader(FragmentProgramID); CHECK_GL();

ProgramID = 0;
VertexProgramID = 0;
FragmentProgramID = 0;

free(sourceVS);
free(sourceFS);
return;
Expand Down Expand Up @@ -224,8 +238,22 @@ PUBLIC GLint GLShader::GetUniformLocation(const GLchar* identifier) {
return value;
}

PUBLIC void GLShader::Dispose() {
glDeleteProgram(ProgramID); CHECK_GL();
PUBLIC GLShader::~GLShader() {
if (CachedProjectionMatrix) {
delete CachedProjectionMatrix;
}
if (CachedModelViewMatrix) {
delete CachedModelViewMatrix;
}
if (VertexProgramID) {
glDeleteShader(VertexProgramID); CHECK_GL();
}
if (FragmentProgramID) {
glDeleteShader(FragmentProgramID); CHECK_GL();
}
if (ProgramID) {
glDeleteProgram(ProgramID); CHECK_GL();
}
}

PUBLIC STATIC bool GLShader::CheckGLError(int line) {
Expand All @@ -249,11 +277,11 @@ PUBLIC STATIC bool GLShader::CheckGLError(int line) {
case GLU_INVALID_ENUM: errstr = "invalid enumerant"; break;
case GLU_INVALID_VALUE: errstr = "invalid value"; break;
case GLU_OUT_OF_MEMORY: errstr = "out of memory"; break;
case GLU_INCOMPATIBLE_GL_VERSION: errstr = "incompatible gl version"; break;
case GLU_INCOMPATIBLE_GL_VERSION: errstr = "incompatible OpenGL version"; break;
// case GLU_INVALID_OPERATION: errstr = "invalid operation"; break;
#endif
default:
errstr = "idk";
errstr = "unknown error";
break;
}
if (error != GL_NO_ERROR) {
Expand Down
6 changes: 3 additions & 3 deletions source/Engine/Rendering/GL/GLShaderContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ PUBLIC STATIC GLShaderContainer* GLShaderContainer::MakeYUV() {

PUBLIC GLShaderContainer::~GLShaderContainer() {
if (Base)
Base->Dispose();
delete Base;
if (Textured)
Textured->Dispose();
delete Textured;
if (PalettizedTextured)
PalettizedTextured->Dispose();
delete PalettizedTextured;
}
33 changes: 33 additions & 0 deletions source/Engine/Rendering/TextureReference.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#if INTERFACE
#include <Engine/Includes/Standard.h>

need_t Texture;

class TextureReference {
public:
Texture* TexturePtr;

unsigned References;
};
#endif

#include <Engine/Rendering/TextureReference.h>

PUBLIC TextureReference::TextureReference(Texture *ptr) {
TexturePtr = ptr;
References = 0;
AddRef();
}

PUBLIC void TextureReference::AddRef() {
References++;
}

PUBLIC bool TextureReference::TakeRef() {
if (References == 0)
abort();

References--;

return References == 0;
}
6 changes: 5 additions & 1 deletion source/Engine/ResourceTypes/ISound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,9 @@ PUBLIC AudioPlayback* ISound::CreatePlayer() {
}

PUBLIC void ISound::Dispose() {

if (SoundData) {
SoundData->Dispose();
delete SoundData;
SoundData = nullptr;
}
}
Loading

0 comments on commit 0232e85

Please sign in to comment.