gpt4 book ai didi

iPhone内存泄漏,不知道为什么Instruments工具这么认为

转载 作者:行者123 更新时间:2023-11-29 05:01:05 25 4
gpt4 key购买 nike

我心脏病发作,找不到问题,请查看下面仪器工具泄漏窗口的屏幕截图,我保留 xmlBody 并通过 @properties 将 doc 复制到头文件中。

如果我释放 theXML 对象,它也会崩溃。不知道为什么。其他对象在 dealloc 方法中释放

@property (nonatomic,retain) NSURLConnection *conn;
@property (nonatomic,retain) GDataXMLDocument *doc;
@property (nonatomic,copy) NSString *xmlBody;

enter image description here

enter image description here

另一种方法

enter image description here

最佳答案

self.docretaincopy 属性吗?

如果是这样,您应该像这样初始化它:

self.doc = [[[GData... alloc] initWith....] autorelease];

theXML 发生的情况如下:

   NSString *theXML = [[NSString alloc] initWithBytes:[xmlData bytes] length:[xmlData length] encoding:NSUTF8StringEncoding];  

您分配并初始化一个字符串对象; theXML 指向它;

   theXML =[theXML stringByReplacingOccurrencesOfString:@"inferenceresponse" withString:@"inferencerequest"];

在这里,您通过调用stringByReplacingOccurrencesOfString创建一个自动释放字符串,然后使theXML指向它; theXML 之前的值丢失;所以你有内存泄漏;

   theXML =[theXML stringByReplacingOccurrencesOfString:@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" withString:@"<SOAP-ENV:Envelope  xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"> "];

在这里,您通过调用stringByReplacingOccurrencesOfString创建一个自动释放字符串,然后使theXML指向它; theXML 之前的值丢失了,但这并不重要,因为该对象是自动释放的,因此它会在某个时间点自动释放。

在这种情况下,您需要做的是:

    NSString *theXML = [[[NSString alloc] initWithBytes:[xmlData bytes] length:[xmlData length] encoding:NSUTF8StringEncoding]];  

并保留其余代码,或者,如果您不想自动释放(但没关系),则:

    NSString *theXML = [[NSString alloc] initWithBytes:[xmlData bytes] length:[xmlData length] encoding:NSUTF8StringEncoding];  

NSString* theXML2 =[theXML stringByReplacingOccurrencesOfString:@"inferenceresponse" withString:@"inferencerequest"];

theXML2 =[theXML2 stringByReplacingOccurrencesOfString:@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" withString:@"<SOAP-ENV:Envelope xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"> "];

[theXML release];

关于iPhone内存泄漏,不知道为什么Instruments工具这么认为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6939197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com