Finding leaks under GeneralBlock-16? - iphone

If ObjectAlloc cannot deduce type information for the block, it uses 'GeneralBlock'. Any strategies to get leaks from this block that may eliminate the need of my 'trial and error' methods that I use? The Extended Detail thing doesn't really do it for me as I just keep guessing.

I find that sometimes if in the leaks instrument you click the button that looks something like this:
{= and drag your app delegate file onto the screen, it will lead you in the right direction by highlighting the code that allocated that leaked block.
Every time it goes into a function call drag the source file with that function onto it. This can be hit and miss though as sometimes these mystery leaks aren't tracked back to the delegate.

In XCode, you can try build and analyze. Sometimes it can be helpful in finding leaks and sometimes not. But it's worth a shot.

I've just started using objective-c and xcode (version 4) and it appears that by clicking on the leaked object in the memory profiler, you can see exactly the point in the code it is referring to in the "extended detail" pane. In here it shows you a stack trace and clicking on a point in the stack will take you to the exact point in the code where the leak is occuring. Not sure if this was available in v3. Hope this helps anyone else tracking down a leaky GeneralBlock-16.

Related

How to exactly find where memory is leaking in project of iPhone

While developing apps in Xcode memory leaks are occurring. When I checked them in extended detail view they are showing different methods that are not related to implemented. How to exactly find out which object is leaking and where it is leaking memory.
When ARC is enabled we have to take care of memory leaks or not?
Even with ARC memory leaks can occur, it just inserts release and autorelease during compile time.
1. You must check for leaks using Build and analyze in XCode, shift+command+b you should be clearing those issues.
2. After that you can start using the instruments by using profile option command+i . This will point you to where the leak might be.
This link will help you too http://soulwithmobiletechnology.blogspot.in/2011/04/how-to-check-memory-leaks-in-xcode-4.html
Edit: Added some screenshots to hopefully make it clear.
During profiling after selecting leaks choose the call tree option and check the boxes hide system libraries , invert call tree and show obj-c only as shown in image below.
After double clicking the symbol name below you'll get the line where it is leaking.
You'll get something like this.
Yes, even with ARC there are memory leaks. ARC is not a garbage collector, it only inserts for you, at compile time, retains, releases and autoreleases in key positions. So although it does help the developer, you should be aware that memory leaks still exist (for instance circular referencing). You can start by using the Static Analyzer and correct any problem it shows you. You can then go to Instruments and choose Leaks.

EXC_BAD_ACCESS while accessing some files

I have a bunch of .gif files that I am displaying, in turns, in a UIImageView.
On the click of specific buttons, the respective gif images are to be displayed.
Some of the gifs display just fine when i click the button but for most of the cases, the App crashes without any error message.
Debugging shows 'EXC_BAD_ACCESS' but when I add a breakpoint and step through the program, all the gif files display correctly on clicking Continue.
Anybody knows what could possibly be going wrong?
Its something related to memory management i suppose but i have no clue as to where i should go looking for the problem area.
Thanks
This is probably about memory management, yes. You might want to turn "Zombies" on for further investigations in that direction. However, this will only tell you if there are messages being sent to prematurely dealloced objects, not how to prevent it.
See this document to learn more about proper memory management:
Memory Management Programming Guide
These two links explain more about enabling and using NSZombie and handling EXC_BAD_ACCESS errors:
NSZombie and XCode Oh My!
SO Question about EXC_BAD_ACCESS

Tracking down EXC_BAD_ACCESS without NSZombie?

I've spent two days on this and I still can't find the cause of the crash.
I know it has something to do with trying to access an object that has been freed but I don't know which access or which object.
Whenever I google trying to find the source of EXC_BAD_ACCESS people suggest using NSZombies. The problem is when I enable zombies (either in xcode via environment variable or in instruments via ObjectAlloc properties) the program doesn't crash where it usually does and the zombies don't report anything (nothing shows up in logs and nothing is flagged in instruments). Is there something I'm missing with NSZombie?
I've tried utilizing some information from xcode's debugger and from ObjectAlloc in instruments but all the information is very cryptic and doesn't really help me.
I've set the debugger to stop on objective-c exceptions. When it does it this is the call stack shown:
0 objc_msgSend
1 ??
2 -[UITableViewCell removeFromSuperView]
3 -[UIView dealloc]
... etc ...
First of all, what's the deal with '1 ??' ? What do the question marks mean? Second of all how can I know where this was called in my code? All the operations stated are too generic (eg UIView dealloc, but which UIView? And where?).
Also when the exception occurs it points to assembly code which again, doesn't do me any good unless I was to spend hours trying to figure out what the code does. I know some assembly but there has to be a better way... right?
Is there any way I can get meaningful information as to what was the last line that ran in my code before the exception occurred?
I've tried sprinkling some NSLogs and breakpoints around but it doesn't help me much because the crash happens after I pop a view controller from a navigation controller. Everywhere I place breakpoints the breakpoints are reached fine (I can't find a point to break after the crash). It's only when I 'continue' in the debugger that the exception occurs. It's as if the crash happens outside of my code so I have no idea where to get a handle on it.
I've looked through my code and double checked that I adhere to all the memory management rules (at least to the best of my knowledge). I'm sure it's something very subtle but I can't seem to find it.
If anyone has any ideas how to find such bugs without NSZombie please share.
Thanks.
Well I found the problem. I had a custom table cell class in which I called [super dealloc] first in the dealloc method (rather than last). I guess I wrote this class in a hurry and didn't really think about it. I'm guessing something got released in the parent that the child needed for releasing?
So I don't have a real answer to my own question but basically I found the problem using a combination of ad-hoc code tracing and various debugging techniques (breakpoints, NSLogs, trying to decypher cryptic stack trace,etc).
Actually the main strategy that helped me was commenting out code bit by bit until I stripped down the problem area into as simple as it gets while still keeping the crash intact. This let me realize the problem wasn't where I thought it would be but in a more subtle area (eg in the dealloc method of a secondary class in this case).
I hope this can sort of help someone out. I'll leave this question unanswered for a bit incase someone has a more thorough debugging strategy without relying on NSZombies. Also if someone can clarify what those two question marks mean in the stack trace that would be helpful.

IPHONE: Analyzing leaks with instruments

I am trying to locate leaks using instruments, but the leaks I see there are like the ones in the next picture:
leaks
As you can see there's no information of which line of code is exactly leaking. All leaks I have, around 20, are like this, or in other words, the leaks don't show any line of my code in particular.
The leak in this picture is related to "_CFAllocatorSystem" (???) on the CoreFoundation and I have others that simply say GSEvent. I have no clue of what's generating these.
How can I discover that?
thanks for any help.
I think you want to go into instruments after running under leak and select "Source View". Then you need to drag your source files into the instrument window. It will then show the lines in the code where the leak occurs along with the call stack.
Some toss off code of mine leaks a view. It looks like this in Instruments:
alt text http://img688.imageshack.us/img688/9669/screenshot20091028at131.png
The thing that Leaks shows you is, the trace to the code that allocated the object that is leaking (which means it is retained but your application has no variables left that have that address). What it does not show you is just where the object should have been released to not cause the leak, for that is impossible to know (it is possible to find where release is currently being called, but that may not be so helpful).
So what this trace is telling me, is that some bit of memory allocated by the system is being retained by you, and then the reference forgotten - one key is the "PurpleEvent" line, which is common in a thread dealing with timer events or perhaps notifications. It could be you received a notification and kept something from it, without releasing it later on.
If you know about at what point the leak occurs, you should be able to isolate what code is running during that time.
See here and especially this quote:
This list informs you about the leaked objects' types, sizes, addresses, and even their call stacks.
Then you can trace the source of leaked memories through the call stacks.
The stack trace shows you exactly which line is the culprit. Apparently line 14 in main.m in your case. Dont know what you confused about?
The guilty was the accelerometer and I am compiling for OS 3.0.
In other words, the accelerometer that Apple said its leaks were fixed, is still leaking like hell.

How do you detect memory leaks on iPhone?

I'm using the Leaks Instruments feature through Xcode to (try and) find memory leaks. I still haven't figured out how to use this program. I click Leaks in the program and see memory increasing as I do various things in the simulator. I have Extended Detail pane displayed. The only thing in Extended Detail pane that references my app is main. As in the main method produced by Xcode. Everything else is UIKit, Foundations, and other SDK classes I didn't write. What am I doing wrong that nothing is showing up from my app?
Before I hit 3 minutes, there are over 100 leaks totaling 2.5k. Is this common?
I've written up a Tutorial on using Instruments to track iPhone memory leaks. I'm not sure if it will help you with what you're dealing with or not...couldn't hurt, though. :-)
http://www.streamingcolour.com/blog/tutorials/tracking-iphone-memory-leaks/
Change the view to "Extended Detail" on the instruments panel. This will show you the stack trace of each leaked object after you stop recording and select the leaked object.
You do see calls into the API, but what you are interested in is finding the last method of your application before the API calls, that is where the leak is.
A tip: turn on "gather memory contents" in the leaks view. Seeing the object values should also help finding where the problem is.
You don't want any leaks. 100 leaks is not typical (at least in my apps ;) Typical should be 0.
I'm not familiar with how to use Leaks, but you can always try running the Clang analyzer on your code to see if that'll turn anything up: http://clang.llvm.org/StaticAnalysis.html. It can often find many bugs that might lead to memory leaks.
Xcode: run -> Start with Performance Tool -> Leaks
Keep in mind that the Simulator may leak when the device will not. Ran into that once already with UITableViewController class.
Use LLVM/Clang Static Analyzer.
Note also that the leak tool is not going to show you instances where objects are over-retained and still held on to. Leaks are cases where objects that should have been let go are just hanging around with no-one to clean them up. Over retained objects are validly held onto even though you'd think they should be gone - thus the leak tool cannot point them out, since they are still referred to and there's no way to tell them apart from objects that should still be retained.
To find those, use the memory reporting tool and make sure that memory use goes down fully after you free an object. If you notice something isn't freeing memory, you can start by putting breakpoints in dealloc to see if what you expect to see released is actually getting released.
You need to look for both cases to keep a clean memory footprint.
Run -> Start with Performance Tool -> Leaks
To detect memory leaks you can use the "build and analyze" function of Xcode.
Simply select Build -> Build and Analyze in the Xcode menu.
Made a sum up of the main memory leak tools: http://bcaccinolo.wordpress.com/2010/09/15/iphone-essential-performance-tools-list/
Leaks application that can be found in Xcode: run -> Start with Performance Tool -> Leaks.
Appleā€™s Instruments utility that can be found in /Developer/Applications/Performance Tools.
One of the best way to find the memory leaks is Select Product-> Analyze. In the left Xcode shows in which file you have memory leaks. What are the variable causing memory leaks. This is one of the best way to find memory leaks.
The memory debugger (button just over the console, next to the view debugger) is quite useful too.
It will show you leaks, and you can check /filter easily if objects are still in memory when they shouldn't.