My Android project includes a lot of ported legacy (FPC) code that likes to use global variables. I’m getting a strange null pointer exception on the below code:
IF (gModExtraInfo <> NIL) THEN BEGIN humanKey := GetResourceString(rsModuleExtraInfoKeys, rimHumanReadableName); abbrKey := GetResourceString(rsModuleExtraInfoKeys, rimTextAbbr); iIndex := 0; found := FALSE; WHILE ((iIndex < gModExtraInfo.NumObjects) AND NOT found) DO ...
The null pointer exception is:
Attempt to invoke virtual method ‘int p010TargetUtility.TObjectArray.NumObjects()’ on a null object reference
in the loop condition.
The code has just checked to ensure that the variable is not null a few lines before this, and nothing in
gModExtraInfo. It seems that
gModExtraInfo is being garbage collected somewhere between the check and the call(?)
Is there some way to prevent global variables from being GC’d? (If it’s relevant, the global
gModExtraInfo is declared in the
interface section of a separate unit that is included in the
uses list of this file.)