Replace all free calls with VVV_free[func]

This replaces all calls to SDL_free with a new macro, VVV_free, that
nulls the pointer afterwards. This mitigates any use-after-frees and
also completely eliminates double-frees. The same is done for any
function to free specific objects such as SDL_FreeSurface, with the
VVV_freefunc macro.

No exceptions for any of these calls, even if the pointer is discarded
or zeroed afterwards anyway. Better safe than sorry.

This is a macro rather than a function that takes in a
pointer-to-pointer because such a function would have type issues that
require casting and that's just not safe.

Even though SDL_free and other SDL functions already check for NULL, the
macro has a NULL check for other functions that don't. For example,
FAudioVoice_DestroyVoice does not check for NULL.

FILESYSTEM_freeMemory has been axed in favor of VVV_free because it
functionally does the same thing except for `unsigned char*` only.
This commit is contained in:
Misa
2022-11-30 22:30:16 -08:00
parent 6e583d949b
commit a926ce9851
15 changed files with 95 additions and 100 deletions

View File

@@ -3,6 +3,7 @@
#include <SDL.h>
#include "Alloc.h"
#include "Constants.h"
#include "FileSystemUtils.h"
#include "Game.h"
@@ -81,17 +82,11 @@ void Screen::init(const struct ScreenSettings* settings)
void Screen::destroy(void)
{
#define X(CLEANUP, POINTER) \
CLEANUP(POINTER); \
POINTER = NULL;
/* Order matters! */
X(SDL_DestroyTexture, m_screenTexture);
X(SDL_FreeSurface, m_screen);
X(SDL_DestroyRenderer, m_renderer);
X(SDL_DestroyWindow, m_window);
#undef X
VVV_freefunc(SDL_DestroyTexture, m_screenTexture);
VVV_freefunc(SDL_FreeSurface, m_screen);
VVV_freefunc(SDL_DestroyRenderer, m_renderer);
VVV_freefunc(SDL_DestroyWindow, m_window);
}
void Screen::GetSettings(struct ScreenSettings* settings)
@@ -126,7 +121,7 @@ void Screen::LoadIcon(void)
return;
}
SDL_SetWindowIcon(m_window, icon);
SDL_FreeSurface(icon);
VVV_freefunc(SDL_FreeSurface, icon);
}
#endif /* __APPLE__ */
@@ -279,7 +274,7 @@ void Screen::UpdateScreen(SDL_Surface* buffer, SDL_Rect* rect )
if(badSignalEffect)
{
SDL_FreeSurface(buffer);
VVV_freefunc(SDL_FreeSurface, buffer);
}
}