Skip to content

Commit

Permalink
Fix ScriptVariant_t::Free()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
z33ky committed Sep 6, 2024
1 parent c9dd357 commit f9d4f33
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion sp/src/public/vscript/ivscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,28 @@ 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 <typename T>
T Get()
Expand Down

0 comments on commit f9d4f33

Please sign in to comment.