[英]Memory Leaks - Objective-C
Can anyone see potential leaks in this code? 有人可以看到此代码中的潜在泄漏吗? I'm getting a "100%" leak according to Instruments on the line "NSString *ender = [temp stringFromDate:now];"
根据仪器在“ NSString * ender = [temp stringFromDate:now];”行上的消息,我收到了“ 100%”泄漏。
NSDateFormatter* temp = [[NSDateFormatter alloc] init];
[temp setDateFormat:@"yyyy-MM-dd"];
NSDate *now = [NSDate date];
NSString *ender = [temp stringFromDate:now];
DashboardViewController *controller = [[DashboardViewController alloc] init];
[controller initWithStartDate:ender andEndDate:ender];
[controller initAccount:account];
[self presentModalViewController:controller animated:NO];
[temp release];
所有这些东西后,您是否释放控制器?
This advice is unrelated to original question, but I think you should rename the initWithStartDate:andEndDate:
and initAccount:
methods since typically methods with "init" in the name return new instances. 该建议与原始问题无关,但是我认为您应该重命名
initWithStartDate:andEndDate:
和initAccount:
方法,因为通常名称中带有“ init”的方法会返回新实例。
Perhaps create your own -(id)initWithStartDate:endDate:account:
and call the designated initializer from within. 也许创建自己的
-(id)initWithStartDate:endDate:account:
并从内部调用指定的初始化程序。
Then you would create a new controller instance with 然后,您将使用以下命令创建一个新的控制器实例
DashboardViewController *controller = [[DashboardViewController alloc] initWithStartDate:ender endDate:ender account:account];
Gonzalo 贡扎洛
Since you pass your controller instance to the -presentModalViewController: method, that method will retain your controller. 由于您将控制器实例传递给-presentModalViewController:方法,因此该方法将保留您的控制器。 So you can safely release your controller, but you also should release your controller, since the memory management rules state that objects that you alloc+inited are owned by you and must be released.
因此,您可以安全地释放控制器,但是也应该释放控制器,因为内存管理规则指出,您分配+初始化的对象归您所有并且必须被释放。
On the other hand - just a small note - NSDateFormatter is a "heavy" object, cache the instance and reuse it, if it's possible. 另一方面-仅需注意-NSDateFormatter是一个“重”对象,如果可能的话,缓存实例并重用它。 Probably this is also the reason why Apple deprecated this method.
可能这也是Apple弃用此方法的原因。 You might call -init on NSDateFormatter from iOS 2.0 till iOS 3.2, but it is deprecated after iOS 3.2 .
您可以从iOS 2.0到iOS 3.2在NSDateFormatter上调用-init,但是在iOS 3.2之后不推荐使用-init。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.