Skip to content

Commit f9d4f33

Browse files
committed
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.
1 parent c9dd357 commit f9d4f33

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

sp/src/public/vscript/ivscript.h

+22-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,28 @@ struct ScriptVariant_t
431431
void operator=( QAngle &&vec ) { m_type = FIELD_VECTOR; m_pAngle = new QAngle( vec ); m_flags |= SV_FREE; }
432432
#endif
433433

434-
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
434+
void Free()
435+
{
436+
// Generally only needed for return results
437+
if ( ! ( m_flags & SV_FREE ) )
438+
{
439+
return;
440+
}
441+
442+
switch ( m_type )
443+
{
444+
case FIELD_VECTOR:
445+
// QAngle and Vector share the type FIELD_VECTOR
446+
// both are trivial to desctruct, so it should be fine to delete QAngle as Vector (or vice versa)
447+
delete m_pVector;
448+
return;
449+
case FIELD_CSTRING:
450+
free( (char*)m_pszString );
451+
return;
452+
}
453+
454+
AssertMsg( 0, "Don't know how to free script variant of type %d", m_type );
455+
}
435456

436457
template <typename T>
437458
T Get()

0 commit comments

Comments
 (0)