views:

60

answers:

4

Hello everyone

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

A: 

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 ?

matei
+2  A: 

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.

Claus Broch
A: 

I got memory leak after removeAllobjects

the methods of MYFileObj

-(void) setFileName:(NSString *)s ;
{
    [fileName release];
    fileName= [s copy];
}
-(NSString *) fileName ;
{
        return [[fileName copy] autorelease];
}
you should not use the answer-section to clearify your question
Till
sorry, in comments, I can not show my codes clearly
+1  A: 

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];
}
Laurent Etiemble
Wrong setter. `[obj setFileName:[obj fileName]]` will crash your app.
KennyTM
Fixed the setter (I hope). Thx.
Laurent Etiemble