ios - 仪器中存在内存泄漏的类

标签 ios instruments memory-leaks

我有几个类,它们的功能是针对 WEB 服务执行 SQL 语句,以便在数据库中获取或设置数据。

工作正常,但问题是当我在仪器/泄漏中测试时,90% 的泄漏是因为这些类。

你能告诉我我失去了什么吗?

谢谢。

代码如下:

数据存储位置:

.h

@interface iSQLResult : NSObject {
    NSMutableArray *Records;
}
@property (nonatomic, assign) int CountX;
@property (nonatomic, assign) int CountY;
@property (nonatomic, retain) NSMutableArray *Columns;
@property (nonatomic, retain) NSMutableArray *Records;
@property (nonatomic, retain) NSMutableArray *FieldsNames;
@property (nonatomic, assign) int ErrorCode;
@property (nonatomic, retain) NSString *ErrorDescription;


-(void)addField:(NSString*)fieldName;
-(void)addRecord:(NSMutableArray*)items;
-(NSMutableArray *)getRecord:(int)y;
-(NSString*)getValue:(int) x posY:(int) y;
-(NSString*)getValueByName:(NSString *) colName posY:(int) y;
-(void)setValueByName:(NSString *) colName posY:(int) y value:(NSString *)value;
-(id) copyWithZone: (NSZone *) zone;
@end

.m

#import "iSQLResult.h"
#import <stdarg.h>

@implementation iSQLResult
@synthesize CountX;
@synthesize CountY;
@synthesize Columns;
@synthesize Records;
@synthesize FieldsNames;
@synthesize ErrorCode;
@synthesize ErrorDescription;

-(id)init
{
    self = [super init];

    if (self)
    {
        self.CountX =0;
        self.CountY =0;
        self.ErrorCode = 0;
        self.ErrorDescription = @"";

        self.FieldsNames = [NSMutableArray array];
        self.Columns = [NSMutableArray array];
        self.Records = [NSMutableArray array];

    }

    return self;
}
-(void)removeRecord:(int)index
{
    [self.Records removeObjectAtIndex:index];
    self.CountY = self.CountY - 1;
}
-(void)addField:(NSString*)fieldName
{
    [self.FieldsNames addObject:[NSString stringWithFormat:@"%@", fieldName]];
    self.CountX = self.CountX +1;
}
-(void)addRecord:(NSMutableArray*)items
{
    [self.Records addObject:items];
    self.CountY = self.CountY +1;
}
-(NSMutableArray *)getRecord:(int)y
{
    return [Records objectAtIndex:y];
}
-(NSString *)getValue:(int) x posY:(int)y
{
    return [[NSString stringWithFormat:@"%@", [[Records objectAtIndex:y] objectAtIndex:x]] copy];
}
-(NSString*)getValueByName:(NSString *) colName posY:(int) y
{
    int a=0;
    for (a=0;a<CountX;a++)
    {
        if ([[colName uppercaseString] isEqualToString:[[FieldsNames objectAtIndex:a] uppercaseString]])
        {
            return [[NSString stringWithFormat:@"%@", [[Records objectAtIndex:y] objectAtIndex:a]] copy];
        }
    }
    return @"";
}
-(void)setValueByName:(NSString *) colName posY:(int) y value:(NSString *)value
{
    int a=0;
    for (a=0;a<CountX;a++)
    {
        if ([[colName uppercaseString] isEqualToString:[[FieldsNames objectAtIndex:a] uppercaseString]])
        {
            [[Records objectAtIndex:y] replaceObjectAtIndex:a withObject:value];
        }
    }

}
-(void)dealloc
{
    [Columns release];
    [Records release];
    [FieldsNames release];
    [ErrorDescription release];

    [super dealloc];
}
-(id) copyWithZone: (NSZone *) zone
{
    iSQLResult *SQLRCopy = [[iSQLResult allocWithZone: zone] init];

    [SQLRCopy setCountX:self.CountX];
    [SQLRCopy setCountY:self.CountY];
    [SQLRCopy setRecords:[self.Records mutableCopyWithZone:zone]];
    [SQLRCopy setColumns:[self.Columns mutableCopyWithZone:zone]];
    [SQLRCopy setFieldsNames:[self.FieldsNames mutableCopyWithZone:zone]];
    [SQLRCopy setErrorCode:self.ErrorCode];
    [SQLRCopy setErrorDescription:[self.ErrorDescription copyWithZone:zone]];

    return SQLRCopy;
}
@end

向web服务通信类请求数据并获取xml结构的类:

.h

#import <Foundation/Foundation.h>
#import "iSQLResult.h"
#import "IM2_WebServiceComm.h"

@interface iSQL : NSObject <NSXMLParserDelegate> {

    iSQLResult *SQLR;
    IM2_WebServiceComm * WSC;

    NSXMLParser *xmlParser;
    BOOL Found;
    BOOL FieldsReaded;
    BOOL loadFieldsNow;
    BOOL loadErrNumNow;
    BOOL loadErrDesNow;
    NSString *chunksString;
    NSMutableArray *tempRecord;
}
@property (nonatomic, retain) NSString *URLConnection;
-(void)SQLReader:(NSString*)SQLString;
-(void)SQLExec:(NSString*)SQLString;
-(void)setURLConnection:(NSString *) WebSURL;
-(iSQLResult*) getSQLR;
-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
-(void) parser:(NSXMLParser *) parser foundCharacters:(NSString *)string;
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict;
@end

.m

#import "iSQL.h"
@implementation iSQL
@synthesize URLConnection;
- (iSQLResult*)getSQLR
{
    return [SQLR copy];
}
-(void)SQLExec:(NSString*)SQLString
{

    FieldsReaded = NO;
    Found = NO;
    loadFieldsNow = NO;

    if (SQLR)
    {
        [SQLR release];
        SQLR = nil;
    }


SQLR = [[iSQLResult alloc] init];

WSC = [[IM2_WebServiceComm alloc] init];
[WSC setURL:URLConnection];

NSString *theXML = [WSC callMethod:@"ExecNonQuery" sendInstruction:SQLString];

@try
{
    xmlParser = [[NSXMLParser alloc] initWithData:[theXML dataUsingEncoding:NSUTF8StringEncoding]];
    [xmlParser setDelegate: self];
    [xmlParser setShouldResolveExternalEntities:NO];
    if(![xmlParser parse])
    {
        NSLog(@"ERROR PARSING");
    }
    [xmlParser release];
}
@catch(NSException * ex) 
{
    NSLog(@"%@",[[ex reason] UTF8String]);
}
[WSC release];
}
-(void)SQLReader:(NSString*)SQLString
{

FieldsReaded = NO;
Found = NO;
loadFieldsNow = NO;

if (SQLR)
{
    [SQLR release];
    SQLR = nil;
}

SQLR = [[iSQLResult alloc] init];

WSC = [[IM2_WebServiceComm alloc] init];
[WSC setURL:URLConnection];

NSString *theXML = [WSC callMethod:@"ExecSQL" sendInstruction:SQLString];

@try
{
    xmlParser = [[NSXMLParser alloc] initWithData:[theXML dataUsingEncoding:NSUTF8StringEncoding]];
    [xmlParser setDelegate: self];
    [xmlParser setShouldResolveExternalEntities:NO];
    if(![xmlParser parse])
    {
        NSLog(@"ERROR PARSING");
    }
    [xmlParser release];
}
@catch(NSException * ex) 
{
    NSLog([NSString stringWithFormat:@"%@",[[ex reason] UTF8String]]);
}
[WSC release];
}

-(iSQLResult *)getSingleSQLR:(iSQLResult *)SQLSource usingRow:(int)y
{

iSQLResult *SQLRAux = [[[iSQLResult alloc]init]retain];

[SQLRAux setCountX:SQLSource.CountX];
[SQLRAux addRecord:[SQLSource getRecord:y]];
[SQLRAux setFieldsNames:SQLSource.FieldsNames];

return SQLRAux;

}

-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
    NSLog(@"Error on XML Parse: %@", [parseError localizedDescription] );
}

//#pragma XML Parser Delegate Methods
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict 
{
    if (!chunksString)
{
    chunksString = [[NSString alloc] init];
}
chunksString = @"";
if ([elementName isEqualToString:@"ErrCode"])
{
    loadErrNumNow = YES;
}
if ([elementName isEqualToString:@"ErrDesc"])
{
    loadErrDesNow = YES;
}

if ([elementName isEqualToString:@"Table"])
{
    Found = YES;
    loadFieldsNow = NO;
    tempRecord = [[NSMutableArray alloc] init];
}

if (Found && ![elementName isEqualToString:@"Table"]) 
{
    loadFieldsNow = YES;
    if (!FieldsReaded)
    {
        [SQLR addField:elementName];
    }   
}
}

-(void) parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{
    chunksString = [chunksString stringByAppendingString:string]; 
}       

-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{
NSString * finalString;
finalString = [NSString stringWithFormat:@"%@",chunksString];

if (loadErrNumNow)
{
    [SQLR setErrorCode:[finalString intValue] ];
    loadErrNumNow = NO;
}
if (loadErrDesNow)
{
    [SQLR setErrorDescription:finalString];
    loadErrDesNow = NO;
}           

if (Found)
{
    if (loadFieldsNow) 
    {
        [tempRecord addObject:finalString]; 
    }
}

if ([elementName isEqualToString:@"Table"])
{
    [SQLR addRecord:tempRecord];
    [tempRecord release];
    Found = NO;
    FieldsReaded = YES;
    loadFieldsNow = NO;

}
}

-(void)dealloc
{
    if (SQLR)
    { 
        [SQLR release];
        SQLR = nil;
    }

    [URLConnection release];

    [super dealloc];

}
@end

这是一个绝对有问题的类,因为泄漏,每次访问都是泄漏 :(

有什么帮助吗?

最佳答案

以下是我注意到的一些事情:

-[iSQLResult copyWithZone:]

您将 mutableCopyWithZone: 的结果(返回一个保留的对象)发送给 setter , setter 再次保留该对象。修复它的一种方法是自动释放副本:

[SQLRCopy setRecords:[[self.Records mutableCopyWithZone:zone] autorelease]]

-[iSQL SQLExec:] 和 -[iSQL SQLReader:]

ivars WSC 和 xmlPareser 被分配然后释放但没有设置为零。这不是泄漏,但您保留了对已释放对象的引用。如果取消引用它,就会崩溃。

-[iSQL getSingleSQLR:usingRow:]

iSQLResult *SQLRAux = [[[iSQLResult alloc]init]retain];

你的意思是自动释放而不是保留在那里吗? (如果你这样做了,为了保持一致性,你不应该也自动释放 getSQLR 中的返回值吗?)

-[iSQL 解析器:didStartElement:namespaceURI:qualifiedName:attributes:]

chunksString 已分配(+1 保留),但稍后在 parser:foundCharacters 中:您将丢失引用(即泄漏字符串)并将其替换为自动释放的字符串。


这就是我注意到的全部内容。听起来你泄漏的不仅仅是那些对象,所以我猜你也在泄漏 iSQL 和/或 iSQLResult 对象。请记住,当您泄漏一个对象时,您也会泄漏这些对象持有的所有引用。即使这些类没有泄漏,如果实例化类的代码没有正确释放对象,您也会看到这些类中的所有成员都发生泄漏。修复泄漏时,始终首先查找“顶级”对象。

关于ios - 仪器中存在内存泄漏的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9507190/

相关文章:

iOS - 即使实际应用程序(SpriteKit 应用程序)没有显示内存泄漏,内存使用量也会不断增长

ios - 如何为 iPhone 6 Plus 之前的所有 iPhone 设备支持一个 xib?

xcode - 无法理解仪器数据

ios - CloudKit 订阅未在 iOS10 上保存

iphone - 仪器内存泄漏与 ARC 混淆

ios - 通过 UIAutomation 在 UIDatePicker 中选择一个日期?

c - C 应用程序调试与 Release模式中的内存泄漏

linux - 如何制定自由策略?

ios - 在标签栏中添加一个按钮,就像 Vinted 应用程序一样

android - Firebase - 同一项目中的多个应用程序可以使用相同的服务帐户 key (推送通知)