[英]Is this usage of NSMutableString a memory leak?
I'm currently using an instance variable that's an NSMutableString in a class which is a delegate for NSURLConnection. 我当前正在使用实例变量,该变量是NSURLConnection的委托类中的NSMutableString。 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:
它在我的课init方法alloc'd非常直接以这种方式:
dataString = [[NSMutableString alloc] init];
It is released in the classes dealloc method. 它在类dealloc方法中发布。
In connection:didReceiveData:, I use the var like this: 在连接:didReceiveData :,我使用var这样的:
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.
因为在这里我设置的NSURLConnection的委托类是单(它主要处理NSURL *类型的呼叫),我需要怎么我再利用我的对象内容一定要小心。 Therefor, in my connectionDidFinishLoading: class I have the following:
因此,在我的connectionDidFinishLoading:类中,我具有以下内容:
// cache away data currently in dataString.
[dataString release];
dataString = [[NSMutableString alloc] init];
Does that strategy of handling my dataString make your eyes bleed? 处理我的dataString的策略是否会让您流血? 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. 如果怀疑泄漏,请运行Instruments的ObjectAlloc探针。 You've done this once already , so clearly you know how.
您已经做过一次 ,很清楚您知道如何做。 There's also the leaks command .
还有泄漏命令 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.