Delphi XE6 ARC on OSX releasing variables

  

I’m having a problem with NSObject instances being freed when I don’t expect. I have a form variable of type NSNumber, in button1 I create an instance and set a value, in button2 I read the value. If I don’t call retain in button 1 then the variable is freed and the app hangs when I click button2, adding a call to retain makes everything work.

This is on OSX using Delphi XE6 with firemonkey.

Here’s some code

Define a form variable of type NSNumber

Fv : NSNumber;

Now add a couple of buttons

for Button1Click

begin
Fv := TNSNumber.Wrap(TNSNumber.OCClass.numberWithFloat(4.0));
ShowMessage(IntToStr(Fv.retainCount)); // value is 1
Fv.retain; // comment out this to make it crash on button2 click
ShowMessage(IntToStr(Fv.retainCount)); // value is 2, or 1 without the retain
end;

for Button2click

begin
ShowMessage(IntToStr(Fv.retainCount)); // value is 1 or crashes without the retain
ShowMessage(FloatToStr(Fv.doubleValue));
end;

Now what seems to be happening is at the end of Button1 click, delphi is releasing Fv by decrementing the reference count – i.e. it acts like its going out of scope. So to make Fv hang around I have to add the Fv.retain. If I click button2 without the retain, then it crashes.

Should I put a retain in – I didn’t think it was necessary, or am I missing something else?

tia

Comments are closed.