内存泄漏调用同步方法

时间:2015-01-31 20:26:34

标签: memory-management

我有一个小的测试功能,运行一小时后我收到内存分配问题和内存警告。问题来自于电话         NSData * data = [NSURLConnection sendSynchronousRequest:req returningResponse:& response error:& error1];

以下是我的代码。 任何帮助将不胜感激。

Here is my app stat after 1 hour running:  
Low: 712Kb, High 275.4 MB, Durarion 1 hour 26 min 

2015-01-31 14:38:11.811 testMem[5268:546116] Received memory warning.
2015-01-31 14:38:16.836 testMem[5268:546116] Received memory warning.
2015-01-31 14:38:24.573 testMem[5268:546116] Received memory warning.
2015-01-31 14:38:29.850 testMem[5268:546116] Received memory warning.
2015-01-31 14:38:34.779 testMem[5268:546116] Received memory warning.
2015-01-31 14:38:40.058 testMem[5268:546116] Received memory warning.
2015-01-31 14:38:44.922 testMem[5268:546116] Received memory warning.

MainView.m

#import "ViewController.h"
#import "global.h"


#import "ServiceSvc.h"
#import "ClsTickets.h"

@interface ViewController ()
{
    NSTimer* g_BACKUPTIMER;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    g_BACKUPTIMER = [NSTimer scheduledTimerWithTimeInterval:.4 target:self selector:@selector(runBackupProcess) userInfo:nil repeats:YES];

}
-(void)runBackupProcess
{
    if (stopTimer == 0)
    {
        [NSThread detachNewThreadSelector: @selector(runtest) toTarget:self withObject:nil];
    }
    else
    {

    }
}



- (void) runtest
{
    @autoreleasepool {
        @try
        {

            ServiceSvc_DoAdminUnitIpad *req2 = [[ServiceSvc_DoAdminUnitIpad alloc] init];
            req2.UnitDesc = @"Unit 6";
            req2.CustomerID = @"800014";
            req2.MachineID = nil;
            req2.UnitID = @"1";
            req2.GPSData = nil;

            ServiceSynchronous* synCrhous = [[ServiceSynchronous alloc] init];

            NSData* data = [synCrhous makeSynchrounousCall:req2];

            data = nil;
            req2 = nil;

        }
        @catch (NSException *exception)
        {
        }
        @finally
        {
        }
    } // end auto

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

ServiceSvc.m

@implementation ServiceSynchronous
-(NSData*) makeSynchrounousCall:(ServiceSvc_DoAdminUnitIpad*) parameters
{
    NSURLResponse* response = nil;
    NSError* error1 = nil;


    NSString *operationXMLString  = @"<?xml version=\"1.0\"?> <soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:ServiceSvc=\"https://xxx/service.asmx\" xsl:version=\"1.0\"> <soap:Body> <ServiceSvc:DoAdminUnitIpad> <ServiceSvc:UnitID>1</ServiceSvc:UnitID> <ServiceSvc:UnitDesc>Unit 6</ServiceSvc:UnitDesc> <ServiceSvc:CustomerID>800014</ServiceSvc:CustomerID> </ServiceSvc:DoAdminUnitIpad> </soap:Body> </soap:Envelope>";
    NSString *msgLength = [NSString stringWithFormat:@"%d",
                           [operationXMLString length]];

    NSMutableURLRequest * req = [[NSMutableURLRequest alloc] init];
    [req setURL:[NSURL URLWithString:@"https://xxx/service.asmx"]];
    [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [req addValue:@"http://tempuri.org/webservice-name/method-name" forHTTPHeaderField:@"SOAPAction"];
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    [req setHTTPMethod:@"POST"];

    [req setHTTPBody:[operationXMLString dataUsingEncoding:NSUTF8StringEncoding]];

    NSData* data = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error1];
    [[NSURLCache sharedURLCache] removeCachedResponseForRequest:req];

    if (error1 == nil)
    {

    }
    else
    {

    }

    operationXMLString = nil;
    req = nil;

    response = nil;
    return data;
}
@end


@implementation ServiceSvc_DoAdminUnitIpad
- (id)init
{
    if((self = [super init])) {
        UnitID = 0;
        UnitDesc = 0;
        CustomerID = 0;
        MachineID = 0;
        GPSData = 0;
    }

    return self;
}

- (NSString *)nsPrefix
{
    return @"ServiceSvc";
}
- (xmlNodePtr)xmlNodeForDoc:(xmlDocPtr)doc elementName:(NSString *)elName elementNSPrefix:(NSString *)elNSPrefix
{

}
/* elements */
@synthesize UnitID;
@synthesize UnitDesc;
@synthesize CustomerID;
@synthesize MachineID;
@synthesize GPSData;
/* attributes */
- (NSDictionary *)attributes
{
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];

    return attributes;
}
+ (ServiceSvc_DoAdminUnitIpad *)deserializeNode:(xmlNodePtr)cur
{
    ServiceSvc_DoAdminUnitIpad *newObject = [ServiceSvc_DoAdminUnitIpad new];

    [newObject deserializeAttributesFromNode:cur];
    [newObject deserializeElementsFromNode:cur];

    return newObject;
}
- (void)deserializeAttributesFromNode:(xmlNodePtr)cur
{
}
- (void)deserializeElementsFromNode:(xmlNodePtr)cur
{



}
@end

ServiceSvc.h

@interface ServiceSvc_DoAdminUnitIpad : NSObject {

/* elements */
    NSString * UnitID;
    NSString * UnitDesc;
    NSString * CustomerID;
    NSString * MachineID;
    NSString * GPSData;
/* attributes */
}
- (NSString *)nsPrefix;
- (xmlNodePtr)xmlNodeForDoc:(xmlDocPtr)doc elementName:(NSString *)elName elementNSPrefix:(NSString *)elNSPrefix;
- (void)addAttributesToNode:(xmlNodePtr)node;
- (void)addElementsToNode:(xmlNodePtr)node;
+ (ServiceSvc_DoAdminUnitIpad *)deserializeNode:(xmlNodePtr)cur;
- (void)deserializeAttributesFromNode:(xmlNodePtr)cur;
- (void)deserializeElementsFromNode:(xmlNodePtr)cur;
/* elements */
@property (retain) NSString * UnitID;
@property (retain) NSString * UnitDesc;
@property (retain) NSString * CustomerID;
@property (retain) NSString * MachineID;
@property (retain) NSString * GPSData;
/* attributes */
- (NSDictionary *)attributes;
@end

@interface ServiceSynchronous : NSObject {
}
- (NSData*) makeSynchrounousCall:(ServiceSvc_DoAdminUnitIpad*) parameters;
@end

1 个答案:

答案 0 :(得分:0)

经过一番研究,我得出结论,Apple的NSURLConnection就是问题所在。问题是在2008年报道的,苹果已经承认了它,但现在是2015年,问题仍然存在。我已经切换到异步调用,内存分配问题仍然存在,但更小。那些在Apple的极客是个玩笑。