0

I am having heart attacks and couldnt find the problem, pls see the screenshots 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;

enter image description here

enter image description here

another method

enter image description here

Spring
  • 11,333
  • 29
  • 116
  • 185
  • Once you allocate an object you need to release it, I haven't seen that part. – Vanya Aug 04 '11 at 09:46
  • you are saying: 'I am retaining xmlBody and copying doc in header file by @properties', But you retaining doc and copy xmlBody – peko Aug 04 '11 at 11:50

1 Answers1

1

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];
sergio
  • 68,819
  • 11
  • 102
  • 123
  • yes it is retain, so what should i try? but it is mutable so doesnt it need to be copy? – Spring Aug 04 '11 at 09:47
  • I dont want it to be autorelase cause I will acess it later, so I wanna make it nil just when I want and relase it in deallocate, can I do that? – Spring Aug 04 '11 at 09:48
  • 1
    If `self.doc` is retain or copy, then you need the autorelease, or you have a memory leak. Once you do the autorelease, you will be able to access it later and nil-ing it in dealloc, and will have no memory leak. Try it. – sergio Aug 04 '11 at 09:54
  • when I autorelase then I dont need to relase in Dealloc I guess? I wonder why cant I relase it in dellaoc method but I have to autorelase it? I want to have control over my properties and accces them when I want so I release them in dellaoc and try to not autorelase them – Spring Aug 04 '11 at 10:11
  • 1
    always the same: if you `[[... alloc] init...]` to some `retain` property, then it must be autoreleased: `[[[... alloc] init...] autorelease`. It's a simple rule that is always true. – sergio Aug 04 '11 at 10:45
  • could you also look at http://stackoverflow.com/questions/6968521/iphone-allocations-problem – Spring Aug 06 '11 at 18:22