a glob of nerdishness

August 27, 2007

Garbage collected iPhoto 7?

written by natevw @ 7:19 am

Does iPhoto 7 use some garbage collection features? If so, that would explain the semi-regular beach balls, regardless of what the user is doing. I’ve been wondering for a while if Tiger will get updated to support the new garbage-collected runtime from Leopard, and perhaps the new iLife suite is testing some of that out internally.

On the other hand, this could also be caused by paging. I was only able to get it to hang up twice this morning — the hard drive was churning, so this points to paging, but I didn’t see much change in iPhoto’s Real vs. Virtual memory usage. Any ideas for a better experimental or investigative method?

Update: While the results posted in the comments aren’t entirely conclusive, the simplest explanation is that the hangs were for plain old paging. The 7.0.2 update seems to have greatly reduced these hang-ups either way.

June 18, 2007

Tips and Tricks for Ruby on Rails

written by hjon @ 7:40 pm

But not from me. However, here is a “What I Wish I Knew” for Ruby on Rails.

June 16, 2007

Global C++ Exception Handling

written by natevw @ 12:01 pm

It’s easy to handle uncaught exceptions at a global level in a C++ program. Most compilers, if not all, provide a function called std::set_terminate(). This lets you register a function that gets called when the program terminates unexpectedly due to an error. This function does not take any arguments, but can re-throw any current exception:


void catch_global() {
 try {
  throw;
 }
 catch (const YourPrintableException& e) {
  std::cerr << "Exiting due to error: " << e << std::endl;
 }
 catch (...) {
  std::cerr << "XQI ERROR -42: UNEXPECTED OCCURRENCE" << std::endl;
 }
 abort();
}

int main() {
 std::set_terminate(catch_global);
 if (true) throw "up";
 else return 0;
}

While this shouldn't replace good error handling practices throughout your code, it can be a handy way for your program to put some last words on the record before it kicks the bucket.

One disclaimer: I'm not completely sure that set_terminate() is an official part of the C++ standard. It's in Thinking in C++ but not the C++ FAQ-Lite, the big C++ reference or the light C++ reference. Official standard or not, it seems to be widespread. You can find documentation on set_terminate() via GNU, IBM or MSDN.

May 25, 2007

Apache-less SVN: Subversion between two Macs via SSH

written by natevw @ 6:55 pm

My first Mac, an original G4 mini, hasn’t seen much use since my new iMac arrived last summer. Hoping to start “spare-time” developing again, and now addicted to version control through work, I made it my goal one afternoon to get Subversion running between the old and the new. Eventually I’d like to expand this beyond source code to a home directory or even a .Mac backend, depending on what Leopard ends up offering.

Apple has an article on compiling a full SVN stack on OS X for use with Xcode. This looked a little bit more painful than I was aiming for, for some reason involving hand-patching Apache 2.0 and plenty of configuration. That route is spelled out nicely (1), but I wanted something even simpler.

Enter Martin Ott and his ready-made Subversion installer packages. Here’s how you can set up your own low-carb Subversion system, hopefully in less than 30 minutes.

Install SVN

Download the SVN package and install it on both your server and your client machines. It will ask you for your password, and then install several binary utilities in /usr/local/bin. This does not include any Apache modules but it does include a standalone daemon named ’svnserve’. This means no WebDAV access, but you can easily use svnserve with ssh to access your data securely across a network.

Configure paths

Unfortunately, programs in /usr/local/bin aren’t easily used by default. The next step is to get the utilities into your Terminal path. On your client machine, just add export PATH=$PATH:/usr/local/bin to your ~/.bash_profile file. For access via SSH to work, the server needs that same line but added the machine-wide /etc/bashrc configuration instead of your user’s profile. For this, you will need administrator privileges, something like sudo pico /etc/bashrc should do the trick. (Alternatively, you could cd /usr/bin/; sudo ln -s /usr/local/bin/svnserve to avoid editing bash’s global configuration. In this case, also configure your user account on the server just as you did the client, because in the next step we use another utility via that path.)

Make a repository

The repository is where all your past and present data will be stored on the server, accessed via the SVN tools. Pick a spot where you don’t mind an extra folder sitting around, and execute on the server machine: svnadmin create ~/repository. Feel free to change the path, as given it will make a new “repository” directory in your home folder and fill it with files. These internal files should generally not be used directly, we’ll test the SVN commands in the next step.

Accessing the repository

Make sure you have “Remote Login” enabled in System Preferences > Sharing on the server machine. If not, enable it, and you should be ready to go back to the client machine. “Check out” your new (empty) repository with this command: svn checkout svn+ssh://your-machine-name.local/Users/yourname/repository new_local_folder. After giving your ssh password a few times, you should find “new_local_folder” copied into the current path.

Beyond…

If you’re not familiar with SVN commands themselves, the maintainers provide a top-notch Subversion Book that is both tutorial and reference. Also by now, you’re probably sick of typing your ssh password. You can improve this by using authorized keys and ssh-agent. The previously linked Apple article finishes with a section titled “Xcode and Subversion”, though there might be an incompatibility between Xcode and the latest SVN software. Let me know how it goes if you make it that far, or get stuck along the way!


  1. Unlike the error messages from Fink trying to update a previous Apache2/SVN install. I gave Fink the ‘rm -rf /sw’ treatment, and now I feel better.

May 23, 2007

Xcode Regular Expression replacement

written by natevw @ 7:55 pm

Thanks to “Search and Replace in Xcode” at Noodlesoft’s blog, I learned how to use RegEx grouping in Xcode’s Search and Replace. In short: \1 instead of $1. Now I can convert the wiki-formatted tables of Kyōiku kanji into a Quisition flash card pack.

May 19, 2007

Scratch

written by natevw @ 1:38 pm

Scratch, designed at MIT, has been making a splash in major news outlets lately. The Scratch project is basically Xcode meets YouTube for games. Sounds strange, but it’s a combination of a desktop-based integrated development environment and a community-based game sharing site. In order to pull this off, the designers developed a programming language that succeeds at being both simple and powerful.

When I saw the headline in the middle on BBC News, I was instantly suspicious:

Scratch front page BBC News

The picture was an instant reminder of Lego Programming hype. (Interesting aside: if you google lego block programming, the BBC article takes second place to that very post!) The article itself is less sensational, toning the headline down to “Free tool offers ‘easy’ coding” and revealing that it is actually a desktop application that allows creation of games.

Trying it out

Feeling that this had all been attempted before, I didn’t think much more of it until I got a note from Hannah, who had also seen the article: “Would this help me understand you?” I guess it’s worth a shot! She downloaded it, and together we figured out how to get the thing started. The cryptic “Open” dialog box was a bit rough, but after we figured out we needed to select the Scratch.image file, she was on her way.

As I watched her learn the “language”, I became impressed with its enlightened design. Scripts are indeed built from graphical pieces, but the process is much more powerful than a mindless succession of commands and much less tacky than drawing flowchart lines. The user can nest commands into control structures and insert test questions into conditionals. Like an open-ended puzzle, the shapes and colors of these “building blocks” give subtle clues as to what pieces may be missing.

Impressive simplifications

Programming in Scratch is a completely interactive affair. The user can attach a number of independent scripts to graphical sprites. Each script is started by one of many available triggers. These triggers are always active, despite the presence of a green flag (just another trigger option) and a stop sign (just an emergency kill switch) in the interface. The interactivity goes deep — there isn’t even an “initial stage”. What the user drags around with the mouse and what gets moved via a script are on equal footing. This is a daring move, forcing the explicit scene initialization in some cases, but greatly simplifying the user model. This is just one of many well-executed simplifications. Variables are also made accessible to beginners, being visible by default and having at most two scope options.

As mentioned before, scripts are attached directly to sprites, meaning that in lieu of a master “puppeteer” script, the sprites are essentially autonomous. All scripts run concurrently upon being triggered. This is the coolest simplification, and also means parallelization becomes an integral part of this beginner’s programming language! Concurrency is heavy wizardry when tacked into many programming models, but is very intuitive in the context of sprite objects. The cat has ideas and the mouse has its own. While shared variables can be used to communicate between scripts (and are an easy concept for beginners), there is also a global message broadcast system. Scripts can broadcast messages, asynchronously or synchronously, which are received by all scripts that are triggered by that particular message.

Wide audience

Scratch doesn’t just encourage algorithmic thinking with the usual control structure and variable concepts. It also teaches important parallelizing patterns that “real” programmers typically encounter only in still-esoteric languages. Scratch can help my wife understand my field, and I hope it can be effective for younger learners. (With hardly any clues from me, Hannah made a reasonably involved Island Rescue game after only two other experiments.) The publicity is justified. What came as further surprise is that it can inspire my programming patterns and design strategies too! Well done, MIT!

May 12, 2007

Subtle bugs stink

written by natevw @ 7:20 am

Consider the following piece of code, to process hex color strings:
sscanf(text, “#%2x%2x%2x%2x”, &rgbcolor.r, &rgbcolor.g, &rgbcolor.b, &rgbcolor.a);

Looks pretty standard, eh? Well, that piece of code cost a day’s work. I was getting segmentation faults in std::list’s iterator post-increment. The iterator had no clear reason to be invalid, although the debugger did show bad values (0×0 or 0xbf000000 for the _M_node internal pointer it was trying to dereference). I copied my project, and hacked it down to a smaller main code path. Strangely enough, the presence the constructor for an otherwise unused class was the shibboleth that determined whether my code crashed or not. Suspecting a compiler bug (how pride resists being wounded!), I tried all manner of settings (optimization, static vs. dynamic linking) before I finally went back to said constructor. Lo and behold, when the constructor called a code path that involved the sscanf above, std::list exploded. When not, it was fine. What on earth?

Well kids, this is why C-style formatters are considered harmful. Sure I had four %x’s and four variables, but “%x”es expect ‘int’ and my variables happened to be ‘char’s underneath a typedef. So the problem was not in std::list, not in the derived class, not in my main code and not even directly in the constructor that clued me in.

Using C++ stringstreams can be a pain, but so is debugging things like this. I wonder if that’s why C++ often gets a bum rap.

April 7, 2007

License options for those without legal departments

written by natevw @ 2:40 pm

Jeff Atwood has put together a handy chart of software licenses on his great blog. It lists only a few of all the software licenses known to man, but that’s just the point. What makes the chart especially handy is his choice of columns. Succinct “Source” and “License” headings help narrow a choice down, and the “Clauses” column suggests the amount of legalese you’re in for upon further investigation. It’s almost as helpful as the Creative Commons license builder(1), but for software developers.

For helping programmers to share their own code, the three Microsoft licenses (especially the two which have Open Source Initiative-approved cousins) seem out of place. All the same, I present for completeness a similar summary of the Apple Public Source License, version 2:

  • Source: Open
  • License: Permissive / Weak copyleft
  • Clauses: 13 with abundant sub-clauses
  • Gist: allows proprietary use of unmodified code, with patent and source code caveats on modifications.

Obviously not a great choice for new code(2). It’s a license Apple uses to voluntarily release the kernel code for OS X, and even they don’t use it for all their available sources.


  1. …though if I ever come across (or make) a page that puts the extra CC polish on the process, I’ll let you know.
  2. The wildebeest himself approves your use and contributions to APSL-licensed software, but doesn’t “recommend you…release new software using this license”.

April 2, 2007

Assembly primer

written by natevw @ 8:09 pm

I’ve been doing a lot of C++ coding at work lately, and sometimes the only “source” Xcode can show when debugging a compiled library is assembly code. Could knowing assembly language help debug C++ code? Short answer: no. There’s better techniques, and the assembly code often takes me closer to the machine (and the deadline) than I need to be in those instances.

Yet I’ve had a longtime interest in assembly code, for numerous reasons(1). Take this tiny piece of segmentation-faulting assembly:

call *8(%edx)

I had a hunch edx was a processor register (a “variable” of sorts), and sure enough Xcode’s debugger showed a 0 in the EDX register. “Call” then seems to say I’m trying to execute code from a bad function pointer that got written into EDX. But what’s with the “*8″, and does the ‘%’ mean anything special? Enter a great tutorial on AT&T Assembly Syntax, which happens to be what the GNU toolset, and therefore Xcode, uses(2). From that, we see that the percent sign is just a sigil that prefixes a register name.
The “*8(” part is a bit trickier. Under “Memory Addressing”, we see that a memory takes the form of “segment-override:signed-offset(base,index,scale)”. Don’t ask me what all that is, but it seems in our case we can simplify that down to “signed-offset(base)”. Lower down, we see that “Branch addressing using registers or memory operands must be prefixed by a ‘*’”. So it appears that this instruction says: Call the code that is located 8 past the address in the EDX register. Cool!(3)

It’s still on my to-read list, but if assembly language is interesting, you might want to check out Paul A. Carter’s PC Assembly Language free PDF book, geared towards assembly language from C. Let me know how it goes!


  1. …from the days when the processor was closer, and just wanting to know how it worked, as well as wanting to write über-optimized code, modify my GPS’s firmware and make things with embedded processors…with only an assembler and raw coder-manliness! Now that Apple uses x86 on their desktop machines, that’s all the more reason to learn
  2. To fully decode Assembly Language, you’ll also need a mnemonic reference for your architecture. The sig9 tutorial uses IA-32, which is has a good chance of being what you’re using.
  3. Or in our case, not so cool. Since the EDX register contains 0, this would call code at address 0×8, which isn’t our code. Thankfully, the kernel detects an address this messed up and puts a quick stop to the program. However, most of those worrisome “arbitrary code execution” security holes which Microsoft was particularly good at work using similar unexpected-address calls: a cracker finds a way to a) put some machine code into memory, and b) put a “call” into the list of upcoming instructions that will run said machine code.

February 3, 2007

Fat vs. Universal

written by natevw @ 7:52 pm

Sounds like the MPAA looking to devour another tasty morsel compliments of our fine legislative system. But today’s topic is Virtual Machines, not virtual monopolies. By “Virtual Machine”, I don’t mean “Virtual Computer”, I mean “Virtual Processor”. There’s much to be said about the uses and shortcomings of virtual machines. I’d like to focus specifically on how the technology could, or could have, benefit Apple formerly Computer.

Apple has the privilege of designing it all: hardware, operating system and a leading share of the applications. Contrast “design” with “dictate” — it wouldn’t be practical for them to design all the chips they need, nor are they able to write software for every niche that has a need. Even their operating system, whether due to beneficence or lack of market domination, follows an admirable number of external standards. One of these is LLVM, a Virtual Machine standard already integrated into part of Leopard’s graphics system.

The two outside ends, hardware and applications, are the areas where Apple must take extra care in its design to give some deference to the plans of its suppliers and developers. And when it comes to processor choice, both come into play. Apple has already led a fairly smooth transition from PowerPC-compiled applications to so-called “Universal Binaries“. These programs are really just fat binaries, a technology which has been in use on the Mac for quite a while now.

Fat -vs- Universal logos

“Universal” is a misnomer: the text may read one way but, as the dyadic logo implies, the reality is that most Universal Binaries are compiled for only 2 platforms, PowerPC and Intel. The next time Apple asks its developers to “check the box” and recompile could be the last for a long time if they move to an actual Universal Binary compiled to run on top of a Virtual Machine. Apple’s involvement in LLVM has been known since at least 2005, and I have no doubt they could easily pull it off. It might feel like joining the ranks of Microsoft “Why are they yelling?” .NET and Java with its “write once, ugly anywhere”, but I think it would still warrant applause at a future WWDC convention.

« Previous PageNext Page »