Various sources appear to discourage typed constants in favour of more object-oriented techniques. For example DelphiBasics where they are described as "very odd" and StackOverflow Question which gives some background on why they might be used. Here is an example of a true and a typed constant.
const
RESULTS_BASIC1 = $01; // True constant
RESULTS_BASIC2: BYTE = $01; // Typed constant
I'm using Delphi 7 and FastMM4, and a leak of a single TCriticalSection is being reported. After enabling a detailed map file in the Project Options, the FastMM4 stack trace shows something like this:
402E58 [StConst][StConst][@GetMem]
40454B [WinConvert][WinConvert][d_len]
404926 [Main][Main][EXAMPLE_TYPED_CONSTANT1]
...
If I then remove the typed constant, the same TCriticalSection is reported as being leaked...on the next typed constant! After slowing removing one typed constant after another, I'm still no nearer to the "true" source of the leak.
Two related questions:
1) Could a typed constant (which are actually variables with a memory address) be the true source of the memory leak? Could FastMM4 be reporting the leak in error?
2) Should typed constants really be avoided and if so, what is the recommended alternative? For example, say I'm using an elapsed processor TickCount (an unsigned 32-bit integer):
dwElapsed := (GetTickCount() - m_dwLastFlashCheck);
if (dwElapsed > 2000) then
begin
m_dwLastFlashCheck := GetTickCount();
DoSomething();
end;
it seems natural to define a "constant" as in:
RATE_FLASH: DWORD = 2000;
// ...
if (dwElapsed > RATE_FLASH) then
// etc