Archive for February, 2009

Fun with AutoRelease Pools in Cocoa Programming

Thursday, February 5th, 2009

xcode.jpg

I was talking to a friend today about the use of [[obj alloc] init] and the AutoRelease pools. Turns out, Apple recommends not to use them in iPhone applications. And that got us to dissecting them, to figure out why Apple wouldn't want this very handy feature used in iPhone apps.

First, we need to lay a few ground rules. When I have the code:

  MyObject*  obj = [[MyObject alloc] init];

the reference count on obj is set to 1. What really does this? Well, it's the alloc. Create it (alloc), and it's reference count is 1. Simple rule to remember.

So what if I want to create something, use it in my method, and then get rid of it? In these cases, you know the lifetime of the object is within the scope of your method, and in that case, you can choose to use the more efficient:

  MyObject*  obj = [[MyObject alloc] init];
 
  // ...use this guy for anything you need
 
  [obj release];

the last line in the code sample balances the alloc in the first line and reduces the reference count by one, and when zero, the object is removed. Easy. But this is very 'manual' - a lot like the C malloc() and free(). But it is efficient. So you just have to decide what's more important - efficiency or a little leeway on the object lifecycle?

For example, say we might want to keep this, but we might not. Then what? If we had written the code like this:

  MyObject*  obj = [[MyObject alloc] init];
 
  // ...use this guy for anything you need
 
  // if we have no error, keep this guy
  if (!error) {
      [self setObject:obj];
  } else {
      [obj release];
  }

Now the question becomes Can't we make this cleaner? and the answer is you bet we can:

  MyObject*  obj = [[MyObject alloc] init] autorelease];
 
  // ...use this guy for anything you need
 
  // if we have no error, keep this guy
  if (!error) {
      [self setObject:obj];
  }

with the addition of the autorelease call, we have placed this instance into an NSAutoreleasePool and at the end of the next run loop, it will clear out the pool, calling release on each item in the pool, and those whose retain counts hit zero are going to be freed.

Why is this better? Well... because when we create it, we can put it in the state that says "Hey, if I don't do anything with this guy, clean him up, but leave me the option to keep him." This, of course, is not as efficient as the first case, but this is far more flexible. If there are times when you might need to keep ahold of something you've created, this is the cleaner solution.

There's also the question of the Factory Pattern. If I'm making things for (possible) consumption, then I need to autorelease them before I return them. This allows the caller to either retain them or not, and if not, then at the end of the next run loop, the NSAutoreleasePool will clear it out and be done with it.

Remember, calling autorelease doesn't change the retain count - it just puts it in a pool that eventually will release the object and then possibly free it. So, it's in the definition of eventually that the inefficiency comes it.

In general, it's a decent idea to do [[[obj alloc] init] autorelease] if you're making a few dozen things at any one time. The NSAutoreleasePool doesn't get too bug, and it's cleaned up easily enough. Also, it gives you the option of having the same initializer code for those things that might stick around, and for those that won't. But make no mistake, it's not as efficient as not using it and careful scoping of the instance lifetime.

This seems to be the point with Apple on the iPhone. It's not as efficient, and with all the things on that little guy, resources have got to be at a premium. So, if you can control the scope of your instances, then do the simpler [[obj alloc] init] with a release when you're done. But if you can't do that, then use autorelease. Just realize the impact to your application.

As a final note, here are some interesting examples of where autorelease helps. Imagine that we have the following code where we create one object, hold on to it, create another, and then message the first. This would be recognized by a C coder as the classic dangling pointer:

  MyObject*  obj = [[MyObject alloc] init];
 
  // ...use this guy for anything you need
 
  // hold on to him for a sec
  MyObject*  holder = first;
 
  [obj release];
 
  // make another - maybe with different parameters
  obj = [[MyObject alloc] init];
 
  // ...use this guy for anything you need
 
  // message the first - and it'll blow up
  [holder doSomething];
 
  [obj release];

this guy is going to blow up because the original instance is long gone, but the pointer is still being held in holder. This seems obvious, but there are tons of ways you can get yourself into this bind by managing the release yourself. You have to be very careful if you're not using autorelease.

But in the iPhone, you have to try.

[2/6] UPDATE: I knew there was a good reference on this topic. This is from an old Stepwise.com posting in 2005, but it's as true to day as then. It's got great coverage of all the rules. Certainly something to keep handy when you get a little confused about the topic.

17″ Unibody MacBook Pro Delayed

Thursday, February 5th, 2009

MacBookPro17.jpg

I read this morning of a letter sent out by Apple to the initial orders on the 17" Unibody MacBook Pro:

To Our Valued Apple Customer:

Thank you again for your order! Wrapping up the new 17-inch MacBook Pro is taking a few days longer than we projected. As a result, we will be unable to ship your 17-inch MacBook Pro until February 19, 2009.

While mine is still slated to be shipped Feb 23rd, I have to wonder if there's going to be a problem that impacts that shipping date. Don't know. We'll have to wait and see. It's not like I'm not going to wait for it.

Apple Updates GarageBand ’09 on Software Update

Thursday, February 5th, 2009

iLife09.jpg

Turns out, there was a slight problem with the "Learn to Play" feature of GarageBand '09 - and so today Apple released an update to GarageBand on Software Update. I haven't used GarageBand, but it's possible I might. The kids are getting a few new instruments and it might be interesting to be able to show them how to get some background music for their tunes.

Gotta love that Software Update...

A Kick in the Teeth Always Hurts – No Matter Who You Are

Wednesday, February 4th, 2009

cubeLifeView.gif

I can't help but feel sad for a guy that I've been working with for over seven years. Yesterday, he was "let go" by the trading-side of the business, and then offered a job on the development-side of the shop. Don't get me wrong, I think he'll be a fine developer. But getting that one-two punch in the gut yesterday had to hurt. I don't care who you are.

We can all be pushed to our limits and show our less wonderful sides to people. It's what happens. Stress causes us to get more "kill or be killed" and that, in turn, suppresses our kindness and tolerance for others, and we show a face to others we might not want to show. It's happened to me, it'll happen again. It's happened to this guy, and it had to be a contributing factor in his getting released from the business-side.

The reason I feel for this guy is that becoming a developer for him now is a nice landing, but it's like having once been a Vegas Headliner and then going back to teach elementary school kids how to sing and dance. It's a huge blow to the ego. I don't care who you are.

Some may certainly get over it faster than others, but we're still in the first 24 hrs. and that's definetely in the "ouch" phase of this particular life-lesson. I just wonder if management is really thinking he's going to stick this demotion out. Sure, the economy is not strong, but this is the financial sector, and there always seems to be jobs out there - someone is making money. If not on the market, then on the volatility. So I wonder if he's going to stick it out.

I hope he finds what he's looking for. Whatever that is.

Firefox 3.0.6 is Out with Security Fixes

Wednesday, February 4th, 2009

Firefox.jpg

I noticed that Firefox 3.0.6 was out this morning with a handy load of security and stability fixes. I think that's the standard mantra these days for finding holes in the code that can be exploited for incoming hacks and plugging them without acknowledging that they ever existed in the first place.

After all, if there's a published problem with a certain library - like OpenSSL, then the update would specifically state that in the release notes. But hey... that's reasonable. They are probably working on a big new release and these are the maintenance things that need to be done given all the users that are depending daily on Firefox as their browser.

A Super Fast Internet Connection Arrives!

Tuesday, February 3rd, 2009

pirate.jpg

OK, the technician just left, and I have to say that he was a very nice guy about the installation. He was not used to all Macs (save one linux box), but we got it installed and working in no time at all.

Now I'm running at 50Mbps down, 10Mbps up. Very nice. Sure, it's just the "next level", but it's something that I've wanted to have for a long time. No longer is the speed of my connection the limiting factor - it's going to be the hardware on the ends that decides. And, with my new laptop, that's not going to be a real problem.

Love the speed. Highly recommended.

UPDATE: OK, from work the speed into the house is fantastic! I can't believe it. It's as if I'm sitting there. True, a lot of it may be because the line here is not very loaded - and I'll have to check later in the day, but it's a wonderful sign so far. Wow... this is what I've wanted for a long time. I'm really glad I got it.

House is Causing Problems with Homework on Mondays

Tuesday, February 3rd, 2009

TV.jpg

My family doesn't watch a lot of TV together, but every time we have an episode of House, M.D. on the DVR to watch, it's time to get everyone together and sit and watch carefully. Everyone tries to pick up the clues, tries to match the BINGO card of House-imsm like "Plasmaferesis" and "it's Lupis", or "get a CAT scan"... it's literally fun for my entire family. And I love every minute of it.

However, last night wasn't the typical House joy-joy as it caused a little problem for the kids. First, we have been having 'issues' with the Comcast DVR box - the fast forwarding would jump minutes and not seconds, just to the end of the recording or to the beginning - seemingly randomly. So Liza called Comcast yesterday and they said that they'd have a technician come out and bring a new DVR box (dual HD tuner). Problem is that all the shows are physically on the DVR, so new box means no shows. This means that the House, M.D. that was taping at 7:00 pm last night on FOX would be gone today when the DVR switch-a-roo happened. No good.

So we decided to watch the episode last night. Yeah... not the first thing you'd see in "Parenting 101"... but these are desperate times, and require desperate measures.

Problem was that after the show at 8:00 pm, two of the kids had homework left to do that they "neglected" to tell us about when they got home and before dinner. So they were planning on sneaking it in after dinner, but then House was far more interesting, and so they put it off.

Procrastination is a type of behavior which is characterized by deferment of actions or tasks to a later time. Psychologists often cite procrastination as a mechanism for coping with the anxiety associated with starting or completing any task or decision.

Thanks Wikipedia... I could have put the pictures of my kids and Liza up there and be just as good. They all get it from her, and she knows it. But, she also got on them for it, and forced them to get their work done quickly so that they still had a reasonable bed time.

So we have to be more careful about replacing DVRs on Tuesday. Prior planning - that's what it's all about.

Trying to Clean Up Some Ancient Audio Files

Tuesday, February 3rd, 2009

WaveEditor.jpg

I was looking at trying to clean up a few digital recordings I'd made back on my old NeXTSTEP turbo Color Slab, which is now gone to the great computer room in the sky, as there was a lot of noise in the samples and it seemed like a simple thing to do - filter it, baby. High-pass filter to be more precise. So I started looking around to see what I had to work with.

Unfortunately, when I sent an email to the Fission creators, it was clear that Fission was going to be a time-slice editing tool - lossless editing in their marketing terms, and I can certainly respect that. It's far more likely in this day and age to have access to a good digital master than the crummy digital master I have now. But be that as it may, it was clear after emailing them that I wasn't going to be able to use Fission for this - I had to look elsewhere.

The next thing I saw was Audacity 1.3.7, a free audio editor. It looked like it had decent capabilities, including a lot of plugins that got installed into the system at /Library/Audio/Plug-Ins/ so that they might be used by many different audio editing applications. While I liked the capabilities of Audacity, it just wasn't getting done what I needed. However, it did point out to me that these plugins might be useful by other apps, and that was very helpful.

The next thing I found was something I'd hit on before - Wave Editor 1.4.4. This was a very nicely done audio editor, and while it was $79, I'm not against paying for a good tool when I see it. What I was able to see when I started playing with it, in depth, was that it had the tools to filter out the noise pretty effectively. I was still a little concerned that it suppressed the volume as much as it did when I applied the HPF, but that's probably something I can deal with in the app as well.

It had the nice tools like a spectrum analyzer and lots of ways of looking at the data - very much as I might have written it - from a math and engineering standpoint. Pretty nice. I also like that they have a complete support system built right into the app so I can ask a question if I get stuck on something. Pretty nice for a tool like this that there's a steep learning curve on.

So I'm going to see if I can't fix up these old files and clean out the noise. Sure do hope so.

GitX 0.6.1 is Out

Tuesday, February 3rd, 2009

GitX.jpg

This morning I noticed that GitX has been updated to 0.6.1 with quite a few new features from version 0.5 that I had been running. I haven't done a lot in Git, but I know that's where I'll be doing all the work I want to do, and for the little projects that I have put into Git, it's a great little tool for visualizing the repository.

I'm still a command-line guy, but old habits die hard. If only Xcode put in Git support. That would be great.

BBEdit 9.1.1 is Out

Tuesday, February 3rd, 2009

BBEdit.jpg

It looks like the guys at Bare Bones software have been busy working on BBEdit 9.1.1 which seems to be just a fixes release. No new features at this time, but plenty of bugs fixed - some even crashers. Nice to see that the updates are still coming.