Objective-c 中的 NSTableViewDataSource 释放
我目前正在学习 Objective-C,并且正在使用 NSTableView 进行训练。
这是我的问题:
我已经通过 Interface Builder 将我的表视图链接到我的控制器,以便它有一个数据源,我已经在我的控制器中实现了 NSTableViewDataSource 协议,并且我已经实现了 -(NSInteger) numberOfRowsInTableView:
和-(id) tableView:objectValueForTableColumn:row:
方法。
我创建了一个原始业务类(“人”),并成功将其内容显示到我的 NSTableView 中。
但是然后,我在 dealloc 方法中放入了一些 NSLog 来查看内存是否被释放,似乎我的数组以及我的“person”实例从未被释放。
这是我在控制器中的 dealloc 代码:
-(void)dealloc
{
NSLog(@"the array is about to be deleted. current retain : %d",[personnes retainCount]);
[personnes release];
[super dealloc];
}
以及在我的“person”类中
-(void) dealloc
{
NSLog(@"%@ is about to be deleted. current retain : %d",[self prenom],[self retainCount]);
[self->nom release];
[self->prenom release];
[super dealloc];
}
当这些 dealloc 应该在应用程序生命周期中调用时?因为我预计他们会在窗户关闭时被叫到,但事实并非如此。
希望足够清楚,
谢谢:)
KiTe。
I'm currently learning objective-c and I'm currently training with NSTableView.
Here is my problem :
I have linked my tableview to my controller through Interface Builder so that it has a datasource, I have implemented NSTableViewDataSource protocol in my controller and I have implemented both -(NSInteger) numberOfRowsInTableView:
and -(id) tableView:objectValueForTableColumn:row:
methods.
I have created a raw business class ("person") and I succeeded to display its content into my NSTableView.
But then, I put some NSLog
in my dealloc methods to see whether the memory was freed or not and it seems that my array as well as my "person" instances are never released.
here is my dealloc code in the controller:
-(void)dealloc
{
NSLog(@"the array is about to be deleted. current retain : %d",[personnes retainCount]);
[personnes release];
[super dealloc];
}
and in my "person" class
-(void) dealloc
{
NSLog(@"%@ is about to be deleted. current retain : %d",[self prenom],[self retainCount]);
[self->nom release];
[self->prenom release];
[super dealloc];
}
When these deallocs are supposed to be called in the application lifecycle? Because I expected them to be called at the window closure, but it didn't.
In the hope of beeing clear enough,
Thanks :)
KiTe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您永远不会释放拥有(唯一)窗口的窗口控制器对象。因此,窗口控制器和 nib 文件中的每个顶级对象都会在整个应用程序生命周期中保留,包括窗口(及其视图)。
由于窗口控制器存在于整个应用程序生命周期中,因此它不会被释放,因此它的
-dealloc
方法永远不会被调用。而且,由于控制器-dealloc
方法从未被调用,因此它的personnes
数组不会被释放。personnes
数组拥有它的元素。由于数组未释放,其元素也未释放,因此永远不会调用相应类/实例的-dealloc
方法。I’m assuming you’re never releasing the window controller object that owns the (only) window. As such, the window controller and every top level object in the nib file are retained throughout the application lifecycle, including the window (and its views).
Since the window controller exists throughout the application lifecycle, it isn’t released, hence its
-dealloc
method is never called. And, since the controller-dealloc
method is never called, itspersonnes
array isn’t released.The
personnes
array owns its elements. Since the array isn’t released, neither are its elements, hence the-dealloc
method of the corresponding class/instances is never called.不要 曾经 使用
retainCount
< /a>.结果充其量是具有误导性的。如果你练习正确的内存管理实践,你会没事的。您是否遇到过内存问题/崩溃?Don't ever use
retainCount
. The results are misleading at best. If you practice proper memory management practices, you'll be fine. Have you had any memory issues/crashes?