views:

521

answers:

1

I have created an accelerometer variable:

UIAccelerometer   *objAccelerometer;

that I am associating to the sharedAccelerometer instance:

objAccelerometer = [UIAccelerometer sharedAccelerometer];
objAccelerometer.delegate = self;

When I release this view (to load a different view), the accelerometer instance causes the program to die. (If I have it commented out, I can switch between views without a problem).

I had a similar problem with an NSTimer, but once I called:

[myTimer invalidate];

(prior to releasing and switching the views) everything worked fine.

How should I properly release the delegate or deallocate or release the accelerometer?

+5  A: 

You don't release the shared accelerometer - you never retained it (and shouldn't) - it's a singleton.

The accelerometer has a reference to your object. For it to release it just set it's delegate property to nil (because it's a property it will release its reference to your object).

Phil Nash
That did it! I thought there might be a function (like release or invalidate) -- but nil worked perfectly!
Jeffrey Berthiaume
No problem. Read up on Objective C 2.0s property syntax to be sure you know what's going on here
Phil Nash