iOS objective-c object: When to use release and when to not use it

你。 提交于 2019-12-10 18:23:43

问题


I m under iOS and I m developping with delphi Tokyo, and this is my code :

      aUIImage := TUIImage.Wrap(TUIImage.alloc.initWithCGImage(aCGImageRef));
      try

        aData := TNSData.Wrap(UIImageJPEGRepresentation((aUIImage as ILocalObject).GetObjectID, cWin_DefaultJPGCompressionRate / 100));
        try
          aWorkPicStream.WriteBuffer(aData.bytes^, aData.length);
        finally
          aData.release; // << this make my code will crash (later not now)
        end;

      finally
        aUIImage.release;
      end;

After executing the code before I have a little later in the execution this error :

myproj  [E][W][I][D][V] Error => Access violation at address 0000000184D4891C, accessing address 0000000107FD286C
At address: $0000000184D4891C (objc_msgSend + 28)

Call stack:
myproj                    $0000000103E00548 Grijjy.Errorreporting.TgoExceptionReporter.GlobalGetExceptionStackInfo(TExceptionRecord*) + 196
myproj                    $00000001030DF0EC Sysutils.Exception.RaisingException(TExceptionRecord*) + 88
myproj                    $0000000103116164 Sysutils.RaiseExceptObject(TExceptionRecord*) + 84
myproj                    $00000001030BB498 _RaiseAtExcept(TObject*, Pointer) + 128
myproj                    $00000001030DD900 Internal.Excutils.SignalConverter(NativeUInt, NativeUInt, NativeUInt) + 68
libobjc.A.dylib           $0000000184D5213C <redacted> + 844
CoreFoundation            $0000000185A40AAC _CFAutoreleasePoolPop + 28
Foundation                $00000001864FB960 <redacted> + 148
myproj                    $00000001031B426C Classes.ThreadProc(Classes.TThread*) + 948
myproj                    $00000

If i comment the line aData.release; then i will not meet any error.

Why ? How to know when we must call release and when we must not call release ?


回答1:


ARC rules for iOS are rather simple as explained in Apple's Basic Memory Management Rules.

Methods whose name begins with alloc, new, copy, or mutableCopy don't require calls to retain. On the contrary, if you call it you will create a memory leak because there will be one retain too many. But they do require release or autorelease. Those object instances are the ones you have created, and under Objective-C they are automatically retained when constructed.

aUIImage is constructed using alloc, you own it and you are responsible for releasing it with release. On the other hand you don't own aData and it will be handled by the system.

Another thing to keep in mind is that for objects you don't own, you may need to call both retain and release to keep the object instance alive as long as you are using it. As received object is normally guaranteed to remain valid within the method it was received in, you don't have to call retain and release upon aData in your code.


The retainCount method returns the current reference count of the Objective-C object instance. This number is purely informative and has no debugging value under iOS or macOS, however it is good enough to show the interaction between Objective-C and Delphi memory management and can be helpful at times.

From the Apple documentation about retainCount:

retainCount - Do not use this method

This method is of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.




回答2:


Apart from the above rules, for general debugging you can run a static analysis of your target - "Cmnd+Shift+B(Build)". This would give some insights on the possible memory leaks. (Note: In certain cases, it might say a possible memory leak for an object but the object could be released somewhere else in code. It depends on your implementation)



来源:https://stackoverflow.com/questions/52314334/ios-objective-c-object-when-to-use-release-and-when-to-not-use-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!