tags:

views:

43

answers:

4

Hi guys,

I am getting the memory leak in my objects setters can any one help me to resolve this??

Code:

  - (void)setEstimateTax2Type:(NSString *)aEstimateTax2Type
{

if ((!estimateTax2Type && !aEstimateTax2Type) || (estimateTax2Type && aEstimateTax2Type && [estimateTax2Type isEqualToString:aEstimateTax2Type])) return;

[estimateTax2Type release];
estimateTax2Type = [aEstimateTax2Type copy]  ;
}

Thank's in advance.

Monish.

A: 

Your conditional:

if (
  (!estimateTax2Type && !aEstimateTax2Type) || 
  (estimateTax2Type && aEstimateTax2Type && 
    [estimateTax2Type isEqualToString:aEstimateTax2Type])
) return;

Appears to terminate the function before releasing the memory:

[estimateTax2Type release];

Although I don't see any alloc

John Weldon
The conditional terminates the method only when both objects are nil or equal. So it should be OK.
JeremyP
+2  A: 

The easiest way to get the setter correct (for example your condition is entirely unnecessary):

//.h
@property (nonatomic, copy) NSString *estimateTax2Type;
//.m
@synthesize estimateTax2Type;
Graham Lee
note that with this you still need to implement dealloc and release estimateTax2Type
cobbal
+1  A: 

There's no problem with sending messages to nil. So your test can be:

if ([aEstimateTax2Type isEqualToString: estimateTax2Type])
{
    return;
}

However, that's not the cause of your leak. I suspect, you are not releasing estimateTax2Type in your dealloc method.

JeremyP
+1  A: 

do you have a dealloc method to release estimateTax2Type when you're class is dealloced?

- (void)dealloc {
    [estimateTax2Type release];
    [super dealloc];
}
cobbal
Thank's for all for your valuable suggestions.
monish