iPhone memory leakage, have no idea why Instruments tools thinks so
I am having heart attacks and couldnt find the problem, pls see the screensh开发者_开发问答ots below from instruments tool leakage window, I am retaining xmlBody and copying doc in header file by @properties.
I t also crashes if I relase the theXML object..dont know why..the other objects are released in dealloc method
@property (nonatomic,retain) NSURLConnection *conn;
@property (nonatomic,retain) GDataXMLDocument *doc;
@property (nonatomic,copy) NSString *xmlBody;
another method
Is self.doc
a retain
or copy
property?
If so, you should initialize it like this:
self.doc = [[[GData... alloc] initWith....] autorelease];
What happens with theXML
is following:
NSString *theXML = [[NSString alloc] initWithBytes:[xmlData bytes] length:[xmlData length] encoding:NSUTF8StringEncoding];
you alloc and init one string object; theXML points to it;
theXML =[theXML stringByReplacingOccurrencesOfString:@"inferenceresponse" withString:@"inferencerequest"];
here, you create an autorelease string by calling stringByReplacingOccurrencesOfString
, then make theXML
point to it; the previous value of theXML
is lost; so you have a memory leak;
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/\"> "];
here, you create an autorelease string by calling stringByReplacingOccurrencesOfString
, then make theXML
point to it; the previous value of theXML
is lost, but it does not matter because the object was autoreleased, so it will be release automagically at some point in time.
In this case also, what you need to do is:
NSString *theXML = [[[NSString alloc] initWithBytes:[xmlData bytes] length:[xmlData length] encoding:NSUTF8StringEncoding]];
and keep the rest of your code, or, if you do not want to autorelease (but it's ok), then:
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];
精彩评论