From 2b1b4544f948d3a9aaa60083832c96c72fd62ceb Mon Sep 17 00:00:00 2001 From: GenericHeroGuy Date: Sat, 20 Sep 2025 16:52:44 +0200 Subject: [PATCH] Introducing: __attribute__((__cleanup__)) I don't wanna hear about MSVC... or C++... This fixes NUMEROUS memory leaks introduced by G_ReadDemoHeader, which I was utterly oblivious to up until an hour ago --- src/doomdef.h | 6 ++++++ src/z_zone.h | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/doomdef.h b/src/doomdef.h index a16eae265..7e2589cf6 100644 --- a/src/doomdef.h +++ b/src/doomdef.h @@ -567,6 +567,12 @@ UINT32 quickncasehash (const char *p, size_t n) #endif #endif +// the GNU cleanup attribute: plugging memory leaks since 2003! +// on scope exit, the cleanup function is called with a pointer to the declared variable, +// essentially behaving like a C++ destructor +// NOTE: you WILL have nasal troubles if the variable is not initialized +#define CLEANUP(f) __attribute__((__cleanup__(f))) + // An assert-type mechanism. #ifdef PARANOIA #define I_Assert(e) ((e) ? (void)0 : I_Error("assert failed: %s, file %s, line %d", #e, __FILE__, __LINE__)) diff --git a/src/z_zone.h b/src/z_zone.h index 709fa1d96..25d5cfab4 100644 --- a/src/z_zone.h +++ b/src/z_zone.h @@ -171,6 +171,12 @@ void *Z_LevelPoolMalloc(size_t size); void *Z_LevelPoolCalloc(size_t size); void Z_LevelPoolFree(void *p, size_t size); +// for use with CLEANUP macro +FUNCINLINE static ATTRINLINE void Z_Pfree(void *p) +{ + Z_Free(*(void **)p); +} + #ifdef __cplusplus } // extern "C" #endif