NSTimer with Touch capability? Any other options - iphone

I am creating my very first iphone app and need help here. My programming background is not C and I am bit lost.
Basically what I want my application to do is to display random bitmaps and let user touch individual bitmap and validate them. The method I used to display the bitmaps is NSTimer with UIImage for the actual bitmaps. So far, it was working fine in displaying the bitmap. But I am stuck on the next step, which is registering the touched bitmaps. I tried to use tag on the bitmap then touchesBegan function. I got a runtime error everytime I touched screen (I think.)
My questions are:
1. Given my requirement, is the method I am using is a good method, or even technically possible?
2. If q#1 answer is no, do you have alternative solution?
Thanks and I appreciate your help!

If you're getting an exception inside your touchesBegan: function, then your code there has a bug in it. If you run your app with the debugger, it should tell you on exactly what line of code the problem is occurring. If I had to guess, I would say that you're trying to send a message to an object which doesn't handle that message, which throws an exception.
Make sure your code is compiling without any warnings -- it's possible you have a typo in a function name somewhere. If you misspell the message name, it will still compile correctly, but it will throw an exception at runtime when you try to send that message.

Related

Message sent to deallocated instance on device only

I'm at a complete loss with a memory bug. This issue only happens on the device and not the simulator. It also only happens when the app is loaded for the first time on the device. If I close the app and relaunch it the issue does not preset itself. This is the error I'm getting
-[CFString retain]: message sent to deallocated instance 0xfd5a2f0
I've tried everything to attempt to figure out what the released string is. Please help me figure out what the released object is when running on the device and not the simulator! Thanks in advance!
UPDATE:
I still do not have a solution but I have confirmed the app will crash consistently on the line of code where I set a frame and the above error shows. If I comment this code out the app will pass this point but may crash at other strange spots. I can't even begin to determine where the crash is originating.
CGRect frame = CGRectMake(27, 96, 265.0, 50.0);
someAcct.frame = frame;
I'm so confused by this since this is not a string. Any ideas?
UPDATE 2: This issue is directly related to the NSUser Defaults being synchronized. This seems pretty random but this is causing the issue. Any Help?
UPDATE 3: The problem has been solved as of a week ago. This was in fact due to a string being inaccessible. A string was passed to a method, that method then did work and passed data to another method and so on. Until finally the data was to be written to user defaults and a local plist contained within the documents folder. All of a sudden the app would crash at very strange places like the above consistently even though those places had nothing to do with a string. I was baffled and after playing with it for a very long time I decided to head over to the .h file and go through each string one at a time by making sure each were synthesized in the .m file and that self.stringName was applied throughout the .m file. After spending an hour on this I finally found the string that was to blame and the issue is now resolved. Using self.stringName allows the memory to be found and avoids these obscure and highly confusing application crashes. Thanks to everyone for all of the suggestions and tips!
To help you with tracing it down, could you try putting NSLog at the start of your didFinishLaunching() method, somewhere in the middle and at the of the didFinishLaunching().
Then also could you push a NSLog in the viewDidLoad() of the first view controller your app shows. For example, if you're using a tab bar interface, then a NSLog in the first tab's root view controller.
You can use NSLog(#"step 1"), NSLog(#"step 2"), NSLog(#"step 3") ... to organise the output so you know where the code managed to reach up to before it crash, rather than NSLog(#"I am here").
The problem is of retain count. Refer mememory management and check u r not missing something.
Don't forget to turn on NSZombieEnabled.
One reason can be due to the iOS compatibility.Check if your device and the simulator are using the same iOS.Try working the simulator on the same version of OS as your device and change the release of the string accordingly.

tap back immediately after moving the map crashes application in iPhone

I came across the scenario where in if after moving the map immediately if I tap on back icon while the map has not fully loaded
The application crashes.
What I can understand is Since the loading is still in progress and I tap back the the application releases the controller but the google map loads asynchronously in NSRUNloop (not sure). So that might be the problem not sure though.
So does anybody know what can be the issue and is there any way to solve this issue?
Please comment if more description required.
It sounds like that when you close the view, the object that is the delegate for the completed map load has been deallocated, causing the crash with a bad access.
A good way to get to the bottom of these types of crashes is to use Instruments (part of the Xcode suite to tools) and go zombie hunting.
For anyone who is still searching for the answer
What exactly happening was that map view events were getting fired even if the controller was released causing a crash in the app.
So the solution is Before setting the value of objMKMapView to nil you need to set value of objMKMapView.delegate to nil.

UITextView delegates problem

I am trying to access the UITextView delegates and have a problem
I have a UIViewController with the UITextViewDelegate protocol and a Nib containing the textView.
If I set the delegate inside viewDidLoad like "textView.delegate = self" and I touch the textView, the app crashes without logging errors.
If I start editing the textView with code like "[textView becomeFirstResponder]" all delegates get called.
When I set the delegate in the Nib creating a connection between the textView and the File's owner and deleting "textView.delegate = self" also no delegates get called.
What am I doing wrong here?
Regards,
Elias
It's not easy to help you without more description, posted code or a xib file.
You say application crashes without any logging errors - well, do you mean that there's no output in console's window ? That is normal, for an app that has crashed.
Anyway, you should be able to get the stack-trace to figure out where approximately the application has crashed. Open the debugger (⇧⌘Y), and see the position. That should give you an idea of what went wrong.
Here you can see an example of such debugger session (after EXC_BAD_CRASH):
First two lines doesn't give us much information, but later on we can see that application has crashed while loading user interface from a NIB file. Well, usually the only code that executes during such load are awakeFromNib methods - it's up to you to find a problem along those lines.
Often top of code's execution doesn't make any sense - for example you might see your ViewController method somewhere, but the top few function calls (those where the code crashed) are located in methods/classes which you never call in your code. In most cases that is a sign of wrong memory de-/allocation. What might happened is that you forgot to retain some of your objects, it has already been released, but you are still keeping reference (a pointer) to its memory. Because that memory has been in fact freed, another object took its place later on, usually some Apple's internal object you've never heard about. Later on your code tries to message your poor object but it sends a message to something completely different. BUMMER! That's how you get those crashes and strange stack traces.
To fix the kind of problem I've just described you can use Instruments and its Zombies instrument. Unfortunately you can't start Zombies from within Xcode, you need to start Instruments standalone, then choose the Zombies under iPhone Simulator/Memory, then Choose Target from the toolbar, you should see your application in there, or be able to navigate to it on filesystem.
What Zombies instrument does is that it never really frees memory after objects are deallocated. Instead, it will mutate those objects into NSZombie class. That class intercepts all calls to itself, and informs you when some code is trying to send a message to it.
This is how such Instruments session looks like (this is the same crash as seen in debugger above):
In the table you can see that we're trying to message UIScrollView that has already been deallocated. You can as well see the whole history of retain/release calls to this particular object. That way you can find a missing retain or wrong release/autorelease.
Remember - Zombies Instruments can only be used with Simulator, because there's not enough memory on the real device to keep all those memory blocks.
Hopefully I could help you with further analysis of your problem.

Memory management error, using cocos2d for iPhone

So I'm getting a EXC_BAD_ACCESS error in cocos2d. From what I've been searching so far it's mostly related to attempting to free an object which has already been released. I have encountered this error before, and its solution was simple and pretty much caused by freeing a released object. But now, using cocos2d (not sure if it's a bug in their framework or not), I'm getting an EXC_BAD_ACCESS in this line:
CCMenuItemSprite *btn = [CCMenuItemSprite itemFromNormalSprite:op selectedSprite:op target:self selector:NSSelectorFromString([sceneMethods objectAtIndex:i])];
Basically, I'm creating a simple menu system for easy maintenance and updating. Nothing too serious. In this particular line, I'm creating a CCMenuItemSprite with self as target and using a selector. I've already asserted that the selector passed as argument is correct and also tried to use different for the normal and selected sprite (though that shouldn't make any difference) but still no go! The error is deep in the cocos2d framework, precisely when the CCMenuItem is "activated" which calls invoke on a NSInvoker of that same class. And analyzing the stack trace, it crashes exactly on the invoke method, which leads to believe it has something to do with the NSInvoker. Anyone had a similar problem or have a suggestion for this problem? Thanks in advance.
Just a guess: are you completely sure that all of the objects in sceneMethods are real selectors?

warning: check_safe_call: could not restore current frame

What does the error warning: check_safe_call: could not restore current frame usually indicate? I've read in other places that it's a memory issue. Is it always a memory problem?
I'm getting this error on the device (not the simulator). NSZombieEnabled shows nothing. If I Build and Debug, my debugger window shows nothing. The peak memory isn't that high (3MB). It just crashes with the same error every time after scrolling around a map.
Any ideas how to debug this? Thanks.
Edit: I added the reason for my crash in an answer below (creating too many SQLite connections). If anyone else gets this error and finds their solution, please post it below. It seems like an error message with multiple causes.
I've seen this a few times, but never been able to actually pinpoint it to a problem outright. Although, the times I've seen it usually there has been some kind of infinite loop or recursion gone wrong and the debugger will catch it. I don't know if you've left it long enough, but after a while, the debugger should start to load the stack frames it does have and display them in the debugger window. There you should be able to see what is going wrong.
Like I said, usually when I've seen this its been due to infinite loops or recursion and the debugger will show upwards of 5000 calls to the same function, so finding the problem shouldn't be too difficult - but, saying that, it may not be the exact issue.
Just my two cents.
This warning also may happens when to use stack based block out of the scope the block defined without copying it.
http://developer.apple.com/library/mac/documentation/cocoa/Conceptual/Blocks/Articles/bxUsing.html#//apple_ref/doc/uid/TP40007502-CH5-SW2
http://www.friday.com/bbum/2009/08/29/blocks-tips-tricks/
I came accross this problem too but I found that it was caused by an image that was at too high a resolution. It would work perfectly in the simulator but not on the device. I resized the image to 320x480 pixels at 180 pixels per inch and now it works perfectly.
Hope that helps
I found the error in my code. It turns out that the SQLite database I was using was causing the crash. I was forgetting to close my database connection, and each time the code hit a particular function, I'd open a new connection. Eventually there were too many, and the app crashed.
It's looking like a lot of these errors are to do with a kind of overload (as Jasarien says).