删除警告和内存泄漏

时间:2012-05-24 12:54:24

标签: ios xcode memory-leaks warnings

我的项目中有一些警告(XCode 4.1,4.3.1),主要是在开源库中 enter image description here


Reachability.h:
+ (Reachability*) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress;

ASIAuthenticationDialog.m:

- (UIViewController *)presentingController
{
    if (!presentingController) {
        presentingController = [[ASIAutorotatingViewController alloc] initWithNibName:nil bundle:nil];

        // Attach to the window, but  don't interfere.
        UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
        [window addSubview:presentingController.view];
        [[presentingController view] setFrame:CGRectZero];
        [[presentingController view] setUserInteractionEnabled:NO];
    }

    return presentingController;
}

ASIDownloadCache:

- (void)setDefaultCachePolicy:(ASICachePolicy)cachePolicy
{
    [[self accessLock] lock];
    if (cachePolicy == ASIDefaultCachePolicy) {
        defaultCachePolicy = ASIReloadIfDifferentCachePolicy;
    }  else {
        defaultCachePolicy = cachePolicy;   
    }
    [[self accessLock] unlock];
}


 NSDate *expiryDate = [fetchDate addTimeInterval:maxAge]; - 不推荐使用addTimeInterval


ASIHTTPRequest.m:

- (void)setDelegate:(id)newDelegate
{
    [[self cancelledLock] lock];
    delegate = newDelegate;
    [[self cancelledLock] unlock];
}


ASINetworkQueue.m:

- (void)setUploadProgressDelegate:(id)newDelegate
{
    uploadProgressDelegate = newDelegate;
    [self resetProgressDelegate:newDelegate];

}


并且有内存泄漏,主要是在libxml中: enter image description here

我使用'useyourloaf'RSS Feed解析示例:
Feed.h:

#import <Foundation/Foundation.h>
#import "TFHpple.h"

@class ASIHTTPRequest, Channel;

@interface Feed : NSObject <NSXMLParserDelegate> {

    NSURL *feedURL;
    ASIHTTPRequest *feedRequest;

    Channel *feedChannel;
    NSMutableArray *feedPosts;

    id currentElement;
    NSMutableString *currentElementData;

    TFHpple *htmlParser;
    NSArray * elements;

}

@property (nonatomic, copy) NSURL *feedURL;
@property (nonatomic, retain) ASIHTTPRequest *feedRequest;
@property (nonatomic, retain) Channel *feedChannel;
@property (nonatomic, retain) NSMutableArray *feedPosts;
@property (nonatomic, assign) id currentElement;
@property (nonatomic, retain) NSMutableString *currentElementData;


- (id)initWithURL:(NSURL *)feedURL;
- (void)refresh;

extern NSString *kFeederReloadCompletedNotification;
extern NSString *kFeederReloadFailedNotification;

@end


Feed.m:

#import "Feed.h"
#import "Channel.h"
#import "Post.h"
#import "ASIHTTPRequest.h"
#import "NSString+HTML.h"

@implementation Feed

@synthesize feedURL;
@synthesize feedRequest;
@synthesize feedChannel;
@synthesize feedPosts;
@synthesize currentElement;
@synthesize currentElementData;

NSString *kFeederReloadCompletedNotification = @"kFeederReloadCompletedNotification";
NSString *kFeederReloadFailedNotification = @"kFeederReloadFailedNotification";

#pragma mark -
#pragma mark === Initialization and release ===
#pragma mark -

-(id)initWithURL:(NSURL *)sourceURL 
{

    if (self = [super init]) 
    {

        self.feedURL = sourceURL;
        self.feedPosts = [[NSMutableArray alloc] init];
    }

    return self;
}

- (void)dealloc 
{

    [currentElementData release];
    [feedPosts release];
    [feedChannel release];
    [feedRequest release];
    [feedURL release];

    [super dealloc];
}

#pragma mark -
#pragma mark === ASIHTTPRequest delegates ===
#pragma mark -

- (void)requestFinished:(ASIHTTPRequest *)request 
{

    NSData *responseData = [request responseData];

    //htmlParser = [[TFHpple alloc] init];

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:responseData];
    [parser setDelegate:self];

    [parser parse];

    if ([parser parse]) 
    {

        [[NSNotificationCenter defaultCenter] 
         postNotificationName:kFeederReloadCompletedNotification
         object:nil];

    }

    [parser release];
}

- (void)requestFailed:(ASIHTTPRequest *)request 
{
    NSError *error = [request error];
    UIAlertView *requestAlert = [[UIAlertView alloc] initWithTitle:[error localizedDescription] message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [requestAlert show];
    [requestAlert release];
    requestAlert = nil;

    [self performSelector:@selector(refresh) withObject:nil afterDelay:30.0];

    [[NSNotificationCenter defaultCenter] 
                        postNotificationName:kFeederReloadFailedNotification
                        object:nil];
}

#pragma mark -
#pragma mark === NSXMLParserDelegate methods ===
#pragma mark -

static NSString * const kChannelElementName = @"channel";
static NSString * const kItemElementName = @"item";

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
               namespaceURI:(NSString *)namespaceURI
               qualifiedName:(NSString *)qName
               attributes:(NSDictionary *)attributeDict 
{

    if ([elementName isEqualToString:kChannelElementName]) 
    {

        Channel *channel = [[Channel alloc] init];
        self.feedChannel = channel;
        self.currentElement = channel;
        [channel release];
        return;     
    }

    if ([elementName isEqualToString:kItemElementName]) 
    {
        Post *post = [[Post alloc] init];
        [feedPosts addObject:post];
        self.currentElement = post;
        [post release];
        return;     
    }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    if (currentElementData == nil) 
    {
        self.currentElementData = [[NSMutableString alloc] init];
    }

    [currentElementData appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
               namespaceURI:(NSString *)namespaceURI
               qualifiedName:(NSString *)qName 
{

    SEL selectorName = NSSelectorFromString(elementName);
    if ([currentElement respondsToSelector:selectorName]) 
    {       
        if(([currentElement isKindOfClass:[Post class]]) && ([elementName isEqualToString:@"description"]))
        {
            if(htmlParser)
            {
                [htmlParser release];
                htmlParser = nil;
            }
            htmlParser = [[TFHpple alloc] initWithHTMLData:[[[currentElementData stringByDecodingHTMLEntities] stringByRemovingNewLinesAndWhitespace] dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]];
            NSArray *img_array = [htmlParser searchWithXPathQuery:@"//img"];
            if(img_array.count)
            {
                NSURL *img_url = [NSURL URLWithString:[[img_array objectAtIndex:0] objectForKey:@"src"]];
                [currentElement setValue:img_url forKey:@"imageURL"];
            }
            else
            {
                [currentElement setValue:nil forKey:@"imageURL"];
            }
            NSArray *youtube_string_array = [htmlParser searchWithXPathQuery:@"//embed"];
            if(youtube_string_array.count)
            {
                NSString *youtube_string = [[youtube_string_array objectAtIndex:0] objectForKey:@"src"];
                [currentElement setValue:youtube_string forKey:@"youtubeString"];
            }
            else
            {
                [currentElement setValue:nil forKey:@"youtubeString"];
            }
        }

        if(([currentElement isKindOfClass:[Post class]]) && ([elementName isEqualToString:@"pubDate"]))
        {
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]autorelease];
            [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
            [dateFormatter setDateFormat:@"eee, dd MMM yyyy HH:mm:ss ZZZ"];

            NSString *date_string = [[currentElementData stringByConvertingHTMLToPlainText]stringByRemovingNewLinesAndWhitespace];

            NSDate *dateFromString = [dateFormatter dateFromString:date_string];

            //[dateFormatter setDateFormat:@"eee, dd MMM yyyy HH:mm"];
            [dateFormatter setDateStyle:NSDateFormatterLongStyle];
            [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
            dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"ru_RU"]autorelease];

            date_string = [dateFormatter stringFromDate:dateFromString];
            [currentElement setValue:date_string forKey:elementName];
            [dateFormatter release];
            dateFormatter = nil;
        }
        else if(([currentElement isKindOfClass:[Post class]]) && ([elementName isEqualToString:@"link"]))
        {
            [currentElement setValue:[[currentElementData stringByConvertingHTMLToPlainText] stringByRemovingNewLinesAndWhitespace] forKey:elementName];
        }
        else
        {
            [currentElement setValue:[[currentElementData stringByConvertingHTMLToPlainText] stringByRemovingNewLinesAndWhitespace] forKey:elementName];    
        }
    }

    [currentElementData release];
    self.currentElementData = nil;
}

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

//  NSString *info = [NSString stringWithFormat:@"Error %i, Description: %@, Line: %i, Column: %i",
//                    [parseError code],
//                    [[parser parserError] description],
//                    [parser lineNumber],
//                    [parser columnNumber]];

    //NSlog(@"RSS Feed Parse Error: %@", info);
}

#pragma mark -
#pragma mark === instance methods ===
#pragma mark -

- (void)refresh {

    self.feedRequest = [ASIHTTPRequest requestWithURL:feedURL];

    [feedPosts removeAllObjects];
    [feedRequest setDelegate:self];
    [feedRequest startAsynchronous];

}

@end

有人可以帮我删除这些警告和内存泄漏吗?任何帮助都将不胜感激!!

1 个答案:

答案 0 :(得分:3)

关于可达性问题:

在Reachability.h文件的顶部添加行

struct sockaddr_in;

摆脱第一组警告......

我文件的前几行是

#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>

struct sockaddr_in;

typedef enum {
    NotReachable = 0,
    ReachableViaWiFi,
    ReachableViaWWAN
} NetworkStatus;