Thursday, April 28, 2011

How do you release a shared accelerator instance on the iPhone?

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?

From stackoverflow
  • 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).

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

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.