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

  

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 ?

Comments are closed.