I have written class and want show it to you ...
I think that this class written not correct and thats why I have leeks in my application. First of all the delloc
never calls. What can I change in this class to make it better, please help.
Articles.h
#import <Foundation/Foundation.h>
@interface Article : NSObject {
BOOL favorite;
NSMutableString * title;
NSMutableString * summary;
NSMutableString * mainLink;
NSMutableString * pubDate;
NSMutableString * author;
NSMutableString * imageLink;
}
@property (nonatomic, assign) BOOL favorite;
@property (nonatomic, retain) NSMutableString * title;
@property (nonatomic, retain) NSMutableString * summary;
@property (nonatomic, retain) NSMutableString * mainLink;
@property (nonatomic, retain) NSMutableString * pubDate;
@property (nonatomic, retain) NSMutableString * author;
@property (nonatomic, retain) NSMutableString * imageLink;
- (id)initWithValues:(NSString *) inTitle mainLink:(NSString *) inMainLink summary:(NSString *) inSummary
pubDate:(NSString *) inPubDate author:(NSString *) inAuthor imageLink:(NSString *) inImageLink;
//Setter methods
- (void)setTheTitle:(NSString *) inTitle;
- (void)setTheMainLink:(NSString *) inMainLink;
- (void)setTheSummary:(NSString *) inSummary;
- (void)setThePubDate:(NSString *) inPubDate;
- (void)setTheAuthor:(NSString *) inAuthor;
- (void)setTheImageLink:(NSString *)inImageLink;
@end
Articles.m
#import "Articles.h"
@implementation Article
@synthesize favorite;
@synthesize title;
@synthesize summary;
@synthesize mainLink;
@synthesize pubDate;
@synthesize author;
@synthesize imageLink;
- (void)dealloc {
NSLog(@"article dealloc \n");
[self.title release];
[self.mainLink release];
[self.summary release];
[self.pubDate release];
[self.author release];
[self.imageLink release];
[super dealloc];
}
- (id)init {
self = [super init];
if(self) {
// set your properties...
self.title = [[[NSMutableString alloc] init] autorelease];
self.mainLink = [[[NSMutableString alloc] init] autorelease];
self.summary = [[[NSMutableString alloc] init] autorelease];
self.pubDate = [[[NSMutableString alloc] init] autorelease];
self.author = [[[NSMutableString alloc] init] autorelease];
self.imageLink = [[[NSMutableString alloc] init] autorelease];
self.favorite = NO;
}
return self;
}
- (id)initWithValues:(NSString *) inTitle mainLink:(NSString *) inMainLink summary:(NSString *) inSummary
pubDate:(NSString *) inPubDate author:(NSString *) inAuthor imageLink:(NSString *) inImageLink
{
self = [super init];
if(self) {
// set your properties ...
if (inTitle != nil) {
self.title = inTitle;
}
if (inMainLink != nil) {
self.mainLink = inMainLink ;
}
if (inSummary != nil) {
self.summary = inSummary;
}
if (inPubDate != nil) {
self.pubDate = inPubDate;
}
if (inAuthor != nil) {
self.author = inAuthor ;
}
if (inImageLink != nil) {
self.imageLink = inImageLink ;
}
self.favorite = NO;
}
return self;
}
@end
ADDED:
Look I have NSXMLParser
in my main class. In the main class .h file I write:
Article * currentArticle;
Now In .m file when parser didStartElement I alloc ant initialize Article in parser didEndElement I release it [self.currentArticle release]
; but delloc not calles.
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
// Copy current Xml Element name.
currentElement = [elementName copy];
if ([elementName isEqualToString:@"item"]) {
// Clear out our story item caches...
self.currentArticle = [[Article alloc] init];
}
[currentElement release];
}
ADDED RELEASE FOR TEST
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
// Copy current Xml Element name.
currentElement = [elementName copy];
if ([elementName isEqualToString:@"item"]) {
// Clear out our story item caches...
self.currentArticle = [[Article alloc] init];
[self.currentArticle release];
}
[currentElement release];
}
look I have added [self.currentArticle release];
right after initialization and put breakpoint here ... When at first time my application enters in this part of code it call init but not call release at second time it call release ? But why ? It's not right
WHY I DON'T USE AUTORELEASE !!!
self.title = [[[NSMutableString alloc] init] autorelease];
self.mainLink = [[[NSMutableString alloc] init] autorelease];
self.summary = [[[NSMutableString alloc] init] autorelease];
self.pubDate = [[[NSMutableString alloc] init] autorelease];
self.author = [[[NSMutableString alloc] init] autorelease];
self.imageLink = [[[NSMutableString alloc] init] autorelease];
I do not use autorelease in this part of code because I have read that it brings to leeks, because when I write autorelease the objects releases in the end of application work ! Am I write ???
Thanks !!!