CocoaDev

Edit AllPages

I’m wondering why I can’t initialize everything I want in the method initWithFrame: of my screen saver. Here is my code :

#import “BalleView.h”

@implementation BalleView

}

@end

NSColor seems to be fine, but NSBezierPath, no.


I mentioned the answer briefly in ScreenSaverVariables, but I’ll mention it again here. Both balle and couleur claim to be “autoreleased” objects, temporary objects that will be automatically deallocated (if appropriate) at the end of the current event loop cycle. Therefore, when you access balle later on in animateOneFrame, your program will crash.

Interestingly, couleur won’t cause your program to cache. The AppKit is probably creating a single instance of “redColor” that never gets freed. You probably shouldn’t rely on that fact, since it is an implementation detail.

See MemoryManagement for more information, especially RetainingAndReleasing.

Here’s how to properly retain/release objects in your initWithFrame:isPreview: method:

– MikeTrent