Pasteboard Op Hosed My Mac

Today, during lunch, I continued to work through Aaron Hillegass‘s book, Cocoa Programming for Mac OS X. I was finishing up the chapter on using the Pasteboard for cut & paste, and my sample app was working as it should. And on my shiny Mac Pro, it ran very fast, indeed.

Then I got to the “Challenge” section… The challenge was to have the app copy its data to the pasteboard in both text and PDF formats. I got this working in about 5 minutes; no sweat.

Then I got creative… In the “For the More Curious: Lazy Copying” section, he explained how to have an application tell the pasteboard that it could provide data in various formats, just like usual, but to only put the data on the pasteboard when specifically asked by another app. I thought to myself, “Self, you should implement that with the text + PDF copying you’ve already got working.” And so I did. I got the code written in just a few minutes, referring to the API docs for various help. I ran my app, and copied the data. So far, so good. Then I went into the Preview app, selected File>New From Clipboard… and that’s when things started going South. I got the spinning beach-ball on Preview, and never a window opening. I toggled back over to my app, and it, too, was spinning. I killed Preview and my app, but trying to restart my app resulted in the window showing up, but the spinning beach-ball, and nothing else.

At this point, I figured I had hosed something, so I killed off Xcode and tried to restart it. Same thing as with the others: menubar + spinning beach-ball. Now, I figured a reboot was going to be required. And so it was, but actually rebooting proved to be a problem. I started killing off all my apps and while some would actually quit, some wouldn’t. I tried killing them from Activity Monitor, which worked on some, but not others. I then went to the Apple menu and selected Restart… which eventually timed out trying to kill the running apps. I tried again, but it still wouldn’t restart. I killed some more apps, and tried restarting again, but no dice. I finally killed the power to the box and turned it back on.

If anyone is interested, here are the relevant pieces of code that were involved in this fiasco.

The class is called BigLetterView, and it’s a subclass of NSView. The first method to see is copy:

 	- (IBAction) copy: (id) sender 
	{
 	    NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
 	    [self declarePasteboardTypes: pasteboard];
  	    NSLog(@"Backing up data");
 	    [self backupDataForCopy];
 	    NSLog("@Backed up data");
  	    // [self writeStringToPasteboard: pasteboard];
 	    // [self writePdfToPasteboard: pasteboard];
 	} 	

Next is the method called from copy: that makes a copy of the data so that if the current values changes, a paste from another app will get the data as they were when the copy was invoked.

 	- (void) backupDataForCopy
 	{ 
	    backupString = [string copy];
 	    backupPdf = [self generatePdf];
 	} 	

Moving right along, we come to the method that is invoked when another app tries to paste our data from the pasteboard.

 	- (void) pasteboard: (NSPasteboard *) pasteboard provideDataForType: (NSString *) type 	{
 	    NSLog(@"Pasteboard requesting data of type: %@", type);
  	    if ([type isEqual: NSStringPboardType])
 	    {
 	        [self writeStringToPasteboard: pasteboard];
 	    } else if ([type isEqual: NSPDFPboardType])
 	    {
 	        [self writePdfToPasteboard: pasteboard];
 	    } else
 	    {
 	        NSBeep();
 	    }
 	} 	

And, finally, the two methods that write the data to the pasteboard when asked

 	- (void) writePdfToPasteboard: (NSPasteboard *) pasteboard 
	{
 	    // NSData *data = [self generatePdf];
     	    // [pasteboard setData: data forType: NSPDFPboardType];
 	    [pasteboard setData: backupPdf forType: NSPDFPboardType];
 	}

  	- (void) writeStringToPasteboard: (NSPasteboard *) pasteboard
 	{
 	    // copy data to the pasteboard
 	    [pasteboard setString: backupString forType: NSStringPboardType];
 	    // [pasteboard setString: string forType: NSStringPboardType]; 	} 	

The problem seems to be in pasteboard:provideDataForType:, though I can’t see what it is. When I hit Cmd-C, the copy works OK. When I switch to Preview and select File>New From Pasteboard is when everything locks up. What looks to be happening is that the pasteboard service (pbs) is getting hosed, and it’s considered a “critical service” by the OS (I read that in some OSX docs), so if it’s dead/dying other apps who try to contact it are locking up. Makes sense. I tried restarting pbs, but that didn’t seem to make a difference.

So, has anyone else fought with this situation? If so

  • Do you see a problem with the code snippets above?
  • How can you recover from hosing pbs?
  • Any other suggestions?

Wow, That’s Quite a CPU You Have There…

I noticed that when I moved over to the new, shiny Mac Pro that I hadn’t brought over all my digital photos. I’m glad I realized this before whacking the hard drive in the old Windows machine… Anyway, I brought over 2Gb of photos and started importing them into iPhoto. Shortly after it started, I noticed my iconified activity monitor showing activity on all four cores of the CPUs, so I opened it and saw a very large number for CPU percentage for iPhoto. I assume it’s some sort of sum of cpu utilization across the cores, but it looks pretty funny, however they compute it.

Firefly Incident at Walt Disney World

We just got back from 10 days at Walt Disney World and it was great, as usual. I’ll have more to say later, including a critique of the current state of the four parks, and their rides and shows. But right now I have a strange juxtaposition of Disney World with Firefly.

My friends know that I’m an absolute fanatic for Firefly, the short-lived Joss Whedon show from a few years ago. I have watched every episode on DVD multiple times, and have worked hard to evangelize my friends. Several of them have heeded my advice and are now hooked, themselves.

Anyway, on the second day of our trip, I wore a t-shirt with the Blue Sun logo on it. I didn’t really expect anyone to recognize it, but you never know. We were at Epcot that day, and were having lunch at the Garden Grill restaurant. This restaurant is in The Land pavilion and features Farmer Mickey, Pluto and Chip & Dale who come around to each table to see the kids. Mickey came round first. I took a picture of him with Thomas, and off he went. Then came Pluto. He was playing with Thomas, when all of a sudden he (Pluto) starts emphatically pointing at me. I was trying to figure out what he meant, checking my face for food, etc. Finially, it dawned on me that he was pointing at my shirt. I said “My shirt? Is Pluto a Firefly fan?” At that he began jumping up and down, pointing one paw at me, and the other at his nose (as in “right on the nose”). I then said, “So, you’ve got a TV out in the doghouse?” To which he began nodding vigorously, still jumping up and down, and pointing at me. He then made me give him the double-knuckle high-five over the table.

And that was it. I never expected Pluto to recognize the t-shirt, but he did. He was the only one, but it was so funny. I laughed about it for the rest of the day.