From 0551292cd3a70da152f55dd999a948ad3754f317 Mon Sep 17 00:00:00 2001 From: Alexander 'z33ky' Hirsch <1zeeky@gmail.com> Date: Fri, 6 Sep 2024 23:25:27 +0200 Subject: [PATCH] Fix ScriptVariant_t::Free() Type FIELD_HSCRIPT is removed, it never sets SV_FREE. strdup()ed strings are not free'd via free() instead of delete, which would be the conformant way in standard C++. It matters not in Source, since both use the same global allocator of the engine, but it is tidier. Add a comment why we feel fine deleting QAngle as Vector. --- sp/src/public/vscript/ivscript.h | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/sp/src/public/vscript/ivscript.h b/sp/src/public/vscript/ivscript.h index 88f4771a5b..de0842fdf5 100644 --- a/sp/src/public/vscript/ivscript.h +++ b/sp/src/public/vscript/ivscript.h @@ -431,7 +431,29 @@ struct ScriptVariant_t void operator=( QAngle &&vec ) { m_type = FIELD_VECTOR; m_pAngle = new QAngle( vec ); m_flags |= SV_FREE; } #endif - void Free() { if ( ( m_flags & SV_FREE ) && ( m_type == FIELD_HSCRIPT || m_type == FIELD_VECTOR || m_type == FIELD_CSTRING ) ) delete m_pszString; } // Generally only needed for return results + void Free() + { + // Generally only needed for return results + if ( ! ( m_flags & SV_FREE ) ) + { + return; + } + + switch ( m_type ) + { + case FIELD_VECTOR: + // QAngle and Vector share the type FIELD_VECTOR + // both are trivial to desctruct, so it should be fine to delete QAngle as Vector (or vice versa) + delete m_pVector; + return; + + case FIELD_CSTRING: + free( (char*)m_pszString ); + return; + } + + AssertMsg( 0, "Don't know how to free script variant of type %d", m_type ); + } template T Get()