Strange memory leaks when running 'Navigation Based Application' template project - iphone

I did a very simple test which was creating a blank project using xCode and execute on device. When executed with Instruments I got memory leaks !
Please note that I am using iPhone 3G device running iOS 4.0.2
Here are the steps to reproduce:
From xCode 3.2.3 Choose File -> New Project -> Navigation-based Application selecting Use Core Data for storage
Choose Device - 4.0 as the build Target
Choose Run -> Run with performance tool -> Leaks
On the iPhone 3G device (running iOS 4.0.2) click the '+' button several times in the launched App.
Here is the Instruments screenshot:
Can anyone confirm this issue?
Thanks,
Joshua

It looks like you're getting leaks related to WebKit (WebCore, JavaScriptCore, etc.). I had the exact same leaks in a few of my projects with iOS 4.0, but the apps still got approved by Apple (because the leaks are not your fault).
The good news? I recently tested these same apps with the 4.1 SDK, and all of my WebKit-related leaks are gone! Since you are using 4.0 I suggest you try the 4.1 SDK to see if they've disappeared.

Cocoa has always had very small leaks in it, and it occasionally has things that look like leaks and aren't really. When looking for leaks, what you are looking for are leaks that grow over time. Seeing a single leak report like this of a few kB is not a cause for concern. If you're running this on the empty template project from Apple, you should open a radar (bugreport.apple.com), since it means that there either is a small but common leak in Cocoa or Instruments is wrong. In either case, it should be fixed, but it's not something to lose sleep over.

Related

memory-leak happens when running on physical iOS device

When testing my app, everything runs fine on the iOS simulator, but I experience memory leaks when running the same app on a physical iOS device. I am new to developing iOS apps and I know that are many potential reasons for this. I want to know why things are different between the simulator and a physical device and if there is a way to solve my memory leak issue?
I would suggest you go through the tutorial below. It will help you a lot to understand how to debug your memory problems and other issues.
My App Crashed – Now What? 1/2
My App Crashed – Now What? 2/2
hint: your problem can be solved with zombie objects; but I advise you to follow the tutorials first.
I'm surprised that you have a leak in your device and not in the simulator. How did you conclude that? Both could be leaking, but the problem is just being manifested differently on the different platforms or simply encountering the problem more quickly on the device than the simulator.
If you have a leak, I'd suggest two things:
Run your code through the static analyzer (select "Analyze" from the "Product" menu in Xcode). Newer developers tend to ignore this incredibly useful tool. I know that I finally mastered my Objective C memory management techniques only after going through the analyzer results and really understanding (and resolving) each and every issue in a really big project I had. Anyway, if you're getting any warnings there, you really should fix them, as they're often indications of memory management problems.
Run the program through the profiler. See the Finding Leaks documentation.
Yes, there is a difference (sometimes an annoying large one) between the simulator and the device. This is because the underlying hardware of each is quite different. Obviously the simulator is simulating the iOS hardware, but that doesn't make it foolproof. With certain APIs and frameworks, things may ONLY work on the hardware, and sometimes bugs will occur on the hardware that didn't occur on the simulator. Just think of it as the "adventure" of iOS development.
As for resources, I'd take a look at #Arab_Geek's links, and also Apple's Tools for iOS Development - specifically Instruments, like Leaks.

How to find memory allocations and leaks in iphone applications without using instruments?

I am planning to create a iPhone/ipad application which can check for allocations and leaks of the applications available on the device. Currently i don't know whether there are any existing API's(which may be private since am not planning to submit it to app store) for achieving this.My thought may be bit crazy but can anyone lemme know whether it's feasible or not.
iOS applications are sandboxed -- one app can't look at the memory or resources of another. So there's no way you're going to be able to write an app that detects leaks in other apps. The story may or may not be different if you're willing to jailbreak your device, but finding information in that case will prove difficult.
Update 1: (got your question wrong!)
Analyzing other apps within a iOS app is not possible on un-jailbreaked phones (sandboxing).
If you can do a jailbreak, try to compile valgrind on iOS (very hard!) and build a GUI (iOS app) around it.
if you search leaks in your app (different question):
For memory leaks you don't need API's.
I would recommend you...
... use ARC.
... use valgrind (popular c instrumentation framework) for iOS (simulator only).
When you find and fix leak in the simulator, the chance is >99.9% that they are also fixed on iOS device builds.
But why not using Instruments?

Is use of LEAKS instrument still common on 3G iPhone?

I'm working with an iPhone 3G, and when I'm trying to investigate memory leaks using the LEAKS instrument, my app crashes. It does not crash when LEAKS is not used.
I'm making no claim to having a bug-free or non-memory-intensive app here. But I'd like to investigate leaks on an actual device. When I'm running LEAKS it is incredibly slow.
Are there still developers working on iPhone 3G? I don't want to be the whiny guy blaming his tools, but I'd also like to be sure the whole dev world hasn't moved on to iPhone 3GS and I'm the only one trying to run both my app and leaks on a 3G.
Currently running iOS 4.0 "gold". Snow Leopard dev env with latest XCode.
the number 1 tool is probably the static analyser, which you have since you have Snow Leopard/latest Xcode. Just hit shift-command-A to run and click the blue boxes in build results to see object lifecycle and leakage. You're not using NSZombieEnabled and Leaks at the same time are you?
Maybe the crash comes as a result of instrumenting the build so that Leaks can do its job.
Getting a clean analyser build may well improve things to the point where you can run under Leaks. Generally these days I consider a clean analyser build a very good indication that I've done all the leak chasing I need to - while Leaks might show more on occasion I have yet to find anything with Leaks that was not also reported by the analyser.
It shouldn't matter that you are running on a 3G rather than a 3GS, I often work with an "old" 3G - the one nobody wants to carry and so it is set aside as the development phone - and from a development point of view there really isn't much difference.
Your problems with the Leaks instrument may be due to the frequency at which it is scanning for leaks in your application. These periodic slowdowns may be causing problems with the execution of your application.
You can disable automatic leak checking by selecting the Leaks instrument and unchecking the Automatic Leaks Checking option on the lower right. When you are ready to check for any leaks that may have accumulated, click on the Check for Leaks Now button below that checkbox option.

iPhone: How to fix a legacy iPhone app which has really bad memory management? unittesting?

I recently inherited an iPhone app. The original developer did not understand memory management and well the app works in simlulator but not in on old iPhone (lots of crashses). Do you have any thoughts on the process by which I can save the app?
Can I utilize or create any unittest to find memory leaks and make the process 'scientific'?
Thanks
Yes! Use Clang! Here is a good tutorial showing the benefits: http://iphonedevelopment.blogspot.com/2009/02/clang-static-analyzer.html
However, it should be installed if you have a newer SDK.
Since the Clang Static Analyzer is now built-in to Xcode on Snow Leopard, it's trivial to use it. Select Build -> Build & Analyze to see any memory problems Clang detects (hint: it will find pretty much all of them).

Core Data Instruments for the iPhone

I'm trying to monitor my core data usage but all the Core Data instruments are greyed out in Instruments. It's also not available from the "Run with Performance Tool" in Xcode. It says that the Core Data instruments are for the Mac and it's not available on the iPhone, but I've heard people talking about using it, and it's mentioned in the Apple docs too.
How can I get them working?
Thanks!
You can run Core Data instruments against the simulator only, not the device.
Open Instruments and select the Core Data items you want, then use Instruments to launch the simulated iPhone app, instead of launching from Xcode.
Hopefully this support is better in future versions. Also, I believe it only works with 3.1, not 3.0.