Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-11-2010, 08:49 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 49
mramin05 is on a distinguished road
Default Memory leak problem using NSXMLParser

Hi guys,
In my app I have xml parser( NSXMLParser) to parse rss data.
But I could not detect the memory leak in my parsing code. I have tried every possible way by me. But "Start with performance->Leaks" shows memory leak. I am afraid to be rejected from apple.
Here is my code
TopDataReader.h
Code:
#import <Foundation/Foundation.h>


@interface TopDataReader : NSObject {

	NSXMLParser * rssParser;
	NSMutableArray * stories;
	// a temporary item; added to the "stories" array one at a time, and cleared for the next one
	NSMutableDictionary * item;
	
	NSString * currentElement;
	NSMutableString * currentTitle, * currentCategoryId, * currentId, * currentPubDate, *currentDescription , *currentImgLink;
	NSNumber *pageNUmber;
}

@property(nonatomic,retain) NSMutableString * currentTitle;
@property(nonatomic,retain) NSMutableString * currentCategoryId;
@property(nonatomic,retain) NSMutableString * currentId;
@property(nonatomic,retain) NSMutableString * currentPubDate;
@property(nonatomic,retain) NSMutableString * currentDescription;
@property(nonatomic,retain) NSMutableString * currentImgLink;
@property(nonatomic,retain) NSNumber *pageNUmber;

- (void)loadNewsData:(int)pageNumber;
- (NSMutableArray*)getXMLData;

@end
TopDataReader.m

Code:
#import "TopDataReader.h"


@implementation TopDataReader

@synthesize currentTitle, currentCategoryId, currentId, currentPubDate, currentDescription , currentImgLink;
@synthesize pageNUmber;

- (void)loadNewsData:(int)pageNumber {

	self.pageNUmber= [NSNumber numberWithInt: pageNumber];
	// Added parser code
	
	NSString * path = [NSString stringWithFormat:@"http://myurl?page=%d",pageNumber];
	
	self parseXMLFileAtURL:path];
	
}

- (NSMutableArray*)getXMLData{
	
	return stories;
}


- (void)parseXMLFileAtURL:(NSString *)URL
{	
	
	stories = [[NSMutableArray alloc] init];

    //you must then convert the path to a proper NSURL or it won't work
    NSURL *xmlURL = [NSURL URLWithString:URL];

    rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
	
    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [rssParser setDelegate:self];
	
    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [rssParser setShouldProcessNamespaces:NO];
    [rssParser setShouldReportNamespacePrefixes:NO];
    [rssParser setShouldResolveExternalEntities:NO];
	
    [rssParser parse];
	

}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {

}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{			
  
	currentElement = [elementName copy];
	if ([elementName isEqualToString:@"item"]) {
		// clear out our story item caches...
		item = [[[NSMutableDictionary alloc] init] autorelease];
		currentTitle = [[[NSMutableString alloc] init] autorelease];
		currentCategoryId = [[[NSMutableString alloc] init] autorelease];
		currentId = [[[NSMutableString alloc] init]autorelease];
		currentPubDate = [[[NSMutableString alloc] init] autorelease];
		currentDescription = [[[NSMutableString alloc] init] autorelease];
		currentImgLink = [[[NSMutableString alloc] init] autorelease];
		
	}
	
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
	
	if ([elementName isEqualToString:@"item"]) {
		
		[item setObject:currentTitle forKey:@"title"];
		[item setObject:currentCategoryId forKey:@"category"];
				
		[item setObject:currentId forKey:@"id"];
		
		[item setObject:currentPubDate forKey:@"pubDate"];
		[item setObject:currentDescription forKey:@"description"];
		[item setObject:currentImgLink forKey:@"image_link"];
		// Trim white space
		currentImgLink = [currentImgLink stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
		
		NSURL *imageUrl = [NSURL URLWithString: currentImgLink];
		
		NSData *nsDataImage = [NSData dataWithContentsOfURL: imageUrl];
		
		if(nsDataImage != nil){
						
			[item setObject:nsDataImage forKey:@"itemImageData"];
		}
		
		
		[stories addObject:[item copy]];
	
	}
	
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
	
	if ([currentElement isEqualToString:@"title"]) {
		[currentTitle appendString:string];
	} else if ([currentElement isEqualToString:@"category"]) {
		[currentCategoryId appendString:string];
	} else if ([currentElement isEqualToString:@"id"]) {
		[currentId appendString:string];
	} else if ([currentElement isEqualToString:@"pubDate"]) {
		[currentPubDate appendString:string];
	} else if ([currentElement isEqualToString:@"description"]) {
		[currentDescription appendString:string];
	} else if ([currentElement isEqualToString:@"ldnfeed:image_link"]) {
		[currentImgLink appendString:string];
	}
	
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
   NSLog(@"%d", [stories count]);
}


- (void)dealloc {
	if(currentElement != nil){
		[currentElement release];
		currentElement =nil;
	}
	if( rssParser!=nil){
		[rssParser release];
		rssParser =nil;
	}
	if( stories!=nil){
		[stories release];
		stories =nil;
	}
	if( item!=nil){
		[item release];
		item = nil;
	}
	if( currentTitle!=nil){
		[currentTitle release];
		currentTitle = nil;
	}
	if( currentId!=nil){
		[currentId release];
		currentId = nil;
	}
	if( currentCategoryId!=nil){
		[currentCategoryId release];
		currentCategoryId = nil;
	}
	if( currentDescription!=nil){
		[currentDescription release];
		currentDescription = nil;
	}
	if( currentImgLink!=nil){
		[currentImgLink release];
		currentImgLink = nil;
	}
	
	
	[super dealloc];
}

@end

Please help me.
mramin05 is offline   Reply With Quote
Old 11-11-2010, 09:37 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Code:
currentElement = [elementName copy];
You leak this on each pass.

You are not using your properties. You should. And you should have properties for all of your instance variables.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 11-11-2010, 11:39 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 49
mramin05 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Code:
currentElement = [elementName copy];
You leak this on each pass.

You are not using your properties. You should. And you should have properties for all of your instance variables.

Thx for reply.
I made it as below..
Code:
currentElement = [[[NSString alloc] initWithString:elementName] autorelease];
I made remaining items as properties.

Please see the image for the block which create leak
Memory Leak | Flickr - Photo Sharing!
Is it ok?
Still leaking.
mramin05 is offline   Reply With Quote
Old 11-12-2010, 08:35 AM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

I suggest you go read the properties link in my signature, as you don't seem to know how to use them.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 11-16-2010, 07:13 AM   #5 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 49
mramin05 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
I suggest you go read the properties link in my signature, as you don't seem to know how to use them.
HI BrianSlick,
Thanks for your advice. I have tried to utilize you advice. My current code is as below which have leak yet at "parser didStartElement" block.

Code:
#import <Foundation/Foundation.h>


@interface PostXMLReader : NSObject {
	
	NSXMLParser * rssParser;
	NSMutableArray * stories;
	NSMutableDictionary * item;
	NSString * currentElement;
	NSMutableString * currentTitle, * currentCategoryId, * currentId, * currentPubDate, *currentDescription , *currentImgLink;
	NSNumber *pageNUmber;
}

@property(nonatomic,retain) NSMutableString * currentTitle;
@property(nonatomic,retain) NSMutableString * currentCategoryId;
@property(nonatomic,retain) NSMutableString * currentId;
@property(nonatomic,retain) NSMutableString * currentPubDate;
@property(nonatomic,retain) NSMutableString * currentDescription;
@property(nonatomic,retain) NSMutableString * currentImgLink;
@property(nonatomic,retain) NSNumber *pageNUmber;

@property(nonatomic,retain) NSXMLParser * rssParser;
@property(nonatomic,retain) NSMutableArray * stories;
@property(nonatomic,retain) NSMutableDictionary * item;
@property(nonatomic,retain) NSString * currentElement;


- (void)loadNewsRankData:(int)pageNumber;

@end
Here is my Implementation:
Code:
#import "PostXMLReader.h"


@implementation PostXMLReader


@synthesize currentTitle, currentCategoryId, currentId, currentPubDate, currentDescription , currentImgLink;
@synthesize pageNUmber;
@synthesize rssParser;
@synthesize stories;
@synthesize item;
@synthesize currentElement;

- (void)loadNewsRankData:(int)pageNumber {
	//NSLog(@"Top loader Loaded");
	self.pageNUmber= [NSNumber numberWithInt: pageNumber];
	// Added parser code
	
	//if ([stories count] == 0) {
	NSString * path = [NSString stringWithFormat:@"http://myurl/rssnewsrank.php?page=%d",pageNumber];
	//NSLog(@"Path %@", path);
	[self parseXMLFileAtURL:path];
	
	if(path !=nil){
		path = nil;
	}
	//}
	
}

- (NSMutableArray*)getXMLData{
	
	return stories;
}


- (void)parseXMLFileAtURL:(NSString *)URL
{	
	NSLog(@"parseXMLFileAtURL function at topdatareader loaded");
	
	@try {
		
		NSLog(@"Retain count before %d", [stories retainCount]);
		
		NSMutableArray *tmpStories = [[NSMutableArray alloc] init];
		
		[self setStories: tmpStories ];
		[tmpStories release];
		tmpStories = nil;
		
		NSLog(@"Retain count after %d", [stories retainCount]);
		
		//you must then convert the path to a proper NSURL or it won't work
		NSURL *xmlURL = [NSURL URLWithString:URL];
		
		[self setRssParser:[[[NSXMLParser alloc] initWithContentsOfURL:xmlURL] autorelease]];
		
		// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
		[rssParser setDelegate:self];
		
		// Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
		[rssParser setShouldProcessNamespaces:NO];
		[rssParser setShouldReportNamespacePrefixes:NO];
		[rssParser setShouldResolveExternalEntities:NO];
		
		[rssParser parse];
	}
	@catch (NSException * e) {
		
		NSLog(@"Parsing error ");
	}
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
	NSLog(@"Parse Error ");
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{			
    //NSLog(@"found this element: %@", elementName);
	NSString *tmpCurElement = [[NSString alloc] initWithString:elementName];
	[self setCurrentElement: tmpCurElement];
	[tmpCurElement release];
	tmpCurElement = nil;						   
							   
	if ([elementName isEqualToString:@"item"]) {
		// clear out our story item caches...
		NSMutableDictionary *tmpItem = [[NSMutableDictionary alloc] init];
		[self setItem: tmpItem];
		[tmpItem release];
		tmpItem = nil;
		
		NSMutableString *tmpCurrentTitle = [[NSMutableString alloc] init];
		[self setCurrentTitle:tmpCurrentTitle ];
		[tmpCurrentTitle release];
		tmpCurrentTitle = nil;
		
		NSMutableString *tmpCurCatID = [[NSMutableString alloc] init];
		[self setCurrentCategoryId: tmpCurCatID];
		[tmpCurCatID release];
		tmpCurCatID = nil;
							
		NSMutableString *tmpCurID = [[NSMutableString alloc] init];
		[self setCurrentId:tmpCurID];
		[tmpCurID release];
		tmpCurID = nil;
										   
		NSMutableString	*tmpCurPubDate =[[NSMutableString alloc] init];
		[self setCurrentPubDate: tmpCurPubDate];
		[tmpCurPubDate release];
		tmpCurPubDate = nil;
		
		NSMutableString	*tmpCurDescription =[[NSMutableString alloc] init];
		[self setCurrentDescription: tmpCurDescription];
		[tmpCurDescription release];
		tmpCurDescription = nil;
								
		NSMutableString	*tmpCurImgLink =[[NSMutableString alloc] init];								   
		[self setCurrentImgLink:tmpCurImgLink];
		[tmpCurImgLink release];
		tmpCurImgLink = nil;
		
	}
	
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
	//NSLog(@"ended element: %@", elementName);
	if ([elementName isEqualToString:@"item"]) {
		
		[item setObject:currentTitle forKey:@"title"];
		[item setObject:currentCategoryId forKey:@"category"];
		
		//NSString *tmpCurrentId = [NSString stringWithFormat:@"%@",currentId];
		
		//tmpCurrentId = [tmpCurrentId stringByReplacingOccurrencesOfString:@" " withString:@""];
		//tmpCurrentId = [tmpCurrentId stringByReplacingOccurrencesOfString:@"\n" withString:@""];
		//tmpCurrentId = [tmpCurrentId stringByReplacingOccurrencesOfString:@"	" withString:@""];
		
		//currentId = [NSMutableString stringWithString:tmpCurrentId];
		
		[item setObject:currentId forKey:@"id"];
		
		[item setObject:currentPubDate forKey:@"pubDate"];
		[item setObject:currentDescription forKey:@"description"];
		[item setObject:currentImgLink forKey:@"image_link"];
		// Trim white space
		//currentImgLink = [currentImgLink stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
		
		//NSURL *imageUrl = [NSURL URLWithString: currentImgLink];
		
		//NSData *nsDataImage = [NSData dataWithContentsOfURL: imageUrl];
		
		//UIImage *itemImage = [[UIImage imageWithData: [NSData dataWithContentsOfURL: imageUrl]] retain];
		//if(nsDataImage != nil){
			//UIImage *img = [UIImage imageNamed:@"some.png"];
			
			//NSData *nsDataImage = UIImageJPEGRepresentation(itemImage, 1.0);
			
		//	[item setObject:nsDataImage forKey:@"itemImageData"];
		//}
		NSLog(@"Retain count before adding  %d", [stories retainCount]);
		id newItem = [item copy];
		[stories addObject:newItem];
		[newItem release];
		newItem = nil;
		NSLog(@"Retain count after adding  %d", [stories retainCount]);
		//[stories addObject:[item copy]];
		
		//NSLog(@"adding story: %@", currentTitle);
		//NSLog(@"Total :%d", [stories count]);
		//NSLog(@"Total End");
	}
	
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
	//NSLog(@"found characters: %@ %@", currentElement, string);
	
	NSString *tmpString = [[NSString alloc] initWithString:string];
	
	@try {
		
		// save the characters for the current item...
		if ([currentElement isEqualToString:@"title"]) {
			[currentTitle appendString:tmpString];
		} else if ([currentElement isEqualToString:@"category"]) {
			[currentCategoryId appendString:tmpString];
		} else if ([currentElement isEqualToString:@"id"]) {
			[currentId appendString:tmpString];
		} else if ([currentElement isEqualToString:@"pubDate"]) {
			[currentPubDate appendString:tmpString];
		} else if ([currentElement isEqualToString:@"description"]) {
			[currentDescription appendString:tmpString];
		} else if ([currentElement isEqualToString:@"imgicon"]) {
			[currentImgLink appendString:tmpString];
		}
		
		[tmpString release];
		tmpString = nil;
	}
	@catch (NSException * e) {
		
		if(tmpString != nil){
			[tmpString release];
			tmpString = nil;
		}
		
	}
	
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
	NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
	NSLog(@"Retain count parserDidEndDocument: %d", [stories retainCount]);
	NSLog(@"%d", [stories count]);
	[prefs setObject:stories forKey:@"com.mycompany.newsRanklist"];
	[prefs synchronize];
	
}


- (void)dealloc {
	if(currentElement != nil){
		NSLog(@"1");
		[currentElement release];
		currentElement =nil;
	}
	if( rssParser!=nil){
		NSLog(@"2");
		[rssParser release];
		rssParser =nil;
	}
	if( stories!=nil){
		NSLog(@"3");
		NSLog(@"Retain count %d", [stories retainCount]);
		[stories release];
		stories =nil;
		NSLog(@"Retain count %d", [stories retainCount]);
	}
	if( item!=nil){
		NSLog(@"4");
		[item release];
		item = nil;
	}
	if( currentTitle!=nil){
		[currentTitle release];
		currentTitle = nil;
	}
	if( currentId!=nil){
		[currentId release];
		currentId = nil;
	}
	if( currentCategoryId!=nil){
		[currentCategoryId release];
		currentCategoryId = nil;
	}
	if( currentDescription!=nil){
		[currentDescription release];
		currentDescription = nil;
	}
	if( currentImgLink!=nil){
		[currentImgLink release];
		currentImgLink = nil;
	}
	
	
	[super dealloc];
}

@end

I found in leak details that this line create leak 100%(XCode display that)
Code:
NSMutableString	*tmpCurPubDate =[[NSMutableString alloc] init];

Can u help me? I have already waste 2 days in this issue.

Please please.....

Thanks in Advance.

-- Amin
mramin05 is offline   Reply With Quote
Old 11-16-2010, 07:33 AM   #6 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

That line alone is not a leak. You have misinterpreted the result.

All retain and copy properties should be released in dealloc. You are not releasing that one, which could explain that leak.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 11-16-2010, 09:44 PM   #7 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 49
mramin05 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
All retain and copy properties should be released in dealloc. You are not releasing that one, which could explain that leak.
Thank a lot. I have changed according to your advice now the leak is in GeneralBlock.

Here is my code

Code:
#import <Foundation/Foundation.h>


@interface PostXMLReader : NSObject < NSXMLParserDelegate> {
	
	NSXMLParser * rssParser;
	NSMutableArray * stories;
	NSMutableDictionary * item;
	NSString * currentElement;
	NSMutableString * currentTitle, * currentCategoryId, * currentId, * currentPubDate, *currentDescription , *currentImgLink;
	NSNumber *pageNumber;
}

@property(nonatomic,retain) NSMutableString * currentTitle;
@property(nonatomic,retain) NSMutableString * currentCategoryId;
@property(nonatomic,retain) NSMutableString * currentId;
@property(nonatomic,retain) NSMutableString * currentPubDate;
@property(nonatomic,retain) NSMutableString * currentDescription;
@property(nonatomic,retain) NSMutableString * currentImgLink;
@property(nonatomic,retain) NSNumber *pageNumber;

@property(nonatomic,retain) NSXMLParser * rssParser;
@property(nonatomic,retain) NSMutableArray * stories;
@property(nonatomic,retain) NSMutableDictionary * item;
@property(nonatomic,retain) NSString * currentElement;


- (void)loadNewsRankData:(int)pageNumberParam;


@end
And IMplementation:
Code:
#import "PostXMLReader.h"


@implementation PostXMLReader


@synthesize currentTitle, currentCategoryId, currentId, currentPubDate, currentDescription , currentImgLink;
@synthesize pageNumber;
@synthesize rssParser;
@synthesize stories;
@synthesize item;
@synthesize currentElement;

- (void)loadNewsRankData:(int)pageNumberParam {
	//NSLog(@"Top loader Loaded");
	//self.pageNumber= [NSNumber numberWithInt: pageNumber];
	
	NSNumber *tmpPageNum = [[NSNumber alloc] initWithInt:pageNumberParam];
	[self setPageNumber: tmpPageNum];
	[tmpPageNum release];
	tmpPageNum = nil;
	
	// Added parser code
	
	//if ([stories count] == 0) {
	NSString * path = [NSString stringWithFormat:@"http://myurl/rssnewsrank.php?page=%d",pageNumber];
	//NSLog(@"Path %@", path);
	[self parseXMLFileAtURL:path];
	
	if(path !=nil){
		path = nil;
	}
	//}
	
}

- (NSMutableArray*)getXMLData{
	
	return stories;
}


- (void)parseXMLFileAtURL:(NSString *)URL
{	
	NSLog(@"parseXMLFileAtURL function at topdatareader loaded");
	
	@try {
		
		NSLog(@"Retain count before %d", [stories retainCount]);
		
		NSMutableArray *tmpStories = [[NSMutableArray alloc] init];
		
		[self setStories: tmpStories ];
		[tmpStories release];
		tmpStories = nil;
		
		NSLog(@"Retain count after %d", [stories retainCount]);
		//stories = [[NSMutableArray alloc] init];
		
		//you must then convert the path to a proper NSURL or it won't work
		NSURL *xmlURL = [NSURL URLWithString:URL];
		
		NSXMLParser *tmpRssParser =[[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
		
		[self setRssParser:tmpRssParser ];
		[tmpRssParser release];
		tmpRssParser = nil;
		xmlURL = nil;
		URL = nil;
		// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
		[rssParser setDelegate:self];
		
		// Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
		[rssParser setShouldProcessNamespaces:NO];
		[rssParser setShouldReportNamespacePrefixes:NO];
		[rssParser setShouldResolveExternalEntities:NO];
		
		[rssParser parse];
	}
	@catch (NSException * e) {
		
		NSLog(@"Parsing error ");
	}
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
	NSLog(@"Parse Error ");
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{			
    //NSLog(@"found this element: %@", elementName);
	NSString *tmpCurElement = [[NSString alloc] initWithString:elementName];
	[self setCurrentElement: tmpCurElement];
	[tmpCurElement release];
	tmpCurElement = nil;						   
							   
	if ([elementName isEqualToString:@"item"]) {
		// clear out our story item caches...
		NSMutableDictionary *tmpItem = [[NSMutableDictionary alloc] init];
		[self setItem: tmpItem];
		[tmpItem release];
		tmpItem = nil;
		
		NSMutableString *tmpCurrentTitle = [[NSMutableString alloc] init];
		[self setCurrentTitle:tmpCurrentTitle ];
		[tmpCurrentTitle release];
		tmpCurrentTitle = nil;
		
		NSMutableString *tmpCurCatID = [[NSMutableString alloc] init];
		[self setCurrentCategoryId: tmpCurCatID];
		[tmpCurCatID release];
		tmpCurCatID = nil;
							
		NSMutableString *tmpCurID = [[NSMutableString alloc] init];
		[self setCurrentId:tmpCurID];
		[tmpCurID release];
		tmpCurID = nil;
										   
		NSMutableString	*tmpCurPubDate =[[NSMutableString alloc] init];
		[self setCurrentPubDate: tmpCurPubDate];
		[tmpCurPubDate release];
		tmpCurPubDate = nil;
		
		NSMutableString	*tmpCurDescription =[[NSMutableString alloc] init];
		[self setCurrentDescription: tmpCurDescription];
		[tmpCurDescription release];
		tmpCurDescription = nil;
								
		NSMutableString	*tmpCurImgLink =[[NSMutableString alloc] init];								   
		[self setCurrentImgLink:tmpCurImgLink];
		[tmpCurImgLink release];
		tmpCurImgLink = nil;
		
	}
	
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
	//NSLog(@"ended element: %@", elementName);
	if ([elementName isEqualToString:@"item"]) {
		
		[item setObject:currentTitle forKey:@"title"];
		[item setObject:currentCategoryId forKey:@"category"];
		
		
		[item setObject:currentId forKey:@"id"];
		
		[item setObject:currentPubDate forKey:@"pubDate"];
		[item setObject:currentDescription forKey:@"description"];
		[item setObject:currentImgLink forKey:@"image_link"];
		
		NSLog(@"Retain count before adding  %d", [stories retainCount]);
		id newItem = [item copy];
		[stories addObject:newItem];
		[newItem release];
		newItem = nil;
		NSLog(@"Retain count after adding  %d", [stories retainCount]);
		
	}
	
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
	//NSLog(@"found characters: %@ %@", currentElement, string);
	
	NSString *tmpString = [[NSString alloc] initWithString:string];
	
	@try {
		
		// save the characters for the current item...
		if ([currentElement isEqualToString:@"title"]) {
			[currentTitle appendString:tmpString];
		} else if ([currentElement isEqualToString:@"category"]) {
			[currentCategoryId appendString:tmpString];
		} else if ([currentElement isEqualToString:@"id"]) {
			[currentId appendString:tmpString];
		} else if ([currentElement isEqualToString:@"pubDate"]) {
			[currentPubDate appendString:tmpString];
		} else if ([currentElement isEqualToString:@"description"]) {
			[currentDescription appendString:tmpString];
		} else if ([currentElement isEqualToString:@"imgicon"]) {
			[currentImgLink appendString:tmpString];
		}
		
		[tmpString release];
		tmpString = nil;
	}
	@catch (NSException * e) {
		
		if(tmpString != nil){
			[tmpString release];
			tmpString = nil;
		}
		
	}
	
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
	NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
	NSLog(@"Retain count parserDidEndDocument: %d", [stories retainCount]);
	NSLog(@"%d", [stories count]);
	[prefs setObject:stories forKey:@"com.mycompany.newsRanklist"];
	[prefs synchronize];
	
}


- (void)dealloc {
	if(currentElement != nil){
		NSLog(@"1");
		[currentElement release];
		currentElement =nil;
	}
	if( rssParser!=nil){
		NSLog(@"2");
		[rssParser release];
		rssParser =nil;
	}
	if( stories!=nil){
		NSLog(@"3");
		NSLog(@"Retain count %d", [stories retainCount]);
		[stories release];
		stories =nil;
		NSLog(@"Retain count %d", [stories retainCount]);
	}
	if( item!=nil){
		NSLog(@"4");
		[item release];
		item = nil;
	}
	if( currentTitle!=nil){
		[currentTitle release];
		currentTitle = nil;
	}
	if( currentId!=nil){
		[currentId release];
		currentId = nil;
	}
	if( currentCategoryId!=nil){
		[currentCategoryId release];
		currentCategoryId = nil;
	}
	if( currentDescription!=nil){
		[currentDescription release];
		currentDescription = nil;
	}
	if( currentImgLink!=nil){
		[currentImgLink release];
		currentImgLink = nil;
	}
	
	if( currentPubDate!=nil){
		[currentPubDate release];
		currentPubDate = nil;
	}
	
	if( pageNumber!=nil){
		[pageNumber release];
		pageNumber = nil;
	}
	
	
	
	
	[super dealloc];
}

@end
Here is the last memory leak screenshot:
General Leak | Flickr - Photo Sharing!

Here is the strange point:
Code:
NSLog(@"Retain count before %d", [stories retainCount]);
		
		NSMutableArray *tmpStories = [[NSMutableArray alloc] init];
		
		[self setStories: tmpStories ];
		[tmpStories release];
		tmpStories = nil;
		
		NSLog(@"Retain count after %d", [stories retainCount]);
First retain count is o but if i don't set tmpStories to nil then the next retain count is 2.
and in dealloc if i don't set stories =nil; then the retain count of stories is still 1 even after release.

It is killing me.

Please....
mramin05 is offline   Reply With Quote
Old 11-16-2010, 09:47 PM   #8 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

I wouldn't worry about that leak.

You cannot rely on retainCount.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 11-16-2010, 11:30 PM   #9 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 49
mramin05 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
I wouldn't worry about that leak.

You cannot rely on retainCount.
Thanks a lot. I am going to the next step.

Thanks again for your time and help.
mramin05 is offline   Reply With Quote
Old 11-17-2010, 03:42 AM   #10 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 49
mramin05 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
I wouldn't worry about that leak.

You cannot rely on retainCount.
Hi,
Another problem related that. Please have a look on the code.
Code:
                        PostXMLParser *postXMLParser = [PostXMLParser new];
			[postXMLParser loadPostData];
			[self populateItemList:[postXMLParser getXMLData]];
                         // populateItemList is a private function to manupulate that data. That is seems to be ok.
HOw to release this "postXMLParser" object? as I can not alloc, init this PostXMLParser.

will it be autoreleased?

Thanks in advance.
-- Amin
mramin05 is offline   Reply With Quote
Reply

Bookmarks

Tags
leak, memory leak, nsxmlparser, rss parser

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 358
7 members and 351 guests
apatsufas, Kirkout, lzwasyc, MarkC, Sami Gh, SamorodovAlex, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:40 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0