Null Pointer exception with global variable

(Matt Robertson) #1

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
  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 GetResourceString affects 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.)

(marc hoffman) #3

Can we get a test case for this?

an object stored in a global var should never get GCed/nil’ed on its own.

(Matt Robertson) #4

I posted this prematurely. Seems to have been a race condition issue with a background thread setting the global reference to nil. I’m happy to delete the thread but somehow I didn’t have permission to delete my own post :upside_down_face:

1 Like
(marc hoffman) #5

BTW, is the uppercase keyword CC in Water working out for you?

1 Like
(Matt Robertson) #6

Thanks for asking. I actually am still using .2395 :grimacing: I had a build fail issue with .2399 and did not have time to look into it as I was in the midst of a release. I am excited to use the uppercase CC soon though now that I (almost) have time to test out the upgrade.

1 Like