Concerns about testing an app on actual iphone - iphone

I would like to start testing my application on my iPhone. However, I have one concern.
What if my app has many memory leaks? If i ran such an app on the iPhone multiple times, wouldn't that result in a crash on my iPhone? i.e memory keeps getting allocated but never deallocated?
should i worry about accidentally creating unnecessary files on my iphone such that each time I run i somehow create a file that i dont delete, and it keeps taking up a few kb of space. I test the app many times and after a while i have no space left on my iphone. can such a thing happen? can testing on an actual device damage my phone???
Thanks!

What if my app has many memory leaks? If i ran such an app on the
iPhone multiple times, wouldn't that result in a crash on my iPhone?
i.e memory keeps getting allocated but never deallocated?
Memory leaks are only a problem as long as the memory is still allocated for your application. As soon as your application closes, or crashes, its memory is deallocated and returned to the OS. If you are running iOS 4 you can double click the home button and close the application to free up memory.
should i worry about accidentally creating unnecessary files on my
iphone such that each time I run i somehow create a file that i dont
delete, and it keeps taking up a few kb of space. I test the app many
times and after a while i have no space left on my iphone. can such a
thing happen?
Yea you should take care to create as few files as possible. However, because all files created by your app are contained in a little sandbox that is associated with your app they are deleted when your app is uninstalled. If you feel like your test app is taking up too much space simply uninstall it and then install it again using Xcode. Although I wouldn't worry too much about this as you would have to create A LOT of large test files before they started filling up your phone.
can testing on an actual device damage my phone???
I highly doubt its possible. That is one of the reasons that apple restricts which API's you can and can't use. If you jailbreak your phone and start doing things apple doesn't want you to do all bets are off.
I wouldn't worry too much about messing up your phone by developing on it. I've never heard of someone damaging their phone doing this and if for some reason you phone starts acting up just restore it in iTunes.

Here's a tip. Use Analyze tool in Xcode. Find leaks and remove them. And check the performance using Instruments. Fix your memory leaks using that as well. Once you do both diligently, there should be no reason to worry about leaks anymore. Your other concerns are a bit unnecessary. If you are using only the public cocoa touch APIs, your device should be perfectly safe :)

Also be sure to clean up any unused resources, etc. When you send it to the background, it only does so much. Make sure you release objects you own, and release the resources. Set autorelease when you can.

Related

iPhone iOS what is the most accepted way of dealing with memory warnings?

I've been surprised that iOS 5.1 does not manage memory quite as I expected. When the device is running a lot of apps, it appears that iOS does not kill memory hogging apps in the background, but sends memory warning to my own app as well.
For example showing a UIImagePicker crashed the app on two test devices. Double tapping the home key and killing some of the background apps prevents the app from receiving the memory warning and crashing.
I'm wandering if iOS would not free up memory for me, is it acceptable to show some sort of alert view notifying the user that the memory is low and some of the background tasks have to be killed?
I'm at a loss of how to deal with such events - does it take time for iOS to clean up some memory (while apps respond to memory warnings)?
iOS does a lot of stuff before bothering you with memory warnings, including killing backgrounded Apps. Since iOS 5, iOS is even going to annoy you as less as possible with memory warnings, meaning that you only get one if there really is a need for you to get rid of stuff that is using memory but not needed right now (and that you can safely recreate in the future without taking hours for it). If your App crashes without giving you a memory warning first, chances are that you allocated so much memory that the system can't tell you that its running out of memory before it decides to kill you, the reason for this is that the memory warning is scheduled on the runloop of the main thread and until you give the runloop time to do another iteration, you won't receive the warning.
Also, Apple doesn't like you to tell the user that there is a memory problem; Its your App that has to deal with it, not the user! So its very very likewise that your App gets rejected if a memory warning comes up while the review team is reviewing your app (rumor says that they send these warnings to test how your App reacts to them)
Soo, to sum it up: iOS does work like you expected by killing what it can and even shutting down other system daemons, only after this happening you will be notified that memory is low. The correct way to respond to these warnings is to free up as much memory as you can, start with the big stuff that can be easily recreated in the future (eg. if your app shows loads of pictures but not all are visible at a time, throw away the ones that aren't visible right now). Telling the user is the wrong way to deal with the problem and Apple doesn't like it, so try to solve the problem on your end.

iPhone: leaks from other Apps taking up RAM?

I'm reading some people stating that if another (3rd party) app on someone's iPhone has been leaking memory, that this may reduce the (mystery) amount of RAM your app would otherwise have available.
This confuses me -- does not all app memory get released when the app is closed by the user? And only one app is open at a time on iPhone?
Normally, any memory that your application allocates will be freed when it exits. However, many of Apple's applications continue running after they're "closed", so memory leaks in Mail, for instance, can affect available memory.
In addition, there are apps out there that claim to free up allocated memory. They really don't do anything other than force some dirty pages out of the buffer cache, but they appear to do something, so people believe they must be doing something useful.
On a jailbroken phone - yes, third party apps can be running at the same time as yours. Running out of memory is common with people who like to have many apps running at once hence the need for task managers, killing tasks etc.
On an unhacked phone - no. Yours is the only non-apple app that is running, no others can run at the same time.
So what can you do? All you can do is try to use the minimum memory possible which you're probably already doing. Realistically you can only test with a factory, unhacked phone, unless you are going to spend hours trying to please everyone. If you think you are maybe using too much you could identify the larger allocations using the instruments tools ("Run with performance tool >" from within Xcode) and then post that chunk of code here to get ideas of how to reduce it.
You should run Instruments and then add the instrument "Memory Monitor" to see the memory use of all of the other processes on your phone. (Add with Window -> Library , then drag the Memory Monitor instrument to the instrument panel.
What I'm still trying to determine is why is iOS releasing memory from MY app, and not all of the other memory pig apps that are not currently running.

Do I trust ObjectAlloc or Leaks for analyzing my iPhone app?

When I run my iPhone app with "Leaks" (which has a section for Object Alloc), my app seems to be fine for memory allocation. However, when I run it with just the ObjectAlloc tool, the memory increases steadily as the app runs its main timer. (It is a timer based app). I'm not sure what to trust. I was just wondering if there are any issues with the ObjectAlloc tool that might pertain to me. Maybe something related to NSTImer? I'm running this on the device (not the simulator). Thanks.
Yes -- trust the tools. They are really quite accurate these days.
Leaks means an object or allocation for which the address of said object/allocation isn't stored anywhere else in your app. The memory is no longer accessible.
However, eliminating all leaks does not mean your app cannot grow without bound.
Unbounded growth can happen for a number of reasons. You might have a cache that keeps adding entries without pruning the least recently used entries. Or maybe a transaction log that is never truncated or flushed to the filesystem. Or you might keep loading new images into your application without throwing out the old.
Once you have eliminated all leaks, look at the output of ObjectAlloc and figure out where all that memory allocation is coming from. In particular, you'll want to figure out what your app is doing to trigger the allocations. The system frameworks won't spuriously cause continual growth without your application directly or indirectly asking for the resources to be consumed.

iPhone: Can my Leaking App harm other apps?

So, I finished writing my first iPhone App, and I have sent it out to a group of beta testers.
Everybody is happy, except for this one guy who noticed that after having run my app, another app is not starting up anymore.
Not knowing too much about memory management, I started looking at the Leaks graphs in Instruments, and noticed, that there are some leaks going on in my app.
Three questions:
Are leaks always bad? The biggest leak is 15k on a total of 5,1 MB allocated memory.
Will Apple refuse my app because of leaks?
Is not all the memory automatically freed up as soon as my app quits? Could it be that my leaking app is harmful to other apps?
Thanks
Sjakelien
Each app is supposed to run in its own "sandbox," and is by design not supposed to affect any other app installed on the phone. If your reviewer found a case where your app really did affect another app, that's definitely not your fault, although I'd love to know how that was accomplished ;).
My guess is, though, that your reviewer's other app broke completely independently of your app, and he/she is spuriously attributing the fault to your app. Ask him to try to reproduce the problem (uninstall both apps, install the other app, install your app).
what the other two answers say i agree with, however nobody has answered the first question ("Are leaks always bad") directly. i would say that yes they are. it's true that your program can possibly run with them for long periods of time without issue. and the OS should clean up the memory after termination (unless there's a bug in the OS, as said before). but eventually the leak will become an issue for someone at some point when they run long enough. also, even if it doesn't become an issue, it's probably a best practice to always fix memory leaks that you know about.
Any leak that persists beyond the exit of your application is an OS bug, and hence Apple's problem.
It would be unfair to reject your app on this basis.
I don't know if that's any comfort to you.
For the record, my app provokes a leak in mediaserverd.
The leak is a tiny bit smaller in the 3.0GM.
Another incorrect usage of an API crashed mediaserverd.
Also 3.0GM. Nasty. I'd have preferred an error code, however
this could theoretically be used to work around the leak.
It depends on what "memory" you are leaking. Are you using device storage space to cache something? In theory then it's possible that you've used up enough space the other app cannot start because it needs a certain amount of free space.
If we're talking physical memory, then no. When your app is dead it is dead. You could ask them to restart the device and see if they have the same issue.
Apple does approve apps that have memory leaks.
That said, we thoroughly check that none of "our code" leaks before submitting to Apple because users don't like having an app suddenly shut down on them. I say "our code" because there are memory leaks in the framework that are not your doing. Thus Apple approves apps with memory leaks.
Also, I am doubtful your app has adversely affected the user's other app. I have not seen that behavior.

Game crashes on the actual iPhone but simulator? memory problem?

I have built my first game using Cocos2D. It worked fine on the simulator. But when it runs on the actual iPhone, it crashes. I don know why. Thought it was memory leaks, so i tried to detect, but no leaks found. I tried to increase and decrease frame rate, neither both succeeded. Anyone experienced please help me out. I am really stressed now. If anyone had the same issue please share with me your opinion.
Yours thanksfully.
I've run into similar issues (I also use Cocos, but I don't think this is Cocos specific). The best thing to do is plug-in your iPhone and watch the stacktrace when it crashes (or retrieve the stacktrace after the fact)
This happened to me a lot because the resources between the iPhone and the simulator were not in sync; in other words, some how resources would be available to the simulator (eg: images) but those same resources were not transferred to the iPhone for whatever reason. Sometimes, if I ran 'clean' on the simulator, I would observe the same issue.
It's extremely frustrating to debug these types of issues, but you'll get used to it.
I agree with Dominic - we definitely need more information to be able to help you - do you have the output from the console or the stack from the debugger?
Also, while memory leaks are a Bad Thing, they rarely lead to crashes directly. They will increase the amount of memory used by your app so if you're memory intensive then you might get a problem but they're not the first place I'd look to try to debug this.
Try running the app in Instruments and watch the memory usage graph - then not only can you see the total that your app is using but you can get an idea of which sections of your app use the most and can focus your efforts in reducing it.
Sam
Sorry, but you need to specify more details. How does it crash? What does the error log say? One thing you might look into is the amount of memory, your game consumes. If it uses more than 64 MB on the actual device, the OS will very likely just terminate it. In the simulator on the other hand, your app might use lots more memory without a problem.
I have found tools like Instruments and NSZombieEnabled to be very helpful in tracking down issues such as these.
Without more information, I would try the following steps:
Delete the app from the device and simulator (using the tap-and-hold technique to make your icons jiggle) and reinstall it. Sometimes a setting (or lack of a setting) in the user defaults will cause a crash, and those don't get wiped out unless you delete and re-run your app.
Also try the "simulate memory warning" option in the simulator and see if that gets it to crash.