NSAttributedStrings and autorelease

First of all, greetings from C4[1]… it’s really nice here, and I absolutely enjoy the show.

I have been trying to track down a bug in CuteClips3 these days. It was a very strange bug and it only happend with faulty RTF data on clipboard. But that alone wasn’t the reason for the crash. The crash was one of those nice memory bugs that occur when an object is released and then accessed…sometimes it works, sometime it doesn’t, most time it crashes, but you just can’t really debug it.

So finally i found out what went wrong with all this:

	[[[NSAttributedString alloc] autorelease] initWithRTF:[self data] documentAttributes:nil];

So I was autoreleasing this new object, but then it was initialized with faulty data. Trying to initialize attributed strings with faulty data is returning nil, so this is not a big deal. The problem is, that the initialize method will also release the attributed string while it was already marked autorelease. So the autorelease pool will also try to release it.

So the correct order would be like this:

	[[[NSAttributedString alloc] initWithRTF:[self data] documentAttributes:nil] autorelease];

That fixed the bug for me 😀

So now I’ll just continue to enjoy the rest of C4, although it’s almost over now.

Karsten

One Response to “NSAttributedStrings and autorelease”

  1. Karsten Says:

    thx man, but it’s already over unfortunately…but it was really an absolutely cool conference!!