1

As I know there is no ARC for the Objective-C objects represented by the import wrapper class. When dealing with Objective-C objects you’ll have to call retain and release yourself at the correct points.

Allocating a new Objective-C object will initialize its reference count to 1 and calling release will drop it to 0 thus destroying it.

so the question, do we need to call release for every object we create ? how exactly work the reference counting with ios objective-c object under delphi ?

I also heard that their is different type of scenario, for exemple if you create such a class with Create, alloc it's seam that retain is called automatically and with the other functions like fileUrlWithPath it's seam retain is not called ... so right now i lost to know when to do release and when to do retain

a typical scenario i encountered is this :

  var aGraphRequest: FBSDKGraphRequest;

  aGraphRequest := TFBSDKGraphRequest.Wrap(TFBSDKGraphRequest.Alloc.initWithGraphPath(StrToNSStr(aGraphPath));
  aGraphRequest.startWithCompletionHandler(GraphRequestCompletionHandler);

this instruction below create a GraphRequest that will call in async (so it's return immediately) http request and call later (when finished) the functions GraphRequestCompletionHandler

the question do i need to write it like

  aGraphRequest := TFBSDKGraphRequest.Wrap(TFBSDKGraphRequest.Alloc.initWithGraphPath(StrToNSStr(aGraphPath));
  aGraphRequest.startWithCompletionHandler(GraphRequestCompletionHandler);
  aGraphRequest.release

instead ?

Other example taken from delphi :

function PNSDictionaryToJSON(const ADictionary: PNSDictionary): string;
var
  LData: NSData;
  LString: NSString;
  LError: NSError;
begin
  if ADictionary = nil then
    raise EArgumentException.Create(sArgumentInvalid);

  LData := TNSJSONSerialization.OCClass.dataWithJSONObject(ADictionary, 0, Addr(LError));
  if (LData <> nil) and (LError = nil) then
  begin
    LString := TNSString.Wrap(TNSString.Alloc.initWithData(LData, NSUTF8StringEncoding));
    Result :=  NSStrToStr(LString);
  end
  else
    Result := string.Empty;
end;

i thing their is a memory leak in this sample no ? miss the release of the LString

zeus
  • 12,173
  • 9
  • 63
  • 184

0 Answers0