Potential memory leak - Received Memory Warning - ios

I am using iOS 7 and I am getting memory warnings in this part of code, but I'am not able to understand how the retain count increases as I release imageToSave variable too.

Static analyser not always write about memory leak but its assuming that when you are calling finalOutput its returning you an allocated object mean +1 retain count object which is never used and its treating this waring as memory leak!

You have not allocated memory to this object yourself, so you don't own it. And you are still releasing it.You can't release objects you don't own.
Unless you use alloc method to allocate memory, you can't just release them........

Related

Prevent EXC_BAD_ACCESS during backgrounding for long running processes

In my application, I have a series of long running processes. Sometimes, when the app gets backgrounded, one of these processes will return and try to notify other objects (which have been released) that it has completed, causing an EXC_BAD_ACCESS. Something like:
[process runForALongTimeWithCompletion:^(){
[possiblyReleasedObject heyTheProcessFinished];
}];
How can I check if possiblyReleasedObject has been released? Or, more precisely in my case, how do I check if it is dereference-able, such that referring to it won't cause a EXC_BAD_ACCESS error?
There is no way to (safely) check if a pointer to an object points to a valid memory location in Objective-C. You'll need to structure your program such that objects holding a pointer to possiblyReleasedObject retain it so that it doesn't get inadvertently released.
When an object holding a pointer to possiblyReleasedObject no longer needs it, it's generally good practice to set the pointer to nil after calling release in order to avoid accidentally dereferencing a bad pointer.

Memory management in Xcode

I try to allocate and release a mutable array in Xcode
NSMutableArray *inventory = [[NSMutableArray alloc] init];
[inventory addObject:#"one"];
[inventory release];
Does anyone help me to explain why after "inventory" was released, it still store 1 objects?
Assuming you are working without Automatic Reference Counting (ARC):
When you release an object, that marks the memory that the object was previously occupying as free. However, it does not necessarily immediately destroy what was in that memory.
If you try to (improperly) access what was at your array's memory address, you may very well find the array members still there. But be warned, this is not safe, and the array and its members can (and will) be overwritten at any time.
You should use ARC for any production code to avoid the dangers of memory mismanagement. It works very well.

Release NSMutableDictionary memory under ARC in iOS

In my app, I used the NSMutableDictionary and when I go to a specific view controller, the memory utilized increases more.
How can I free the used memory in Objective-C?
All you need to do is assign it to nil. Under ARC that's all that's needed to 'release' an object:
self.myMutableDictionary = nil; //It no longer exists.
Note that this kills the reference that the view controller this code is run from has - if this is the only strong reference, then the object will be removed and the memory freed. If another object(s) have strong reference(s) pointing to the dictionary, though, then the memory won't be freed until those references are also set to nil. It's the responsibility of the allocating objects to be responsible and set their references to nil when they no longer need the object.

Apple Instruments - Reference Counting

I'm having trouble understanding what's happening here. I am debugging an app because I think it's retaining a class 'DownloadController' after its been used. Using ARC, it should automatically release the object when the Reference Count is zero.
So in instruments it looks like
From that I assume, that 48 bytes have are still 'live' and being used, correct?
So I try to find what is causing this object to be retained, when its no longer needed, by using reference count traces.
At the bottom it says, RefCt is zero; so why has the object not been released? How can I debug further, why it hasn't been released?
These ARC anomilies were being caused by NSZombieEnabled, after removing this argument it started freeing objects.

Does FastMM only report memory leaked and/or memory allocated?

I am debugging my program with FastMM and it seems that a lot of the memory leaks it is reporting seem legitimate memory allocation not an actually memory leak.
For instance,
A memory block has been leaked. The size is: 20
This block was allocated by thread 0xD44, and the stack trace (return addresses) at the time was:
404902 [System.pas][System][#GetMem][3693]
406597 [System.pas][System][TObject.NewInstance][11044]
406B2A [System.pas][System][#ClassCreate][12121]
60A1D2 [CtrlObjs.pas][Ctrlobjs][TConnObj.Create][430]
61703B [Control.pas][Control][TControlMgr.FindLinks][854]
60ACB2 [CtrlObjs.pas][Ctrlobjs][TControlObject.FindLink][746]
60E2A3 [CtrlObjs.pas][Ctrlobjs][TDelayControl.EvalPulse][2105]
60E4C0 [CtrlObjs.pas][Ctrlobjs][TDelayControl.Evaluate][2193]
6102D4 [CtrlObjs.pas][Ctrlobjs][TLineControl.Evaluate][3155]
60ABF1 [CtrlObjs.pas][Ctrlobjs][TControlObject.ActiveCount][711]
6105D8 [CtrlObjs.pas][Ctrlobjs][TLineControl.ActiveCount][3261]
The block is currently used for an object of class: TConnObj
TConnObj is class that is frequently used to create an object and be destroyed when the program is not need it anymore. However, FastMM is reporting it to be actual memory leak. So, how do you tell which is which when looking at the FastMM memory leak log file?
If FastMM is reporting it as a leak then it really is a leak.
You have created an object and failed to destroy it before the program terminates. Usually this is due to a straightforward error in your code. Perhaps you omitted a try/finally to protect the object's life.
If it's a global scope object that is not being destroyed then you could simply destroy it at program termination. Or you could call RegisterExpectedMemoryLeak to indicate that you do not intend to destroy that object. But only do that when you intentionally leak an object. Don't use it to paper over a leak that is not intentional.
But the bottom line is that FastMM does not lie. If it says you are leaking, trust it.

Resources