twhl-community / halflife-updated

Half-Life SDK updated to compile under VS2019 and 2022. Check README.md for more information.
Other
382 stars 146 forks source link

Rework operator new and delete overloads to allocate memory directly #71

Closed SamVanheer closed 2 years ago

SamVanheer commented 2 years ago

The overloaded operator new and delete used in CBaseEntity have a second parameter used to pass the entvars_t instance to allow the engine to allocate memory for the entity. This is legal C++, but the syntax can cause problems. Clang-tidy complains about it and says destructors won't be called as a result.

Allocate the memory directly instead. This eliminates a call into the engine and allows dynamic memory allocation in the client as well without having to implement the engine function. This also ensures both overloads follow exactly the standard without requiring conditional compilation.

Note that the engine allocates the memory using calloc, so this will not reduce engine heap memory usage.