0

Help find out how to solve the problem using the memory in my application, may have another way to implement my task.

Have the following code:

Constants.h

#import <Foundation/Foundation.h>

extern NSString * const ITEM;
extern NSString * const TITLE;
extern NSString * const IMAGE;
extern NSString * const DESCRIPTION;
extern NSString * const TEXT;

@interface Constants : NSObject
{

}

@end

Constants.m

#import "Constants.h"

@implementation Constants

NSString * const ITEM           = @"item";
NSString * const TITLE          = @"title";
NSString * const IMAGE          = @"image";
NSString * const DESCRIPTION    = @"description";
NSString * const TEXT           = @"text";

 - (void)dealloc
{
    [super dealloc];
}

@end

myNews.h

#import <Foundation/Foundation.h>

@interface myNews : NSObject
{
    NSString *itemTitle;
    NSString *itemImageUrl;
    NSString *itemDescription;
    NSString *itemText;
}

@property (nonatomic, retain) NSString *itemTitle;
@property (nonatomic, retain) NSString *itemImageUrl;
@property (nonatomic, retain) NSString *itemDescription;
@property (nonatomic, retain) NSString *itemText;

@end

myNews.m

#import "myNews.h"

@implementation myNews

@synthesize itemTitle;
@synthesize itemImageUrl;
@synthesize itemDescription;
@synthesize itemText;

@end

XMLController.h

#import <Foundation/Foundation.h>

@class Constants;
@class myNews;

@interface XMLController : NSObject
{
    NSMutableString *currentNodeContent;
    NSMutableArray *newsArray;
    NSXMLParser *parser;
    myNews *currentNew;
}

@property (readonly, retain) NSMutableArray *newsArray;

-(id)loadXMLByURL:(NSString *)urlString;

@end

XMLController.m

#import "XMLController.h"
#import "Constants.h"
#import "myNews.h"

@implementation XMLController

@synthesize newsArray;

-(id)loadXMLByURL:(NSString *)urlString
{
    newsArray = [[NSMutableArray alloc] init];
    NSURL *url = [NSURL URLWithString: urlString];
    parser = [[NSXMLParser alloc] initWithContentsOfURL: url];
    [parser setDelegate:(id)self];
    [parser parse];
    return self;
}

-(void) parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString: ITEM])
    {
        currentNew = [myNews alloc];
        currentNodeContent = [[NSMutableArray alloc] init];
    }
}

-(void) parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{    
    if ([elementName isEqualToString: TITLE])
    {
        currentNew.itemTitle = currentNodeContent;
    }

    if ([elementName isEqualToString: IMAGE])
    {
        currentNew.itemImageUrl = currentNodeContent;
    }

    if ([elementName isEqualToString: DESCRIPTION])
    {
        currentNew.itemDescription = currentNodeContent;
    }

    if ([elementName isEqualToString: TEXT])
    {
        currentNew.itemText = currentNodeContent;
    }

    if ([elementName isEqualToString: ITEM])
    {
        [newsArray addObject:currentNew];
        [currentNew release];
        currentNew = nil;
        [currentNodeContent release];
        currentNodeContent = nil;
    }
}

-(void) parser:(NSXMLParser *)parser
foundCharacters:(NSString *)string
{
    currentNodeContent = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

@end

myAppDelegate.h

#import <UIKit/UIKit.h>
#import "MainViewController.h"
#import "DetailViewController.h"

@class MainViewController;
@class DetailViewController;

@interface myAppDelegate : NSObject <UIApplicationDelegate>
{
    UIWindow *window;
    UINavigationController *myNavigationController;    
    MainViewController *myMainViewController;
    DetailViewController *myDetailViewController;  
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *myNavigationController;
@property (nonatomic, retain) IBOutlet MainViewController *myMainViewController;
@property (nonatomic, retain) IBOutlet DetailViewController *myDetailViewController;

@end

myAppDelegate.m

#import "myAppDelegate.h"
#import "myNews.h"
#import "MainViewController.h"
#import "DetailViewController.h"

@implementation myAppDelegate

@synthesize window;
@synthesize myNavigationController;
@synthesize myMainViewController;
@synthesize myDetailViewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [window addSubview:myNavigationController.view];
    [window makeKeyAndVisible];

    return YES;
}

- (void)dealloc 
{
    [window release];
    [myNavigationController release];
    [myMainViewController release];
    [myDetailViewController release];
    [super dealloc];
}

@end

MainViewController.h

#import <UIKit/UIKit.h>
#import "XMLController.h"

@interface MainViewController : UIViewController
{
    XMLController *xmlcont;
UILabel *myTitleLabel;

// declared here some 10 objects

}

@property (nonatomic, retain) UILabel *myTitleLabel;

@end

MainViewController.m

#import "MainViewController.h"
#import "myNews.h"

@implementation MainViewController

@synthesize myTitleLabel;

- (void)completeRefresh
{       
    [myMainViewController viewDidLoad];
}

- (void)viewDidLoad
{    
    [super viewDidLoad];

UIButton *myRefreshButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 35.0f, 23.0f)];
[myRefreshButton setImage:[UIImage imageNamed:@"ButtonRefresh.png"] forState:UIControlStateNormal];
[myRefreshButton addTarget:self
                        action:@selector(completeRefresh)
                        forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *myRefreshBarButton = [[[UIBarButtonItem alloc] initWithCustomView:myRefreshButton] autorelease];
[self.navigationItem setRightBarButtonItem: myRefreshBarButton];
[myRefreshButton release];

    xmlcont = [[XMLController alloc] loadXMLByURL:@"http://link_to_XML_file.xml"];

    NSLog(@"array = %@", [xmlcont newsArray]);

    for (myNews *oneNew in [xmlcont newsArray]) {

                CGRect frameTitleLabel = CGRectMake(0.0f, 200.0f, 320.0f, 60.0f);
    myTitleLabel = [[UILabel alloc] initWithFrame:frameTitleLabel];
    myTitleLabel.backgroundColor = [UIColor blackColor];
    myTitleLabel.text = [myOneNew itemTitle];
    myTitleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size: 17.0];
    myTitleLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:myTitleLabel];
    [myTitleLabel release];

    }

}

- (void)viewDidUnload
{
self.myTitleLabel = nil;
    [super viewDidUnload];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc 
{
[myTitleLabel release];
    [super dealloc];
}

@end

During the test the application using the Instruments: Activity Monitor shows that when you click refresh and call 'completeRefresh' memory (Real Mem) is not cleared when reload viewDidLoad, and accumulates with every touch and calling 'completeRefresh'

Maybe someone has a possible solution?

9
  • Please also state why do you want to implement this completeRefresh thing. If you want to reload your view you can use [self.view setNeedsDisplay]; Jul 29, 2011 at 7:09
  • I have created in viewDidLoad ScrollView with subview of the array which is obtained by parsing the XML file. I need to reboot to re-viewDidLoad get an array with new data. I just have not found any other way how to do it. What is the method setNeedsDisplay and how to use it in my case? The problem in my case I have is the accumulation of memory. Everything else works fine.
    – scazy0rg
    Jul 29, 2011 at 7:18
  • in your refresh, erase your array and get new content in it and then call setNeedsDisplay on your scrollView. this method refreshes the view. Try using it and tell if it works Jul 29, 2011 at 7:24
  • btw its text data why dont you use tableview and call [tableview reloadData]; much simpler Jul 29, 2011 at 7:25
  • @xs2bush No, not only text data is also UIImageView in UIScrollView, tableview does not suit me, setNeedsDisplay does not work: (
    – scazy0rg
    Jul 29, 2011 at 8:00

1 Answer 1

1

To avoid leaking memory you need to release your retained ivars/properties in dealloc. For example:

myNews.m

- (void)dealloc {
    // Setting these properties to nil releases them because they're (nonatomic, retain)
    self.itemTitle = nil;
    self.itemImageUrl = nil;
    self.itemDescription = nil;
    self.itemText = nil;
    [super dealloc];
}

XMLController.m

- (void)dealloc {
    self.newsArray = nil;
    // These ivars aren't properties so you just have to call release on them
    [currentNodeContent release];
    [parser release];
    [currentNew release];
    [super dealloc];
}

etc. Do this for all your classes.

1
  • In my myNews.m I have (void)dealloc I just do not have copied this part of the code. In XMLController.m I do not [parser release]; in (void)dealloc, application fails. I am doing [parser release]; in loadXMLByURL
    – scazy0rg
    Jul 29, 2011 at 7:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.