I'm currently using an instance variable that's an NSMutableString in a class which is a delegate for NSURLConnection. The variable is responsible for building a string of data that's returned from the delegate method:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
The string, 'foo', has a property set with retain on it. It is alloc'd in my classes init method very straight forward in this fashion:
dataString = [[NSMutableString alloc] init];
It is released in the classes dealloc method.
In connection:didReceiveData:, I use the var like this:
NSString *tmpString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[dataString appendString:tmpString];
[tmpString release];
Now this is where things get tricky. Since the class where I set the NSURLConnection delegate is a singleton (it mainly handles NSURL* type calls), I need to becareful with how I re-use my objects. Therefor, in my connectionDidFinishLoading: class I have the following:
// cache away data currently in dataString.
[dataString release];
dataString = [[NSMutableString alloc] init];
Does that strategy of handling my dataString make your eyes bleed? Am I leaking memory? What can I do that's smarter?
Are you going to ask us whether every use of an object in your application is a memory leak?
Read the documentation. Learn the rules of object ownership and you will have your answer already, every time.
And if you suspect a leak, run Instruments' ObjectAlloc probe. You've done this once already , so clearly you know how. There's also the leaks command .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.