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

@@ -4,6 +4,7 @@
#include <SDL.h>
#include <utf8/unchecked.h>
#include "Alloc.h"
#include "Constants.h"
#include "CustomLevels.h"
#include "Entity.h"
@@ -158,7 +159,7 @@ void Graphics::destroy(void)
#define CLEAR_ARRAY(name) \
for (size_t i = 0; i < name.size(); i += 1) \
{ \
SDL_FreeSurface(name[i]); \
VVV_freefunc(SDL_FreeSurface, name[i]); \
} \
name.clear();
@@ -228,22 +229,20 @@ void Graphics::create_buffers(const SDL_PixelFormat* fmt)
void Graphics::destroy_buffers(void)
{
#define FREE_SURFACE(SURFACE) \
SDL_FreeSurface(SURFACE); \
SURFACE = NULL;
#define FREE_SURFACE(SURFACE) VVV_freefunc(SDL_FreeSurface, SURFACE)
FREE_SURFACE(backBuffer)
FREE_SURFACE(footerbuffer)
FREE_SURFACE(ghostbuffer)
FREE_SURFACE(foregroundBuffer)
FREE_SURFACE(menubuffer)
FREE_SURFACE(warpbuffer)
FREE_SURFACE(warpbuffer_lerp)
FREE_SURFACE(towerbg.buffer)
FREE_SURFACE(towerbg.buffer_lerp)
FREE_SURFACE(titlebg.buffer)
FREE_SURFACE(titlebg.buffer_lerp)
FREE_SURFACE(tempBuffer)
FREE_SURFACE(backBuffer);
FREE_SURFACE(footerbuffer);
FREE_SURFACE(ghostbuffer);
FREE_SURFACE(foregroundBuffer);
FREE_SURFACE(menubuffer);
FREE_SURFACE(warpbuffer);
FREE_SURFACE(warpbuffer_lerp);
FREE_SURFACE(towerbg.buffer);
FREE_SURFACE(towerbg.buffer_lerp);
FREE_SURFACE(titlebg.buffer);
FREE_SURFACE(titlebg.buffer_lerp);
FREE_SURFACE(tempBuffer);
#undef FREE_SURFACE
}
@@ -347,8 +346,7 @@ void Graphics::updatetitlecolours(void)
} \
} \
\
SDL_FreeSurface(grphx.im_##tilesheet); \
grphx.im_##tilesheet = NULL; \
VVV_freefunc(SDL_FreeSurface, grphx.im_##tilesheet); \
}
#define PROCESS_TILESHEET(tilesheet, tile_square, extra_code) \
@@ -376,7 +374,7 @@ bool Graphics::Makebfont(void)
font_positions[codepoint] = pos;
++pos;
}
FILESYSTEM_freeMemory(&charmap);
VVV_free(charmap);
}
else
{
@@ -502,7 +500,7 @@ static void print_char(
if (scale > 1)
{
SDL_FreeSurface(surface);
VVV_freefunc(SDL_FreeSurface, surface);
}
}
@@ -2179,7 +2177,7 @@ void Graphics::drawentity(const int i, const int yoff)
setRect(drawRect, xp, yp - yoff, sprites_rect.x * 6, sprites_rect.y * 6);
SDL_Surface* TempSurface = ScaleSurface( spritesvec[obj.entities[i].drawframe], 6 * sprites_rect.w,6* sprites_rect.h );
BlitSurfaceColoured(TempSurface, NULL , backBuffer, &drawRect, ct );
SDL_FreeSurface(TempSurface);
VVV_freefunc(SDL_FreeSurface, TempSurface);