Repeat of destructor/constructor C++ discussion. TFC uses them. HL
doesn't. Read why and how below.
From: Ken Birdwell
Sent: Thursday, September 13, 2001 2:31 AM
To: '[EMAIL PROTECTED]'
Subject: RE: [HLCoders] CBaseEntity Memory Mgmnt Q.
I guess an important point to remember is that there is NO C++ in the
engine. It's 100% C and assembly*. The only C++ portions are the game
(server) and client dll. Their interface to the engine is 100% C, that's
what all the Dispatchblah functions are for in cbase.cpp; they resolve the
C interface to their C++ counterparts.
During initialization, the engine calls the game dll's GetEntityAPI()
function. This returns a structure filled in with function pointers to all
the game dll's dispatch functions, functions that the engine can then call
at the appropriate time. Half-life implemented its game DLL in C++, though
you don't have to. At an early stage of the project the game dll was in fact
written completely in C - it was quicker to convert the original Quake 1 QC
code to C first instead of directly to C++ - your game dll could also be
written completely in C, but I can't think of why you'd ever want to.
Besides calling GetEntityAPI() during initialization, the engine also calls
the function GetEntityAPI2(), in which the game dll can return back another
list of dispatch functions, most notably containing the function
OnFreeEntPrivateData. This function is called by the engine whenever it is
going to free up an edict, it's basically giving the game dll an opportunity
to do something - such as calling a local destructor on the private data -
before that memory becomes invalid.
As to constructors and destructors, we never used them. The reasons are
mostly historical, there's no fundamental reason why we didn't, it's just
that the during the conversion from C to C++ they were never needed and we
just got into the habit of not using them. Well, that's not totally true,
you may also notice that there's not a single** call to new in the entire
game dll, it (was) 100% written to just use the engine's memory management
system. The driving force behind that coding standard was that the game
needed to run on a 16MB console machine with no virtual memory, and you
should also note that there's no (okay, hardly any) explicit CBaseEntity
pointers in any of the base classes since in some configurations (that you
don't have to worry about) the engines garbage collection memory system can
(could) move the addresses of everything on you between calls to
DispatchThink(). Not really something that's very clean to do on a real
free form C++ system, so avoiding calls to new and calls to destructors and
constructors made it a lot simpler to avoided bugs when that happened,
though I don't think the code works perfectly in its current configuration
(ie the GearBox guys had to beat on it some to make in run on a machine like
the PS2 again).
So who cares, call new all you want, make your mod run on a system that
needs 128MB of ram and go for it, you don't need to make sure it runs on all
the computers that people bought in 1995 like we had to (boo hoo, now I'm
just whining). If you want your destructors called, do this:
NEW_DLL_FUNCTIONS gNewDLLFunctions =
int GetNewDLLFunctions(NEW_DLL_FUNCTIONS *pFunctionTable, int
if(!pFunctionTable || *interfaceVersion !=
*interfaceVersion = NEW_DLL_FUNCTIONS_VERSION;
memcpy(pFunctionTable, gNewDLLFunctions, sizeof(gNewDLLFunctions));
void OnFreeEntPrivateData(edict_s *pEdict)
*okay, technically there's C++ in the engine for VGUI and audio stuff, but
it's very small and only went in the last year or so and it wasn't there
last time I worked on it.
**not sure if that's true anymore, it was true last time I checked.
From: Soul Sounds [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, September 12, 2001 10:10 AM
To: [EMAIL PROTECTED]
Subject: [HLCoders] CBaseEntity Memory Mgmnt Q.
Should You Check Your Credit Report? Of course! We all check
our credit card statements for inaccuracies and we should do
the same for our credit history. Click here now to check
yours FOR FREE at ConsumerInfo.Com!
I tried adding a destructor to a class I derived from CBaseEntity and
got the following error: