Example code programming Objective-C with Cocoa in Xcode and Interface Builder (Leopard)

42: Stop the Garbage Collector from collecting objects in your nib file

Cocoa example of stopping the Garbage Collector from collecting up objects such as NSObjectController in a .nib (.xib) file

Page under construction

Problem: We are using Garbage Collection in a Cocoa Application and our .xib (.nib) objects including NSObjectController keep getting collected. How do we stop this happening?

Let us explore: create a new project in XCode: File->New Project->Cocoa Application
Call it: 042_Garbage_collection_nib.

In Project->Edit Project Settings set 'Objective-C Garbage Collection' to Required.

Now create the class: MyController of type NSObjectController.
Code it as shown below and save.

// MyController.h #import <Cocoa/Cocoa.h> @interface MyController : NSObjectController { } @end // MyController.m #import "MyController.h" @implementation MyController - (void)awakeFromNib { NSLog(@"Cocoa Application: MyController awakeFromNib"); } // end awakeFromNib - (void) finalize { NSLog(@"Cocoa Application: MyController finalize"); [super finalize]; } // end finalize @end

Click on the MainMenu.xib to go into Interface Builder.

Drag an NSObjectController onto the mainMenu.xib panel.In the inspector name it MyController.

Save and run.

The nib, xib in Interface Builder

The nib, xib in Interface Builder

Program output showing that the finalize method was called

Console output showing that the finalize method of MyController was called

As may be seen from the Console output above, the finalize method of MyController was called which shows that the object was destroyed.

Stop the program and go into Interface Builder. In the MainMenu.xib click on the File's Owner icon. Open the Inspector Connections pane and drag from the delegate outlet to the MyController icon.

Save and run.

In Interface Builder make the NSObjectController a delegate of File's Owner.

Make the NSObjectController object (MyController) a delegate of File's Owner

Program output showing that the finalize method of the NSObjectController object was not called.

Console output showing that the finalize method of MyController was not called

Great! The finalize method was not called which means that our object survived the Garbage Collector.

What if we have more than this one top level object in our .xib/.nib?.

Create three new Objective-C class objects: MyObject1, MyObject2, MyObject3 and put an awakeFromNib and a finalize method into each one as shown here for MyObject3.

// MyController.h #import <Cocoa/Cocoa.h> @interface MyController : NSObjectController { } @end // MyController.m #import "MyController.h" @implementation MyObject3 - (void)awakeFromNib { NSLog(@"MyObject3 awakeFromNib"); } // end awakeFromNib - (void) finalize { NSLog(@"MyObject3 finalize"); [super finalize]; } // end finalize @end

Go into Interface Builder and drag three objects from the palette onto the MainMenu.xib panel.

In the Inspector rename them as MyObject1 etc.

Save and Run.

As can be seen below, the finalize method of each of the objects 1, 2, 3 was called.

Program output showing that the finalize method was called for MyObject1 through 3

Console output showing that the finalize method of MyObject1 through 3 was called

In XCode change the header file of MyController as shown below.

// MyController.h #import <Cocoa/Cocoa.h> #import "MyObject1.h" #import "MyObject2.h" #import "MyObject3.h" @interface MyController : NSObjectController { IBOutlet id myObject1; IBOutlet id myObject2; IBOutlet id myObject3; } @end

In Interface Builder select MyController and in the Inspector Connections pane drag from each of the outlets to the respective object as shown.

In interface builder link the outlets of MyController (NSObjectController) to each of the objects MyObject1, 2, 3.

In interface builder link the outlets of MyController (NSObjectController) to each of the objects MyObject1, 2, 3.

Save and Run.

Success! Our objects survived the GC! Their finalze methods were not called.

Program output showing that the finalize method was NOT called for MyObject1 through 3

Console output showing that the finalize method of MyObject1 through 3 was NOT called

If you want to download the code

Click the Download Link to obtain 042_Garbage_collection_nib.zip file of this whole OS X 10.5 Leopard program.

Download 042_Garbage_collection_nib.zip (2.3 MB)



Please send me your comments

If you include your e-mail I may reply!  

Page last modified: 18:58 Sunday 12th. May 2013

Julius Guzy

Paintings & Drawings

  • Link to View at Bogay near Derry (river Foyle in distance)

animatedPaint