Instrument finds leaks on Simulator, but not on the Device - iphone

I was tuning my iPhone app before submission to AppStore, when I noticed that Instrument finds a lot of memory leaks if I run in simulator|debug... but if I run it on the Device Instrument doesn't find any one.... To be honest, there were two leaks and I've fixed them. Now no leaks on the Device, but still too leaks on the Simulator.
Who's right??

Trust in the device.
The simulator will be using a slightly different memory model - always test on the device :)
Sam

Did you have NSZombieEnabled set for your simulator build? That will cause a lot of leaks to be reported.

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?

app crashing on device

the app I've installed on an iPhone 3G device periodically crashes. I'm wondering if it's the device or the code. the code works fine on the simulator.
I have other apps from the app store that do the same thing. e.g. the stackoverflow app - whenever I do a search the app crashes. I've even had the iPod app crash too.
I've restored the phone a few times and still the same thing. I'm getting ready to upload the app to the app store and was wondering if anyone had any tips to make sure that my code is not the problem. it's difficult to tell because there are no errors in the simulator or memory leaks in Instruments
thanks in advance.
You can be guaranteed that it is your code. Run it in the debugger on the device and see where it is crashing. If you are getting EXC_BAD_ACCESS use the following to help track it down
http://www.cocoadev.com/index.pl?NSZombieEnabled
It most certainly is your code that crashes.
Run in debug mode and watch the console (real debug mode, not only the debug target).
Also, you can (and should) grab the crash report from the organizer. You can also watch the device's console there.
Are you sure you are not running out of memory?
the easiest way to see if this is your problem is to implement didReceiveMemoryWarning method of your controllers and check if they are called.
In the simulator you can simulate a memory warning but I am not sure if even in simulator app will crash if memory is not released. (I believe not)
Also run your app with instruments, using allocations: Run > Run with performance tools > Allocations.
And use your app for a while and you will see. ;)

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 Simulator leaks vs iPhone Device Leaks?

Is it possible that there will still be leaks when running my app on an iPhone even if the simulator has absolutely none??
If you run your app through Instruments, and there don't appear to be any memory leaks, you're probably okay. Make sure to thoroughly test the application, to cover as many scenarios and as much code as possible.
No software is perfect, so there's always a chance, but if you are thorough with testing, you should be fine.
It is possible that some memory leaks to be present when tested on a real device. Since the firmware of the device is different than of the simulator, and also due to the fact that simulator is a software, that won't always be perfect as andy suggested, you have to test for memory leaks in the device itself before submitting. Simulator may not always catch all the memory leaks.
My first answer would be: Yes, always. All that matters is the hardware in the end. The simulator is just there to reduce the time between iterations when testing.
My second answer would be: it's possible because there's no way to prove that an app won't leak memory. See also: the halting problem.