Sheehan Alam
Sheehan Alam

Reputation: 60859

Checking for nil before releasing an object

Is this good or bad practice?

if (!theConnection && !receivedData) {
        // release the connection, and the data object
        [theConnection release];
        // receivedData is declared as a method instance elsewhere
        [receivedData release];
    }

Upvotes: 0

Views: 164

Answers (2)

Eiko
Eiko

Reputation: 25632

It's bad practice - just look through Apple's sample code to get a feeling for how it should like.

Use [theConnection release];

or [theConnection release]; theConnection = nil;

and the same for receivedData.

Upvotes: 2

Vladimir
Vladimir

Reputation: 170829

Sending any message to nil object has no effect, so you can safely remove that check.

Moreover your code leaks memory in case only 1 of the object is non-nil.

Upvotes: 5

Related Questions