iPhone NSMutableArray release problem - iphone

I have two controllers, SyncController and XMLController. SyncController sends some parameters to XMLController, which connects to an API and wraps the result as objects within an NSMutableArray, and returns the array back to the SyncController.
Some code:
SyncController.h
-(void)urlHandler:(NSArray *)urlHandler listObjectsFinishedLoading:(NSMutableArray *)resultData;
SyncController.m
- (void)urlHandler:(NSArray *)urlHandler listObjectsFinishedLoading:(NSMutableArray *)resultData;
{
NSMutableArray *receivedObjects = [[NSMutableArray alloc] init];
[receivedObjects addObjectsFromArray:resultData];
for (Object *o in receivedObjects) {
//DO SOME STUFF
}
[receivedObjects release];
}
XMLController.h
#interface XMLController : NSObject {
NSMutableArray *objects;
}
#property (nonatomic, retain) NSMutableArray *objects;
XMLController.m
-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
objects = [[NSMutableArray alloc] init];
if ([delegate respondsToSelector:#selector(urlHandler:listObjectsFinishedLoading:)]) {
[delegate urlHandler:self listObjectsFinishedLoading:objects];
}
//[objects release];
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
// Initialize an Object
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
// Put some properties unto Object
// Ad Object to the objects array
// release Object
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
//[objects release];
}
- (void)dealloc {
//[objects release];
[super dealloc];
}
My question is, how do I properly release the objects array? If I don't release it, the code works properly (the actions from //DO SOME STUFF are executed) but it obviously leaks. If I release it, wherever I do it (see the commented //[objects release]; in three places) the app crashes.
Any suggestions? Thanks.

Try to
if ([delegate respondsToSelector:#selector(urlHandler:listObjectsFinishedLoading:)]) {
[delegate urlHandler:self listObjectsFinishedLoading:[objects autorelease]];
}

Maybe you're deallocating the Object at - (void)parserDidEndDocument:(NSXMLParser *)parser and again in - (void)dealloc. Try to set the object to nil (≠ NULL) when you release it, so you know it won't get released again.
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
[objects release], objects = nil;
}

-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
NSMutableArray *temp=[[NSMutableArray alloc] init];
self.objects=temp;
[temp release];
if ([delegate respondsToSelector:#selector(urlHandler:listObjectsFinishedLoading:)]) {
[delegate urlHandler:self listObjectsFinishedLoading:objects];
}
//[objects release];
}
and then
- (void)dealloc {
//[objects release];
[self.objects release];
[super dealloc];
}
use in this way it definitely works.

You are defining objects as a retained property, and then addressing the instance variable directly. If you are using synthesize to generate the getters and setters, then let them do the memory management for you.
self.objects = [[[NSMutableArray alloc] init] autorelease];
and
self.objects = nil;
rather than manually doing the releases.

thanks for your help. Still doesn't work, but I get the feeling that teh problem might be in the Object class. Releasing the array calls release on every object right?
Object.h
#interface Object : NSObject {
NSString *name;
}
#property (nonatomic, retain) NSString *name;
Object.m
-(void) dealloc{
[self.name release];
[super dealloc];
}
If I comment the [self.name release]; line, then the array in question CAN be released, it doesn't crash, and with no leaks. But then the app leaks NSStrings in other places where the Object name property is used.
It might be something very trivial that I miss.
Thanks.

Related

accessing array from application delegate

i have declared 1 array in application delegate
#property(strong,nonatomic)NSMutableArray *books;
i have added objects to this array in XMLParser.m
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:#"Books"])
return;
NSLog(#"i m in didEndElement");
if([elementName isEqualToString:#"Book"]) {
[appDelegate.books addObject:aBook]; //here i have added objects to array
[aBook release];
aBook = nil;
NSLog(#"books=%#",appDelegate.books);
}
else
[aBook setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
i have declared this array in XMLParser.h like this
#interface XMLParser : NSObject {
NSMutableString *currentElementValue;
AppDelegate *appDelegate;
Book *aBook;
}
now i have to access this array in BooksDetailViewController.m , how do i access this array.
in short how i can access array from application delegate into BooksDetailViewController
First of all you should have to alloc and init your array, then You can add objects in it.
You should have to create AppDelegate instance in your BooksDetailViewController like this,
in .h file
AppDelegate *appDelegate;
and in .m file
appDelegate = [[UIApplication sharedApplication]delegate];
Now you can access your array like this,
appDelegate.books

Memory Leak & App Crash when going back to Album List

I'm using three20 to create image viewer. First i'm creating list of albums from the sql db and when user selects any album, its url string is passed to the this code that creates thums of available pics on network using XML Parser. everything works fine but when user goes back to the album list and selects another album. app crashes with 'Thread 1: Program received singal: "EXC+BAD_ACCESS" in main.m. plus XCode Product Analyze gives potential memory leak where i'm creating photoSource in the viewDidLoad. Here is the code
#import "AlbumController.h"
#import "PhotoSource.h"
#import "Photo.h"
#import "AlbumInfo.h"
#import "AlbumDatabase.h"
#implementation AlbumController
#synthesize albumName;
#synthesize urlAddress;
#synthesize images;
- (void)viewDidLoad
{
[super viewDidLoad];
// NSLog(#"%#", self.urlAddress);
[self createPhotos]; // method to set up the photos array
self.photoSource = [[PhotoSource alloc]
initWithType:PhotoSourceNormal
title:self.albumName
photos:images
photos2:nil];
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// release and set to nil
}
-(void)createPhotos
{
if ([stories count] == 0)
{
NSString *path = self.urlAddress;
[self parseXMLFileAtURL:path];
}
images = [NSMutableArray arrayWithCapacity:[stories count]]; // needs to be mutable
for (int i = 0; i < [stories count]; i++)
{
NSString *img = [[stories objectAtIndex:i] objectForKey:#"image"];
img = [img stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
//NSString * caption = [[stories objectAtIndex:i] objectForKey:#"caption"];
//caption = [caption stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[images addObject:[[[Photo alloc] initWithURL:img smallURL:img size:CGSizeMake(320, 212)] autorelease]];
}
}
#pragma mark -
#pragma mark XML Parser Implementation
- (void)parserDidStartDocument:(NSXMLParser *)parser{
//NSLog(#"found file and started parsing");
}
- (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];
// here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
// this may be necessary only for the toolchain
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 {
NSString * errorString = [NSString stringWithFormat:#"Unfortunately it is not possible to load Pictures. Please check Internet Connection. (Error code %i )", [parseError code]];
//NSLog(#"error parsing XML: %#", errorString);
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:#"Failed to load the feed." message:errorString delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
//NSLog(#"found this element: %#", elementName);
currentElement = [elementName copy];
if ([elementName isEqualToString:#"item"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentCaption = [[NSMutableString alloc] init];
//currentThumbnail = [[NSMutableString alloc] init];
currentImage = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(#"ended element: %#", elementName);
if ([elementName isEqualToString:#"item"]) {
// save values to an item, then store that item into the array...
//[item setObject:currentThumbnail forKey:#"thumbnail"];
//[item setObject:currentCaption forKey:#"caption"];
[item setObject:currentImage forKey:#"image"];
[stories addObject:[[item copy] autorelease]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
// save the characters for the current item...
if ([currentElement isEqualToString:#"thumbnail"]) {
//[currentThumbnail appendString:string];
}// else if ([currentElement isEqualToString:#"caption"]) {
//[currentCaption appendString:string];
//}
else if ([currentElement isEqualToString:#"image"]) {
[currentImage appendString:string];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
NSLog(#"all done!");
NSLog(#"stories array has %d items", [stories count]);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if (toInterfaceOrientation == UIInterfaceOrientationPortrait ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
return YES;
}
else
{
return NO;
}
}
#pragma mark -
#pragma mark Memory Management
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[currentElement release];
[rssParser release];
[stories release];
[item release];
[currentCaption release];
//[currentThumbnail release];
[currentImage release];
[images release];
[stories release];
[super dealloc];
}
#end
and here is the didSelectRowAtIndexPath thats pushing this view
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AlbumInfo *info = [_albumInfos objectAtIndex:indexPath.row];
AlbumController *albumController = [[AlbumController alloc] init];
albumController.urlAddress = info.address;
albumController.albumName = info.name;
[self.navigationController pushViewController:albumController animated:YES];
[albumController release];
}
Here is the code for AlbumController.h
#import <Foundation/Foundation.h>
#import "Three20/Three20.h"
#interface AlbumController : TTThumbsViewController <NSXMLParserDelegate>
{
NSString *albumName;
NSString *urlAddress;
// images
NSMutableArray *images;
// parser
NSXMLParser * rssParser;
NSMutableArray * stories;
NSMutableDictionary * item;
NSString * currentElement;
NSMutableString * currentImage;
NSMutableString * currentCaption;
}
#property (nonatomic, strong) NSString *albumName;
#property (nonatomic, strong) NSString *urlAddress;
#property (nonatomic, retain) NSMutableArray *images;
- (void)createPhotos;
- (void)parseXMLFileAtURL:(NSString *)URL;
#end
used this tutorial http://www.raywenderlich.com/1430/how-to-use-the-three20-photo-viewer
Need help solving this memory leak and need to know why its crashing.
Thanks
Simple. In Xcode 4.0+, Just click-hold on the Run icon, and press Profile. It'll open up Instruments, and you'll want Zombies. Then navigate your app to where the crash happened before, and this time, it'll show up in Instruments with the caller, and all the information about it.
The memory leak in viewDidLoad is caused by the following line:
self.photoSource = [[PhotoSource alloc]
initWithType:PhotoSourceNormal
title:self.albumName
photos:images
photos2:nil];
[PhotoSource alloc] returns an object you own (with a retain count of +1).
initWithType:title:photos:photos2: does not change the retain count.
So viewDidLoad is left with an object it owns, but no pointer to it. To balance the alloc you should send an autorelease message:
self.photoSource = [[[PhotoSource alloc]
initWithType:PhotoSourceNormal
title:self.albumName
photos:images
photos2:nil] autorelease];

Desperate for NSXMLParser Guidance

I previously asked this question XMLParser Advice.
However I am still unable to get it to function properly....
So I guess I will start from scratch:
Located at a certain URL is an XML Tree that looks like this
<result>
//stuff that I dont need
<title>
//thing that I do need
</title>
//stuff that I dont need
<body>
//thing that I do need
</body>
<result>
How the heck do I go about parsing that?
The (useless) code I have so far can be found in the link at the top of this question.
Thank you for your time.
Write a simple class, which will be the parser's delegate.
#interface YourObject : NSObject <NSXMLParserDelegate> {
NSString *title, *body; // object attributes
NSXMLParser *parser; // will parse XML
NSMutableString *strData; // will contains string data being parsed
}
#property(readwrite, copy) NSString *title, body;
// will be used to set your object attributes
-(void)fetchValuesAtURL:(NSString *)url;
#end
The fetchValuesAtURL: method will initiate the parse operation.
#implementation YourObject
#synthesize title, body;
-(id)init {
self = [super init];
if(self) {
title = #"";
body = #"";
parser = nil;
strData = [[NSMutableString alloc] initWithCapacity:10];
}
return self;
}
-(void)fetchValuesAtURL:(NSString *)url {
if(parser) {
[parser release];
}
NSURL *xmlURL = [NSURL URLWithString:url];
parser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[parser setDelegate:self];
[parser parse];
}
-(void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict {
// element is about to be parsed, clean the mutable string
[strData setString:#""];
}
// the probably missing method
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
// content (or part of) has been found, append that to the current string
[strData appendString:string];
}
-(void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
// element has been parsed, test the element name
// and store strData accordingly
if([elementName isEqualToString:#"title"]) {
self.title = strData;
}
else { // or else if, here you got two elements to parse
self.body = strData;
}
}
-(void)dealloc {
[title release];
[body release];
[strData release];
if(parser) {
[parser release];
}
[super dealloc];
}
#end
Then :
YourObject *obj = [[YourObject alloc] init];
[obj fetchValuesAtURL:#"http://www.site.com/xml/url"];
NSXMLParser's delegate is able to do many more things, as described in Event-Driven XML Programming Guide from Apple.
For complete reference on delegate methods, see NSXMLParserDelegate Protocol Reference.

iPhone: memory leak on autoreleased object?

I am using the XMLParser class, which contains an array with XMLElement objects. The XMLElement is being allocated using the autorelease operation. However, for some reason I'm getting a memory leak (using instruments) on this line:
self.currentElement = [[[XMLElement alloc] init] autorelease];
I'm also releasing the XMLParser object in the caller class. I've highlighted the "problematic" lines in the XMLParser source code below as comment.
I really tried everything to fix it, but unfortunately I do not understand why this is happening at all. Any help is appreciated!
Thank you very much!
// --- XMLElement
#import <Foundation/Foundation.h>
#interface XMLElement : NSObject {
NSDictionary *attributes;
NSMutableArray *children;
NSString *textValue;
NSString *tagName;
XMLElement *parentElement;
}
-(id) init;
-(void) addChild:(XMLElement*) child;
#property(nonatomic, retain) NSDictionary *attributes;
#property(nonatomic, retain) NSMutableArray *children;
#property(nonatomic, retain) NSString *textValue;
#property(nonatomic, retain) NSString *tagName;
#property(nonatomic, retain) XMLElement *parentElement;
#end
#import "XMLElement.h"
#implementation XMLElement
#synthesize attributes, children, textValue, parentElement, tagName;
-(id)init {
if(self = [super init]) {
children = [[NSMutableArray alloc] init];
}
return self;
}
-(void) addChild:(XMLElement*) child{
[self.children addObject:child];
}
- (void)dealloc {
[attributes release];
[children release];
[textValue release];
[tagName release];
[parentElement release];
[super dealloc];
}
#end
// --- XMLParser
#import <Foundation/Foundation.h>
#import "XMLElement.h"
#interface XMLParser : NSObject<NSXMLParserDelegate> {
XMLElement *currentElement;
XMLElement *currentParentElement;
NSMutableString *currentElementValue;
}
- (BOOL)parseData: (NSData*) dataToParse;
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict;
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
#property (nonatomic, retain) XMLElement *currentParentElement;
#property (nonatomic, retain) XMLElement *currentElement;
#property (nonatomic, retain) NSMutableString *currentElementValue;
#end
#import "XMLParser.h"
#implementation XMLParser
#synthesize currentElementValue, currentElement, currentParentElement;
- (BOOL)parseData: (NSData*) dataToParse {
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:dataToParse];
[parser setDelegate:self];
BOOL success = [parser parse];
[parser release];
return success;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict{
if(currentElement){
self.currentParentElement = currentElement;
}
// ------------------------------------------------------------
// Instruments is marking this line as source of the leak with 90%
self.currentElement = [[[XMLElement alloc] init] autorelease];
// --------
currentElement.tagName = elementName;
currentElement.attributes = attributeDict;
currentElement.parentElement = self.currentParentElement;
if(self.currentParentElement){
[self.currentParentElement addChild:currentElement]; // and this one with 10%
}
self.currentElementValue = nil;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if(!currentElement) {
return;
}
if(currentElementValue == nil)
self.currentElementValue = [NSMutableString stringWithString:string];
else
[currentElementValue appendString:string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if(currentElement == nil){
if( currentParentElement.parentElement){
self.currentParentElement = currentParentElement.parentElement;
}
}else{
currentElement.textValue = currentElementValue;
[currentElementValue release];
currentElementValue = nil;
self.currentParentElement = currentElement.parentElement;
currentElement = nil;
}
}
- (void)dealloc {
[currentParentElement release];
[currentElement release];
[super dealloc];
}
#end
You must release currentElement in your dealloc method in XMLParser.
It's created as autorelease but then assigned to a retain property, so you are actually retaining it once (which is good).
UPDATE: you should do self.currentElement = nil; when you are done with it, not currentElement = nil;.
This:
self.currentElement = [[[XMLElement alloc] init] autorelease];
retains currentElement (because the property is declared with retain).
Later, in parser:didEndElement:namespaceURI:qualifiedName:, you do this:
currentElement = nil;
which causes the leak because you are not releasing currentElement.
From what I see on this page, this is a documented apple bug. I have experienced the same problem in some of my apps...
Is your XMLParser being run on a background thread? If so, you need to create an NSAutoReleasePool for that thread in order for your [[[XMLElement alloc] init] autorelease] call to work.
HI guys.
the reason is just one simple thing,
instead of
#property(nonatomic, retain) XMLElement *parentElement;
must be
#property(nonatomic, assign) XMLElement *parentElement;
also you need delete [parentElement release] from dealloc
reason is that you are creating cyclic referances. parent to child and chils to parent
and when you are realesing parser object the elements still ocurrs in memory with pointer xount > 0

UITableView crashes when trying to scroll

I have a problem with data in UITableView. I have UIViewController, that contains UITableView outlet and few more things I am using and ... It works :) ... it works lovely, but ...
I've created an RSS reader class that is using delegates to deploy the data to the table ... and once again, If I'll just create dummy data in the main controller everything works!
problem is with this line: rss.delegate = self;
Preview looks a little bit broken than here are those RSS reader files on Google code:
(Link to the header file on GoogleCode)
(Link to the implementation file on Google code)
viewDidLoad function of my controller:
IGDataRss20 *rss = [[[IGDataRss20 alloc] init] autorelease];
rss.delegate = self;
[rss initWithContentsOfUrl:#"http://rss.cnn.com/rss/cnn_topstories.rss"];
and my delegate methods:
- (void)parsingEnded:(NSArray *)result {
super.data = [[NSMutableArray alloc] initWithArray:result];
NSLog(#"My Items: %d", [super.data count]);
[super.table reloadData];
NSLog(#"Parsing ended");
}
- (void)parsingError:(NSString *)message {
NSLog(#"MyMessage: %#", message);
}
- (void)parsingStarted:(NSXMLParser *)parser {
NSLog(#"Parsing started");
}
Just to clarify, NSLog(#"Parsing ended"); is being executed and I have 10 items in the array.
Hope someone will be able to help me as I am becoming to be quite desperate, and I thought I am not already such a greenhorn :)
Thanks,
Ondrej
Full copy of my header file (table controller)
the WGTempTableController class is UIViewController with table outlet, data array etc ...
//
// CRFeedController.h
// czReader
//
// Created by Ondrej Rafaj on 5.4.10.
// Copyright 2010 Home. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "WGTempTableController.h"
#import <IGDataRss20.h>
#interface CRFeedController : WGTempTableController <IGDataRss20Delegate> {
//NSString *startUrl;
}
#end
Full copy of my implementation file (table controller)
All other functions like numberOfSectionsInTableView or numberOfRowsInSection are in that WGTempTableController
//
// CRFeedController.m
// czReader
//
// Created by Ondrej Rafaj on 5.4.10.
// Copyright 2010 Home. All rights reserved.
//
#import "CRFeedController.h"
#import "WGTempCell.h"
#implementation CRFeedController
- (void)viewDidLoad {
[super viewDidLoad];
IGDataRss20 *rss = [[[IGDataRss20 alloc] init] autorelease];
rss.delegate = self;
[rss initWithContentsOfUrl:#"http://rss.cnn.com/rss/cnn_topstories.rss"];
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)parsingEnded:(NSArray *)result {
super.data = [[NSMutableArray alloc] initWithArray:result];
NSLog(#"My Items: %d", [super.data count]);
[super.table reloadData];
NSLog(#"Parsing ended");
}
- (void)parsingError:(NSString *)message {
NSLog(#"MyMessage: %#", message);
}
- (void)parsingStarted:(NSXMLParser *)parser {
NSLog(#"Parsing started");
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
#pragma mark Table view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MyCell";
WGTempCell *cell = (WGTempCell *) [table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CRFeedCell" owner:nil options:nil];
for(id currentObject in topLevelObjects) {
if([currentObject isKindOfClass:[WGTempCell class]]) {
cell = (WGTempCell *) currentObject;
break;
}
}
}
NSDictionary *d = [super.data objectAtIndex:indexPath.row];
[[cell cellTitle] setText:[d objectForKey:#"title"]];
return cell;
}
- (void)dealloc {
[super dealloc];
}
#end
Full copy of my header file (rss reader)
//
// IGDataRss20.h
// IGFrameworkProject
//
// Created by Ondrej Rafaj on 4.4.10.
// Copyright 2010 Home. All rights reserved.
//
#import <Foundation/Foundation.h>
#class IGDataRss20;
#protocol IGDataRss20Delegate <NSObject>
#optional
- (void)parsingStarted:(NSXMLParser *)parser;
- (void)parsingError:(NSString *)message;
- (void)parsingEnded:(NSArray *)result;
#end
#interface IGDataRss20 : NSObject {
NSXMLParser *rssParser;
NSMutableArray *data;
NSMutableDictionary *currentItem;
NSString *currentElement;
id <IGDataRss20Delegate> delegate;
}
#property (nonatomic, retain) NSMutableArray *data;
#property (nonatomic, assign) id <IGDataRss20Delegate> delegate;
- (void)initWithContentsOfUrl:(NSString *)rssUrl;
- (void)initWithContentsOfData:(NSData *)inputData;
#end
Full copy of my implementation file (rss reader)
//
// IGDataRss20.m
// IGFrameworkProject
//
// Created by Ondrej Rafaj on 4.4.10.
// Copyright 2010 Home. All rights reserved.
//
#import "IGDataRss20.h"
#implementation IGDataRss20
#synthesize data, delegate;
- (void)initWithContentsOfUrl:(NSString *)rssUrl {
self.data = [[NSMutableArray alloc] init];
NSURL *xmlURL = [NSURL URLWithString:rssUrl];
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[rssParser setDelegate:self];
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
}
- (void)initWithContentsOfData:(NSData *)inputData {
self.data = [[NSMutableArray alloc] init];
rssParser = [[NSXMLParser alloc] initWithData:inputData];
[rssParser setDelegate:self];
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser {
[[self delegate] parsingStarted:parser];
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:#"Unable to parse RSS feed (Error code %i )", [parseError code]];
NSLog(#"Error parsing XML: %#", errorString);
if ([parseError code] == 31) NSLog(#"Error code 31 is usually caused by encoding problem.");
[[self delegate] parsingError:errorString];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
currentElement = [elementName copy];
if ([elementName isEqualToString:#"item"]) currentItem = [[NSMutableDictionary alloc] init];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"item"]) {
[data addObject:(NSDictionary *)[currentItem copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (![currentItem objectForKey:currentElement]) [currentItem setObject:[[[NSMutableString alloc] init] autorelease] forKey:currentElement];
[[currentItem objectForKey:currentElement] appendString:string];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
//NSLog(#"RSS array has %d items: %#", [data count], data);
[[self delegate] parsingEnded:(NSArray *)self.data];
}
- (void)dealloc {
[data, delegate release];
[super dealloc];
}
#end
Your subject says it crashes when you try to scroll. I don't know what this would have to do with your rss.delegate, so I'm just going to ignore that and focus on probable scrolling-related bugs here, which are usually in tableView:cellForRowAtIndexPath:.
Check your CRFeedCell.xib, view info on your WGTempCell object, and make sure its Identifier field matches your CellIdentifier in your code. ("MyCell")
Make sure you're not using that same CellIdentifier for some other UITableViewCell subclass elsewhere in your code.
What kind of crash is it? If it's EXC_BAD_ACCESS, double-click on your executable, go to Arguments, create an NSZombieEnabled environment variable, and set it to YES. (Uncheck it when you're done debugging to avoid leaking memory.) This will show you which object you were trying to access when the app crashed.
Set a breakpoint on the setText: call in tableView:cellForRowAtIndexPath:. Then at your gdb prompt, type po [d objectForKey:#"title"]. Make sure that object is really an NSString.
It looks to me, as if you initialize the NSMutableArray data twice: First in initWithContentsOfUrl: , then again in parsingEnded: . Maybe you should do a removeAllObjects in parsingEnded instead.