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

@@ -1,5 +1,6 @@
#include "GraphicsResources.h"
#include "Alloc.h"
#include "FileSystemUtils.h"
#include "Vlogging.h"
@@ -37,7 +38,7 @@ SDL_Surface* LoadImage(const char *filename)
return NULL;
}
error = lodepng_decode32(&data, &width, &height, fileIn, length);
FILESYSTEM_freeMemory(&fileIn);
VVV_free(fileIn);
if (error != 0)
{
@@ -61,14 +62,14 @@ SDL_Surface* LoadImage(const char *filename)
SDL_PIXELFORMAT_ARGB8888,
0
);
SDL_FreeSurface( loadedImage );
SDL_free(data);
VVV_freefunc(SDL_FreeSurface, loadedImage);
VVV_free(data);
SDL_SetSurfaceBlendMode(optimizedImage, SDL_BLENDMODE_BLEND);
return optimizedImage;
}
else
{
SDL_free(data);
VVV_free(data);
vlog_error("Image not found: %s", filename);
SDL_assert(0 && "Image not found! See stderr.");
return NULL;
@@ -104,10 +105,7 @@ void GraphicsResources::init(void)
void GraphicsResources::destroy(void)
{
#define CLEAR(img) \
SDL_FreeSurface(img); \
img = NULL;
#define CLEAR(img) VVV_freefunc(SDL_FreeSurface, img)
CLEAR(im_tiles);
CLEAR(im_tiles2);
CLEAR(im_tiles3);