If you’re building a cocoa app, don’t use pthread mutexes and pthread threads. Use NSThread and NSLock. I’ve written a few cocoa apps that use pthreads, but maybe I’ve just been lucky till now.
I just spent about 8 work hours trying to figure out why my NSTextStorage object wouldn’t concatenate properly with my NSMutableAttributed, occasionally crashing the NSTextView. A lot of google searches pointed to memory stomping so I debugged with both GuardMalloc and NSZombieEnabled and set breakpoints on my raiseException methods. No beans.
Then I saw that maybe I should be surrounding my appendAttributedString calls to NSMutableAttributedString (and NSTextStorage) with -[NSMutableAttributedString beginEditing] and -[NSMutableAttributedString endEditing]. Did this. Same result.
I was updating the text in my NSTextView from the consumer main thread by popping off strings from a worker producer thread. I was doing this in a thread-way using pthread threads and pthread mutexes to make sure the shared objects were handled safely. Then I noticed a mailing list post and Apple doc that says you need to create at least one NSThread before autoreleasing works correctly. So I changed my pthread threads to NSThread threads, which are just pthreads that have a bit more on top. Things still didn’t work. Lastly out of desperation I changed my pthread mutexes to NSLocks. Then everything worked. It looks like somehow the pthread mutexes were slipping. I don’t know why, but for now I’ve stopped the bleeding.
I’m aware that some users don’t know how to use svn. So here’s a tutorial on how to download and compile our browser for mac.
How to get, build and run our source:
Install XCode from apple. The version you need depends on which OS you are running (3.2 for 10.6, 3.1 for 10.5, or 2.5 for 10.4.) It’s a free download, but its huge and can take an hour to get, but if you are interested in programming there’s no reason you shouldn’t. Go to this page and click on the “Mac Dev Center” button. You’ll need to register a free account with apple to download it.
If you don’t have fink, install it. fink is a command-line tool that lets you download programs from source and magically builds them. It also provides the option to download binaries via apt-get. Installation should take about 10 minutes. Yes, I know 10 minutes today is like three hours in late 20th century time, but it will really help you get some awesome tools including dict.
Install svn using fink. svn is a source control tool that lets many users work on the same code
files at once, while keeping track of changes and progress. There are GUI versions of it (rapidsvn or versions,) but I prefer to use it via terminal. Here’s how to get it:Open the terminal (in /Applications/Utilities folder) and type:
sudo apt-get install svn
You can also type “sudo fink install svn” to build it from source, which you may have to do if a binary is not available.
Download the repository source. If you want to you can make and cd to a new directory. Then download the source (creates a new folder in the current directory which you can see by typing pwd)
svn co https://algorhythmics.svn.sourceforge.net/svnroot/algorhythmics algorhythmics
Now you can go back to the finder. Probably a new folder called algorhythmics exists in your home directory. Open it and open FuckingWebBrowser/FuckingWebBrowser.xcodeproj.
Hit the build and go button or press command-R. The source then builds and then finally the program runs. If you want to tinker in the code you might be interested in a few files: FuckingAudioIO.cpp and FuckingSynth.h/cpp and its subclasses, which generate sound. If you are uneasy in c++ I recommend changing single number/parameter values and recompiling. If the program breaks after your change, simply revert back and try again.
Next, if you still want more fun, install dict using sudo fink install dict from the terminal. Now typing “dict artful” looks up through the net a bunch of meanings of the word and possibly save you from a silly word misuse that I sometimes still make. I like the tool because it’s really fast and light and I always have the terminal open.
Now you can open the project file NSSpeechSynthesizer.xcodeproj and build and run a singing recursive dictionary that uses dict to obliterate meaning and makes me wonder where it was in the first place. It was hacked together from an apple speech example and is pretty crude. But one day I’ll get back to it and clean it up for a release.
There are likely to be problems for some people in this process. I will try to update troubleshooting as people post their issues. Until then happy hacking.