views:

101

answers:

1

Hi all! I need to convert a NSDate to a string using my personal data format... The problem is that this code on the simulator (4.01) crashes... sometimes the code is "freezed", sometimes give the BAD_ACCESS... here is the code:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
+2  A: 

It's likely not this code, but some other problem in your app. I wrote a blog that goes through the various ways to find the bug

http://www.loufranco.com/blog/files/debug-iphone-crash-EXC_BAD_ACCESS.html

The issue is you get EXC_BAD_ACCESS at the point that a bad memory access is made, not at the point that corrupted the heap or caused the underlying problem.

If alloc is crashing, it's a good bet that your heap is corrupt, and that the corruption happened earlier than this code.

Lou Franco
thanks, but The strange thing is that without the code above all works fine... and now... enabling guardmalloc: http://grab.by/5xvg
ghiboz
Right, but if you put that line earlier in your program it will work, right? That line is like a mini-heap corruption detector -- try just copying it around and see where things went wrong.
Lou Franco
damn.... I put in viewDidLoad of my main viewcontroller and works...so now?How can I search the problem of my app??thanks very much!
ghiboz
hmm... I tried to put in the viewDidLoad of my 2nd viewController and crashes......
ghiboz
Try this http://www.loufranco.com/blog/files/debugging-memory-iphone.html
Lou Franco
thanks Lou...but a question.. what are usually the causes of this?? an object not dealloched o some other thing??
ghiboz
The signal is sent when your program reads or writes a memory location that isn't mapped to your application space. This means that the pointer that was dereferenced is either corrupt or pointing to a deallocated object. The real cause could be anything -- that's the problem. Common causes are sending messages to deallocated objects and buffer overruns on memory space (e.g. writing past the end of an array). In your case, since alloc is crashing, it's likely that the heap is already corrupt and alloc is confused by it. A common reason is a multi-dealloc (e.g. calling dealloc after release)
Lou Franco
The important thing is that the line of code having the problem isn't necessarily the cause -- it's detecting the problem that was caused elsewhere.
Lou Franco
so the problem is not an instance not dealloched but some instance dealloched but used... it's right??I ask you these things to reach to solve my problem.. thanks again Lou
ghiboz
Not deallocating usually only causes leaks, and not this. The root cause is using a memory address that isn't assigned to your application. In your case, since that's happening on alloc, I suspect that the heap is corrupt. After enabling Guard Malloc, read this http://developer.apple.com/iphone/library/documentation/Performance/Conceptual/ManagingMemory/Articles/MallocDebug.html
Lou Franco
thanks Lou! tonight I try!
ghiboz
I wrote this to try to explain it better http://www.loufranco.com/blog/files/Understanding-EXC_BAD_ACCESS.html
Lou Franco
thnaks Lou!!! thanks very much!
ghiboz