Fix #9588, 140a96b: [Squirrel] Reaching memory limit during script registration could prevent further script detections (#9589)

Also the memory allocation triggering the limit was never freed.
And if the exception was thrown in a constructor using placement new, the pre-allocated memory was not freed either.
This commit is contained in:
Loïc Guilloux
2021-10-02 15:13:58 +02:00
committed by GitHub
parent 78d66b7732
commit ccd586a736
2 changed files with 31 additions and 1 deletions

View File

@@ -62,6 +62,24 @@ struct SQRefCounted
SQUnsignedInteger _uiRef;
struct SQWeakRef *_weakref;
virtual void Release()=0;
/* Placement new/delete to prevent memory leaks if constructor throws an exception. */
inline void *operator new(size_t size, SQRefCounted *place)
{
place->size = size;
return place;
}
inline void operator delete(void *ptr, SQRefCounted *place)
{
SQ_FREE(ptr, place->size);
}
/* Never used but required. */
inline void operator delete(void *ptr) { NOT_REACHED(); }
private:
size_t size;
};
struct SQWeakRef : SQRefCounted