ASIHTTPRequest crashes on iOS 3.1.3 - ios

My application crashes as soon as I call [[ASIHTTPRequest alloc] initWithURL:url]; on my iPod Touch 1G. It has no issue on iOS 4.x.
The error I see is Error Domain=ASIHTTPRequestErrorDomain Code=10 UserInfo=0x368790 "NSRangeException"
and the backtrace I get from a crash manager system I have implemented is
backtrace: (
"0 MayasDUp 0x0007f21b -[SWCrashManager backtrace] + 18",
"1 MayasDUp 0x0007ece1 sighandler + 144",
"2 libSystem.B.dylib 0x3049e7eb _sigtramp + 26",
"3 MayasDUp 0x0005128b -[ASIHTTPRequest buildPostBody] + 850"
)
I even placed the [[ASIHTTPRequest alloc] initWithURL:url] call alone, without performing anything else like setting post variables and starting the connection. The crash still happens.
I've placed breakpoints and log NSLogs inside ASIHTTPRequest but nothing useful came out.
Has anyone had a similar issue?

After much digging, I found that the issue was very deep and it actually was a compiler bug.
In short, using LLVM 2.0 and setting Optimization Level > None for the project resulted in corrupted compiled files (ASIHTTPRequest and ASIFormDataRequest) which caused the crash and provided no real data in the stack trace.
The solution we applied was to leave Compiler and Optimization settings intact and add a -O0 compiler flag for the 2 ASIHttp files that had the issue.
Other users reported that XCode 4.0.2 fixed the compiler issue.
For anyone interested, there are some details here:
http://groups.google.com/group/asihttprequest/browse_thread/thread/c598649bfe71643c/8ee09defd12386ba?lnk=gst&q=crashes#8ee09defd12386ba

You probably have something running on the main thread that is taking over 10 seconds. Are you doing synchronous http requests? You must use:
[request startAsynchronous];
if you're on the main thread.

Related

helgrind says "Bug in libpthread"

I am running helgrind to check for data races in my program. Helgrind reports 222 errors, all of them are:
Thread #21: Bug in libpthread: pthread_cond_wait succeeded without prior pthread_cond_post
I could not find anything about this error message on google. Within the valgrind source code, this seems to originate here:
if (!timeout && !libhb_so_everSent(cvi->so)) {
/* Hmm. How can a wait on 'cond' succeed if nobody signalled
it? If this happened it would surely be a bug in the threads
library. Or one of those fabled "spurious wakeups". */
HG_(record_error_Misc)( thr, "Bug in libpthread: pthread_cond_wait "
"succeeded"
" without prior pthread_cond_post");
}
However, I can not believe that I got 222 spurious wake-ups in about a second.
What could be the cause of this?
There are two condition variables, both in shared memory. The error always seems to happen with one of them, not with the other.

iOS: convert stacktrace entry to method name with line number

In a production app with the debug information stripped out, how do you convert the output of:
NSLog(#"Stack Trace: %#", [exception callStackSymbols]);
To an legible class and method name? A line number would be a blessing.
Here's the output I'm getting:
0 CoreFoundation 0x23d82f23 <redacted> + 154
1 libobjc.A.dylib 0x23519ce7 objc_exception_throw + 38
2 CoreFoundation 0x23cb92f1 <redacted> + 176
3 MyApp 0x23234815 MyApp + 440341
The final line is the bread and butter line, but when I use dwarf to find the address, nothing exists.
dwarfdump --arch armv7 MyApp.dSYM --lookup 0x00234815 | grep 'Line table'
I've read here that you need to convert the stack address to something else for dwarf or atos:
https://stackoverflow.com/a/12464678/2317728
How would I find the load address or slide address to perform the calculation? Is there not a way to calculate all this prior to sending the stacktrace to the log from within the app? If not, how would I determine and calculate these values after receiving the stack trace? Better yet, is there an easier solution I'm missing?
Note I cannot just wait for crash reports since the app is small and they will never come. I'm planning on sending the stack traces to our server to be fixed as soon as they appear.
EDITORIAL
The crash reporting tools in iOS are very rough, particularly when compared to Android. In android, the buggy lines are sent to Google analytics, you use the map to debug the line--simple (comparatively). For iOS, you are confronted with: a) waiting on user bug reports (not reasonable for a small app), b) sending stack traces to a server where there are scant tools or information on how to symbolicate the stack traces, c) relying on large quasi-commercial 3rd party libraries. This definitely makes it harder to build and scale up--hoping Apple will eventually take notice. Even more hopeful someone has spotted an easier solution I might have missed ;)
Thanks for your help!
A suggestion, you can easily get the method name, exception reason and line number using:
NSLog(#"%# Exception in %s on %d due to %#",[exception name],__PRETTY_FUNCTION__,__LINE__,[exception reason]);

How to know which line is causing exception?

I am new to XCode and Objective C. I have intentionally make a mistake to assign number to NSString*.
NSString* s = #1;
[s uppercaseString];
Though XCode gives me warning, this code will compile. But at runtime I get exception. Now I see in logs, (Sorry for image, I was not able to paste is as text properly due to formatting)
In this log, how I find exact place of error. How this log tells me which code to change.
So it looks like you are running the Release build (debug symbols stripped) and if you got that crash log in a production environment you would need to symbolicate it in order to find the line.
This Apple TN gives some details of Symbolication.
In a development environment you would simply add an exception breakpoint and run it from Xcode, as the debug symbols would not be stripped.
To understand what line causes the problem, you usually need to add exception breakpoint to your project as explained in this document;
In the bottom-left corner of the breakpoints navigator, click the
Add button.
Choose Add Exception Breakpoint.
In the Exception pop-up menu, choose the type of exception on which
you want execution to stop:
All. Stops on all exceptions.
Objective-C. Stops on Objective-C exceptions.
C++. Stops on C++ exceptions. To stop on a particular C++ exception, specify the exception name.
Choose the phase of the exception handling process at which you want program execution to stop.
Click Done.
line 5 Sam : [BIDViewController viewDidLoad] + 143 , if this is a release build , you need to resolve with symbols the memory address of the function , this is called "symbolize" the crash dump...
In the log look for your project name and you will come to know.
e.g.
line 5 Sam : [BIDViewController viewDidLoad] + 143
If you want to produce real crash without warning, try following code it will produce index out of bound exception and will crash
NSArray *array = #[#"1",#"2"];
NSLog(#"Item not accessible->%#",(NSString*)array[2]);
set Exception breaking point or enable NSZombie object
or
NSZombie
From the menu bar, choose Project > Scheme > Edit Scheme

iOS 7: Strange AVAssetExportSession bug

I have used AVAssetExportSession to join 2 video files. It works fine in iOS 6 but has very strange bug in iOS 7.
Let me explain this bug. I have 2 video files, the first video is 8 seconds duration and the second is 81 seconds duration. In iOS7, it will be fine if I do the second + the first. But if I do the first + the second, it will be error:
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x176cb5c0 {NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x176ca000 "The operation couldn’t be completed. (OSStatus error -12633.)", NSLocalizedFailureReason=An unknown error occurred (-12633)}
I have checked exportPresetsCompatibleWithAsset and supportedFileTypes. There is no problem with it. Could anyone tell how to fix this? Any help would be appreciated.
Does anyone know this bug :(. I provide 2mp4 files which cause the bug: test 1, test 2. Do join "test 2" + "test 1" is fine, but do "test 1" + "test 2" will cause an error. It only happens in iOS7
I found that you can replace AVAssetExportSession with SDAVAssetExportSession. You can then specify settings instead of using presets which provide different results across different devices.
I had to change __weak typeof(self) wself = self; to
__weak SDAVAssetExportSession * wself = self; on line 172 of SDAVAssetExportSession.m.
I had an "unknown error occurred (-12633)" message.
Found the answer to be adding two samples with same timestamp.
Apparently error -12633 is an InvalidTimestamp.
See this SO post -> AVAssetWriter unknown error
If you are using methods insertTimeRanges or insertTimeRange, you need to ensure that the input time ranges are valid. To be more specific, you should not use the asset's duration for this, you need to get the accurate time ranges from individual asset tracks.
Use command ffprobe -show_frames path_to_file to inspect your video files, which sometimes may be very helpful.

EXC_BAD_ACCESS signal received

When deploying the application to the device, the program will quit after a few cycles with the following error:
Program received signal: "EXC_BAD_ACCESS".
The program runs without any issue on the iPhone simulator, it will also debug and run as long as I step through the instructions one at a time. As soon as I let it run again, I will hit the EXC_BAD_ACCESS signal.
In this particular case, it happened to be an error in the accelerometer code. It would not execute within the simulator, which is why it did not throw any errors. However, it would execute once deployed to the device.
Most of the answers to this question deal with the general EXC_BAD_ACCESS error, so I will leave this open as a catch-all for the dreaded Bad Access error.
EXC_BAD_ACCESS is typically thrown as the result of an illegal memory access. You can find more information in the answers below.
Have you encountered the EXC_BAD_ACCESS signal before, and how did you deal with it?
From your description I suspect the most likely explanation is that you have some error in your memory management. You said you've been working on iPhone development for a few weeks, but not whether you are experienced with Objective C in general. If you've come from another background it can take a little while before you really internalise the memory management rules - unless you make a big point of it.
Remember, anything you get from an allocation function (usually the static alloc method, but there are a few others), or a copy method, you own the memory too and must release it when you are done.
But if you get something back from just about anything else including factory methods (e.g. [NSString stringWithFormat]) then you'll have an autorelease reference, which means it could be released at some time in the future by other code - so it is vital that if you need to keep it around beyond the immediate function that you retain it. If you don't, the memory may remain allocated while you are using it, or be released but coincidentally still valid, during your emulator testing, but is more likely to be released and show up as bad access errors when running on the device.
The best way to track these things down, and a good idea anyway (even if there are no apparent problems) is to run the app in the Instruments tool, especially with the Leaks option.
A major cause of EXC_BAD_ACCESS is from trying to access released objects.
To find out how to troubleshoot this, read this document:
DebuggingAutoReleasePool
Even if you don't think you are "releasing auto-released objects", this will apply to you.
This method works extremely well. I use it all the time with great success!!
In summary, this explains how to use Cocoa's NSZombie debugging class and the command line "malloc_history" tool to find exactly what released object has been accessed in your code.
Sidenote:
Running Instruments and checking for leaks will not help troubleshoot EXC_BAD_ACCESS. I'm pretty sure memory leaks have nothing to do with EXC_BAD_ACCESS. The definition of a leak is an object that you no longer have access to, and you therefore cannot call it.
UPDATE:
I now use Instruments to debug Leaks. From Xcode 4.2, choose Product->Profile and when Instruments launches, choose "Zombies".
An EXC_BAD_ACCESS signal is the result of passing an invalid pointer to a system call. I got one just earlier today with a test program on OS X - I was passing an uninitialized variable to pthread_join(), which was due to an earlier typo.
I'm not familiar with iPhone development, but you should double-check all your buffer pointers that you're passing to system calls. Crank up your compiler's warning level all the way (with gcc, use the -Wall and -Wextra options). Enable as many diagnostics on the simulator/debugger as possible.
In my experience, this is generally caused by an illegal memory access. Check all pointers, especially object pointers, to make sure they're initialized. Make sure your MainWindow.xib file, if you're using one, is set up properly, with all the necessary connections.
If none of that on-paper checking turns anything up, and it doesn't happen when single-stepping, try to locate the error with NSLog() statements: sprinkle your code with them, moving them around until you isolate the line that's causing the error. Then set a breakpoint on that line and run your program. When you hit the breakpoint, examine all the variables, and the objects in them, to see if anything doesn't look like you expect.I'd especially keep an eye out for variables whose object class is something you didn't expect. If a variable is supposed to contain a UIWindow but it has an NSNotification in it instead, the same underlying code error could be manifesting itself in a different way when the debugger isn't in operation.
I just spent a couple hours tracking an EXC_BAD_ACCESS and found NSZombies and other env vars didn't seem to tell me anything.
For me, it was a stupid NSLog statement with format specifiers but no args passed.
NSLog(#"Some silly log message %#-%#");
Fixed by
NSLog(#"Some silly log message %#-%#", someObj1, someObj2);
The 2010 WWDC videos are available to any participants in the apple developer program.
There's a great video: "Session 311 - Advanced Memory Analysis with Instruments" that shows some examples of using zombies in instruments and debugging other memory problems.
For a link to the login page click HERE.
Not a complete answer, but one specific situation where I've received this is when trying to access an object that 'died' because I tried to use autorelease:
netObjectDefinedInMyHeader = [[[MyNetObject alloc] init] autorelease];
So for example, I was actually passing this as an object to 'notify' (registered it as a listener, observer, whatever idiom you like) but it had already died once the notification was sent and I'd get the EXC_BAD_ACCESS. Changing it to [[MyNetObject alloc] init] and releasing it later as appropriate solved the error.
Another reason this may happen is for example if you pass in an object and try to store it:
myObjectDefinedInHeader = aParameterObjectPassedIn;
Later when trying to access myObjectDefinedInHeader you may get into trouble. Using:
myObjectDefinedInHeader = [aParameterObjectPassedIn retain];
may be what you need. Of course these are just a couple of examples of what I've ran into and there are other reasons, but these can prove elusive so I mention them. Good luck!
I find it useful to set a breakpoint on objc_exception_throw. That way the debugger should break when you get the EXC_BAD_ACCESS.
Instructions can be found here DebuggingTechniques
Just to add another situation where this can happen:
I had the code:
NSMutableString *string;
[string appendWithFormat:#"foo"];
Obviously I had forgotten to allocate memory for the string:
NSMutableString *string = [[NSMutableString alloc] init];
[string appendWithFormat:#"foo"];
fixes the problem.
Another method for catching EXC_BAD_ACCESS exceptions before they happen is the static analyzer, in XCode 4+.
Run the static analyzer with Product > Analyze (shift+cmd+B).
Clicking on any messages generated by the analyzer will overlay a diagram on your source showing the sequence of retains/releases of the offending object.
Use the simple rule of "if you didn't allocate it or retain it, don't release it".
Run the application and after it fails (Should display "Interrupted" rather than "EXC_BAD_ACCESS"... Check the Console (Run > Console)... There should be a message there now telling what object it was trying to access.
I've been debuging, and refactoring code to solve this error for the last four hours. A post above led me to see the problem:
Property before:
startPoint = [[DataPoint alloc] init] ;
startPoint= [DataPointList objectAtIndex: 0];
.
.
.
x = startPoint.x - 10; // EXC_BAD_ACCESS
Property after:
startPoint = [[DataPoint alloc] init] ;
startPoint = [[DataPointList objectAtIndex: 0] retain];
Goodbye EXC_BAD_ACCESS
Hope you're releasing the 'string' when you're done!
I forgot to return self in an init-Method... ;)
This is an excellent thread. Here's my experience: I messed up with the retain/assign keyword on a property declaration. I said:
#property (nonatomic, assign) IBOutlet UISegmentedControl *choicesControl;
#property (nonatomic, assign) IBOutlet UISwitch *africaSwitch;
#property (nonatomic, assign) IBOutlet UISwitch *asiaSwitch;
where I should have said
#property (nonatomic, retain) IBOutlet UISegmentedControl *choicesControl;
#property (nonatomic, retain) IBOutlet UISwitch *africaSwitch;
#property (nonatomic, retain) IBOutlet UISwitch *asiaSwitch;
I encountered EXC_BAD_ACCESS on the iPhone only while trying to execute a C method that included a big array. The simulator was able to give me enough memory to run the code, but not the device (the array was a million characters, so it was a tad excessive!).
The EXC_BAD_ACCESS occurred just after entry point of the method, and had me confused for quite a while because it was nowhere near the array declaration.
Perhaps someone else might benefit from my couple of hours of hair-pulling.
Forgot to take out a non-alloc'd pointer from dealloc. I was getting the exc_bad_access on my rootView of a UINavigationController, but only sometimes. I assumed the problem was in the rootView because it was crashing halfway through its viewDidAppear{}. It turned out to only happen after I popped the view with the bad dealloc{} release, and that was it!
"EXC_BAD_ACCESS" [Switching to process 330] No memory available to program now: unsafe to call malloc
I thought it was a problem where I was trying to alloc... not where I was trying to release a non-alloc, D'oh!
How i deal with EXC_BAD_ACCESS
Sometimes i feel that when a EXC_BAD_ACCESS error is thrown xcode will show the error in the main.m class giving no extra information of where the crash happens(Sometimes).
In those times we can set a Exceptional Breakpoint in Xcode so that when exception is caught a breakpoint will be placed and will directly intimate the user that crash has happened in that line
NSAssert() calls to validate method parameters is pretty handy for tracking down and avoiding passing nils as well.
I just had this problem. For me the reason was deleting a CoreData managed object ans trying to read it afterwards from another place.
I've been debuging, and refactoring code to solve this error for the last four hours. A post above led me to see the problem:
Property before:
startPoint = [[DataPoint alloc] init] ;
startPoint= [DataPointList objectAtIndex: 0];
x = startPoint.x - 10; // EXC_BAD_ACCESS
Property after:
startPoint = [[DataPoint alloc] init] ;
startPoint = [[DataPointList objectAtIndex: 0] retain];
Goodbye EXC_BAD_ACCESS
Thank you so much for your answer. I've been struggling with this problem all day. You're awesome!
Just to add
Lynda.com has a fantastic DVD called
iPhone SDK Essential Training
and Chapter 6, Lesson 3 is all about EXEC_BAD_ACCESS and working with Zombies.
It was great for me to understand, not just the error code but how can I use Zombies to get more info on the released object.
To check what the error might be
Use NSZombieEnabled.
To activate the NSZombieEnabled facility in your application:
Choose Project > Edit Active Executable to open the executable Info window.
Click Arguments.
Click the add (+) button in the “Variables to be set in the environment” section.
Enter NSZombieEnabled in the Name column and YES in the Value column.
Make sure that the checkmark for the NSZombieEnabled entry is selected.
I found this answer on iPhoneSDK
I realize this was asked some time ago, but after reading this thread, I found the solution for XCode 4.2:
Product -> Edit Scheme -> Diagnostics Tab -> Enable Zombie Objects
Helped me find a message being sent to a deallocated object.
When you have infinite recursion, I think you can also have this error. This was a case for me.
Even another possibility: using blocks in queues, it might easily happen that you try to access an object in another queue, that has already been de-allocated at this time. Typically when you try to send something to the GUI.
If your exception breakpoint is being set at a strange place, then this might be the cause.
I got it because I wasn't using[self performSegueWithIdentifier:sender:] and -(void) prepareForSegue:(UIstoryboardSegue *) right
Don't forget the # symbol when creating strings, treating C-strings as NSStrings will cause EXC_BAD_ACCESS.
Use this:
#"Some String"
Rather than this:
"Some String"
PS - typically when populating contents of an array with lots of records.
XCode 4 and above, it has been made really simple with Instruments. Just run Zombies in Instruments. This tutorial explains it well: debugging exc_bad_access error xcode instruments

Resources