EXC_BAD_ACCESS crashes and troubleshooting

Hello all,

I have been working on an application project in ApplescriptObjC and Xcode 4.6. Some details of what my project entails can be found in a recent post where I asked for advice on the main function of the application (link below).

http://macscripter.net/viewtopic.php?id=40329

For this particular project, I am actually reading 2 different log files and displaying them in 2 tableviews in the application’s main interface window. All of that functions mostly to my liking (would like to learn how to scroll to the bottom of the updated tableview on insertion of log data), but will save that for another post after trying myself.

When my application launches (under applicationWillFinishLaunching) I have several lines of code that change file permissions via a chmod command inside of a ‘do shell script’ command. These lines utilize the ‘with administrator privileges’ notation after each (which may be repetitive and also I suspect the cause for the crash).

Two buttons in the interface are used to manually refresh the log file. After each refresh, the log file’s contents will be displayed, sometimes showing the updated lines since the last update. I believe, based on the code supplied in the link above, that the log files are fully passed through the array controllers each time (which also may be the cause for the crash).

What I am experiencing is that whenever one of the buttons to manually refresh the log file tableview (either) is clicked multiple times, such as repeatedly to see any additional contents of the log, the application crashes with a EXC_BAD_ACCESS error on multiple threads. The best details I can provide (without further instruction) are displayed below. I have read Shane Stanley’s section on memory management in ApplescriptObjcC Explored (thanks Shane, a wonderful resource!) about the differences between ARC and garbage collection methods of memory management. As such, I attempted to troubleshoot the application by enabling ARC to see if the crashes still occur, which they do, on the same threads with the same EXC_BAD_ACCESS error. I have also set a delay timer of 5 (max I want for user friendliness) to see if spacing out the refreshes that way will in any way impact the crashes (it does not).

come.apple.main-thread (serial)
Thread 1
0 objc_msgSend

line showing error is:
0x1005ac250: movq 16(%r11), %r10

error is:
Thread 1: EXC_BAD_ACCESS (code=13, address=0x0)

Thread 1 shows 3 more crashes same error as above, but in differently numbered areas of the application.
31 NSApplicationMain
32 main
33 start

There are additional Threads, but most show the exact same EXC_BAD_ACCESS. My purpose for the post is obviously to get assistance getting my application running reliably, but also to establish a pattern of troubleshooting for others to try to follow. Many Google searches on this error turn up application-specific issues, very few (other than this forum) attributed to ASOC. If this type of crash has been documented further, I will be happy consulting a link to something I have not found previously, and very appreciative!

It would seem from reading around that either at launch or at some early function of the application a reference is being made to something that is not getting closed as the application continues to run. Any help is greatly appreciated!

Are you by any chance getting error logs before the crash ? Something like below ?

My problem may not have been the same, but I was getting a similar crash to yours. I managed to trace it back to two issues:

Build settings ( ARC and Garbage Collection ) - I still don’t know what the correct settings should be. To get around it, I have been creating my projects in Xcode 3, then opening them in Xcode 4. Any help on this issue welcome
The above was basically not retaining any of my properties that were set via code.

The second problem was the way I was dealing with strings within repeat loops. When doing one-off “blah blah as text” , things were fine. As soon as i used the same code in a repeat loop, my app would generate similar errors. As soon as i changed all my code to conform to NSString’s class ( stringWithString etc), no more crashing.

One more plea ! I have looked at previous posts regarding build settings and i can’t even locate the settings that many posts refer too. Any help would be appreciated.

Regards

P

Hey Pez,

I get no response from the debug area in Xcode when Running my application. At first launch I receive a prompt for administrator credentials to assist with the application’s initial functions as detailed in my first post. Edit: I get no response in the debug area in Xcode until my application crashes, in which case Xcode becomes the primary application and points at the debug threads that are showing the EXC_BAD_ACCESS errors.

To the best of my knowledge, and based on reading Shane Stanley’s ApplescriptObjC Explored, it would seem that applications that will be utilized only on 10.8 would use ARC, and any others (specifically for backwards-compatibility) should utilize GC. I am currently using GC, but built my application using ARC as a test to see if the crashes continued, which they did.

Per the thread I linked in my first post, I believe I am using NSString rather than as text. This is to the best of my interpretation of the code supplied by stefanK and Shane, as well as the API and Xcode docs.

I will agree that most posts and troubleshooting blog posts I have followed for help seem to include steps specific to iOS, and I have trouble finding some settings mentioned.

Put log commands either side of the calls. Your first aim should be to try to track down where the crash is happening.

Again, sprinkle lots of log calls around the relevant code.

If you are using stringWithContentsOfFile_encoding_error_, add code to check for errors, something like:

set {txt, theError} to current application's NSString's stringWithContentsOfFile_encoding_error_(unixpath, UTF8StringEncoding, reference)
if txt = missing value then
log theError
...

Worth a try, but unlikely to tell you much.

The error itself is too low level to tell you much, I’m afraid. As I said, the first job is to try to track dwon where it’s happening. Good luck!

It’s all covered in my book, pages 232-233 of the latest edition. There’s also errata here to cover Xcode 4.6:

www.macosxautomation.com/applescript/apps/errata.html

In working to troubleshoot my issue I went through a couple of steps. Due to the fact that ApplescriptObjC is a learning experience for me, and according to similar EXC_BAD_ACCESS threads here on MacScripter referring to how hard it is to track down these errors, I wanted to give some helpful hints that I’m attempting to use throughout my journey to quell these crashes.

The first thing I have done is to set ‘log’ lines at intervals appropriate to application function. For example, in the first post here I referenced a thread where stefanK and Shane Stanley provided me with some ASOC for importing the contents of a UTF8 encoded text log file into a tableview, through an array controller. For each line of code, I inserted a log line after that, log 1, log 2, etc. While the application is running, I am able to view each logged item to know what has successfully completed. In doing this, I have found that my application crashes ‘randomly’…meaning after all logging has completed, or a couple of logs have completed, either or. This tells me that the crashes are something that can be attributed to a buildup, over time, as opposed to some specific function or single line of code that causes an instantaneous crash. This is how I made the initial assumption that it could be memory related (that and a Google search).

The next thing I have done is comment out lines of code that may be particularly suspicious to me during the crashing. In the initial post, I mentioned that I thought the call for ‘with administrator privileges’ could be the culprit. However, after commenting out all of the lines of my application that included that phrase, I am able to eliminate that from the equation.

At this point I am left to believe that the culprit behind the crashes is something being called by the code provided by stefanK and Shane. I examined the ASOC calls to the NSString class and extensions that were made in that code provided (if this seems foreign, reference the link to macscripter thread in first post). In the Xcode documentation for NSString, under the Methods to Override section, it talks about using an initializer for any of the NSString subclasses. Further down, under Creating and Initializing a String from a File, I find one of the commands referenced, a stringWithContentsOfFile_encoding_error (with colons not _ in Xcode docs).

In arriving at this point in the troubleshooting road, I would like to ask the question of the community as to whether or not NSString is being initialized or rather, allocated and deallocated by using this particular Task of NSString? Hopefully this question makes sense, but my hunch is that the use of NSString in the application is not being deallocated each time the log file is checked. While I am not constantly pressing the interface button to refresh the logs, perhaps the number of log lines it must pull each time in total is affecting how memory gets allocated? Is my understanding of what is going on at least somewhat in the right direction?

I thank any responses in advance.

The error suggests the opposite: something is being deallocated prematurely. With garbage collection, deallocation (and memory management in general) is beyond your control. The fact that it happens under ARC too makes me think it’s possibly a framework bug you are triggering.

Please add the error logging code for that method I posted above. Also try changing compilers and perhaps 32-bit vs 64-bit.

Shane,

I think I was typing so long I did not see your responses being posted.

Upon inserting the error handling code:

Applescript:
set {txt, theError} to current application's NSString's stringWithContentsOfFile_encoding_error_(unixpath, UTF8StringEncoding, reference)
if txt = missing value then
log theError

my crash changes somewhat. I still get EXC_BAD_ACCESS, but instead of getting it on:
come.apple.main-thread (serial)
Thread 1
0 objc_msgSend

I now get it on:
come.apple.main-thread (serial)
Thread 1
CFRetain

Also, these:
31 NSApplicationMain
32 main
33 start

have now changed to these:
26 NSApplicationMain
27 main
28 start

In the Debug area (set to All Output) I get “(lldb)” - nothing else.

See my other most recent post for the result of inserting logs.

Anything appear in the log?

Nothing appears in the log that I can view in the Debug Area, which is where my other logs appear when I make log statements. Is there another place to look?

Changing the compiler from Apple LLVM Compiler 4.2 (what was being used by default) to LLVM GCC 4.2 gives the same EXC_BAD_ACCESS from CFRetain in thread 1 (shown below from other post).

come.apple.main-thread (serial)
Thread 1
CFRetain

26 NSApplicationMain
27 main
28 start

All of this happens with the error handling statement you suggested, and does not add anything to the log.

If you want to email it to me off-list, I can at least have a look.

Ok will do.

Also, here are a couple of screenshots:

http://imgur.com/IShseqU
http://imgur.com/zffFXUj

Thanks.

Wanted to close out this thread with a wonderful piece of troubleshooting advice. When in doubt, RTFM. In this case, the M is Shane’s AppleScriptObjC Explored.

I ended up sending my project to Shane, to which he was able to determine that GC was not actually enabled. Referencing page 233 of his manual above would have answered my question, but I think I was more concerned with reading the errata link he posted. The answer to how I fixed my crashes is there, regarding the enabling of GC memory management in my application. What I thought was enabled, wasn’t and had to be done manually.

EDIT: One more thing to add, just to surmise Shane’s answer to me in email for those for the future. Memory management is performed manually by Xcode if ARC or GC is not specifically turned on. Someone can post more questions about this in depth, and even I may do so after dealing with this issue, for the good of the community, but don’t have time right now.

Well at least we don’t have to worry about a new problem lurking in the frameworks :slight_smile:

Thanks for all the posts! It really helps having outside perspective’s !
Regards

P

Posting a link to the ‘final’ project…

https://github.com/loyaltyarm/mac/tree/master/Syncafee