memory-leak happens when running on physical iOS device - iphone

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.

Related

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 there a performance hit on development-enabled iOS devices?

Here's my situation:
I've got a personal iPhone 4 that I use day-to-day, and I was thinking of using it for development.
If I do enable my iPhone for development, will there be any noticeable difference with regards to the performance of the device?
Thanks
Not significantly. If there's any diagnostic information that needs to be collected, it's crash logs, which are already generated on a regular basis by all devices from day 1. When you enable your device for development, Xcode just collects information about it once, presumably to know how to decide how to work with it as you build and test your apps.
You may have debug versions of your apps on your iPhone, but I seriously doubt they will affect OS performance. Even if they do stuff that causes them to slow down it should only happen while their debuggers are running.
Anyway, when it comes to using your personal device for development, I am sure there are other, more important concerns than performance.
Anecdotally, my white iPhone 4 disagrees that it was "violated" in any way by Xcode. In fact it seems pleased to work with it. But that could be just because it knows a developer owns it, so it gets itself in the testing mood.
I think the performance difference is minimal. All iOS devices in our house are enabled for debugging (because I like to try out my apps on all sorts of devices) and I haven't been able to tell the difference.
But it might be something personal, something you would have to try for yourself. Just make sure you make a backup of your device, before enabling. If you think it slows down your device to much, just restore to the backup.

What are the potential issues of jail-breaking to test the new background APIs on a 3G?

I've registered/paid for the iPhone developer program, which allows me to test apps on a device. I have an old 3G that I would like to use. However, I'm interested in using the significant change notifications as well as it working in the background.
If I were to jailbreak to enable background processing and things like that, what issues could I have, and how likely are they to arise?
EDIT:
I actually do have an iPhone4, but I'm using it for my personal phone. I've heard that it's not to great of an idea to use your everyday phone as a development device. I've done a little more reading, looks like I could use Ad-Hoc distribution to test while avoiding the problems of using your primary phone as a development device. Seems like the only downside of this would be no debug info, other than logs.
iOS 4 has been hacked to allow home screen wallpapers and multitasking on iPhone 3G. (http://pixelatedgeek.com/2010/06/ios4-jailbreak-and-multitasking-now-available-for-iphone-3g/)
It seems to be running fine without any issues, probably not as fast as an iPhone 3GS or iPhone 4.
Just be careful if you use this as your main testing device and make sure you test on supported hardware before releasing. Personally, I'd use something that was 100% supported to be sure everything worked as expected. However, if you don't own a supported device this may not be possible.
Again, make sure you test on friends 3GS or 4 before releasing. I don't foresee any major issues, but testing features on hardware that isn't officially supported could cause problems.
Update: I've used my phone for development purposes before and haven't run into any earth shattering problems. If an issue comes up you can always restores your device.
If you are planning to target iOS4 you really, really really should spring for an iPhone4. You can understand how things will run on it, and also be able to see how the higher resolution display interacts with your app. I'm not sure running iOS4 on a hacked 3G will tell you much more than running in the simulator, and in fact because of the reduced memory may show some issues that just do not occur on real devices.
One other great option is a recent generation Touch. Although it doesn't give you the display to check against it is a good test platform for multitasking.

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.