1

I hope to store MYFileObj to NSMutableArray (fileArray) and display data on an UITavleView(tableview).

//----------------------------------MYFileObj
#import <UIKit/UIKit.h>


@interface MYFileObj :  NSObject  {

    NSString *fileName;

}

-(void) setFileName:(NSString *)s ;
-(NSString *) fileName ;


@end

the array I want to store data

NSMutableArray *fileArray;

I created new object and add to fileArray

MYFileObj *newobj=[[MYFileObj alloc] init ];
NSString *ss=[[NSString alloc] initWithFormat:@"%@",path]  ;
[newobj setFileName:ss];
[ss release];
[fileArray addObject:newobj];
[newobj release];

[atableview reloadData];

After the first time relaodData and do something, I want to reload fileArray and redraw atableview.

//code to remove all object in atableview
if([fileArray count]>0)
{  
   [fileArray removeAllObjects];
   [atableview reloadData];
}  

I notice that there are memory leak. I hope to know the method "removeAllObjects" removes only MYFileObj themselves or also removes MYFileObj's member property "fileName"?

Thanks

interdev

1
  • Please use declared properties. The getter and setter can be created automatically with @property(copy,nonatomic) NSString* fileName; in the interface and @synthesize fileName; in the implementation. Commented Apr 25, 2010 at 12:55

3 Answers 3

2

You do not state where you have detected the memory leak, but I'll assume from the posted code that you are not releasing the fileName in MyFileObj's dealloc method.

Sign up to request clarification or add additional context in comments.

Comments

1

A good practice is to use retain/release in the setter. This way you avoid unnecessary object creation/copy:

- (void)setFileName:(NSString *)s {
    [s retain]; // <- Retain new value
    [fileName release]; // <- Release old value
    fileName = s;
}

- (NSString *)fileName {
    return fileName;
}

- (void)dealloc {
    [fileName release]; // <- Release the instance
    [super dealloc];
}

2 Comments

Wrong setter. [obj setFileName:[obj fileName]] will crash your app.
Fixed the setter (I hope). Thx.
0

it depends how you implemented setFileName and dealloc in your MyFileObj class. Do you send a release message to fileName in dealloc ? Do you send it a retain in your setter ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.