增加了多少引用计数
我现在这是一个愚蠢的问题,但我对这个案例的理解仍然很差。这是关于内存管理和引用计数的,我对如果使用复制、分配和可变复制会增加多少引用计数有一些疑问。这是我的代码:
这是 myController.h :
#import <UIKit/UIKit.h>
@interface myController : UIViewController {
NSMutableString *mutableStringCode;
}
@property (nonatomic, copy) NSMutableString *mutableStringCode;
@end
这是
#import "myController.h"
@implementation myController
-(void)viewDidLoad{
mutableStringCode = [[NSMutableStringCode alloc]init];
[self refresh];
}
-(void)refresh{
NSMutableString *myFileContents = [NSMutableString stringWithContentsOfFile:localPath encoding:NSUTF8StringEncoding error:&error];
mutableStringCode = [myFileContents mutableCopy];
//another code
myFileContents = nil;
}
-(void)dealloc{
[mutableStringCode release];
[super dealloc];
}
@end
这段代码中的 myController.m ,我有一些疑问: 1. mutableStringCode 增加了多少引用计数? 2.使用copy
而不是retain
设置mutableStringCode
属性是正确的方法吗? 3. 在属性上设置复制后,我是否仍然需要分配该 mutableStringCode
?
有人可以向我描述一下吗? 谢谢
i now this is a silly question, but i still have some poor understanding about this case. It is about memory management and reference count, i have some doubt about how many reference count will increase if i use copy, alloc, and mutable copy. This is the code i have :
This is myController.h :
#import <UIKit/UIKit.h>
@interface myController : UIViewController {
NSMutableString *mutableStringCode;
}
@property (nonatomic, copy) NSMutableString *mutableStringCode;
@end
and this is myController.m
#import "myController.h"
@implementation myController
-(void)viewDidLoad{
mutableStringCode = [[NSMutableStringCode alloc]init];
[self refresh];
}
-(void)refresh{
NSMutableString *myFileContents = [NSMutableString stringWithContentsOfFile:localPath encoding:NSUTF8StringEncoding error:&error];
mutableStringCode = [myFileContents mutableCopy];
//another code
myFileContents = nil;
}
-(void)dealloc{
[mutableStringCode release];
[super dealloc];
}
@end
in this code, i have some doubts :
1. how many reference count increase in that mutableStringCode ?
2. is it a true way to set mutableStringCode
property using copy
not retain
?
3. am i still have to alloc that mutableStringCode
after i set it copy on property?
can some body describe it to me??
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在这里看到的主要问题是,在刷新期间,您没有释放已经创建的 mutableStringCode。所以它保留在内存中(这是通常的内存泄漏)。
您可以尝试使用分析或 Instruments 的泄漏工具来捕获这些类型的内存泄漏。
The main problem I see here is that during refresh you are not releasing already created mutableStringCode. So it stays retained in memory (it is usual memory leak).
You can try to catch these types of memory leaks with Analyse or with Instruments' Leaks tool.