基于视图的NSTableView EXC_BAD_ACCESS对狮子与ARC [英] View based NSTableView EXC_BAD_ACCESS on Lion with ARC

查看:232
本文介绍了基于视图的NSTableView EXC_BAD_ACCESS对狮子与ARC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这很奇怪。我有一个超级简单的项目来学习NSTableView,它设置在我的笔记本,设置为基于视图的tableView。我也设置了dataSource并委托给我的控制器obejct。



当我这样做,然而,运行,我得到一个EXC_BAD_ACCESS,主函数和堆栈的其余部分是Cocoa内部的(所以不是我的代码)。



除了这个项目使用ARC一个新的项目,所以这是默认)。



我也尝试使用分析仪,以确保我没有不当地做记忆管理任何地方,没有问题它。



如果我没有设置dataSource / delegate,我不会得到崩溃,但显然这不是一个非常好的方式来构建我的应用程序! / p>

有任何想法吗?



编辑 $ b

代理和数据源都在IB中设置。代码如下(基于视图)。请注意,无论此代码是否存在,我都会崩溃,无论是哪种情况,都会发生崩溃:

   - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return 5;
}

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSTextField * cell = [tableView makeViewWithIdentifier :@MyViewowner:self];

if(nil == cell){
cell = [[NSTextField alloc] initWithFrame:CGRectZero];


cell.identifier = @MyView;
}


[cell setStringValue:[NSString stringWithFormat:@Row%d,row + 1]];

return cell;
}


解决方案

>

我一直(有意地)试图泄漏一个变量(因为我太懒了,不能创建一个实例变量...在这里写快速代码),但当然ARC



所以,我只需要一个强的属性,所以我试图粘附的对象(该对象充当我的tableView的委托和dataSource)不会过早发布。


This is weird. I've got a super simple project to learn NSTableView, and it's set up in my nib, set as a View-based tableView. I've also set the dataSource and delegate to my controller obejct.

When I do this, however, and run, I get an EXC_BAD_ACCESS, with the trace starting in my main function and the rest of the stack is internal to Cocoa (so not my code).

There's really nothing fancy going on, other than this project is using ARC (it's a new project, so this was the default).

I also tried using the Analyzer to make sure I wasn't improperly doing memory managment anywhere and there were no issues with it.

I don't get the crash if I don't set the dataSource/delegate, but obviously this is not a very good way to build my app!

Any ideas?

Edit

The delegate and dataSource are both set up in IB. The code is as follows (view-based). It's important to note, I'm getting crashes whether or not this code is present, and it's the same crash in either case:

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    return 5;
}

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    NSTextField *cell = [tableView makeViewWithIdentifier:@"MyView" owner:self];

    if (nil == cell) {
        cell = [[NSTextField alloc] initWithFrame:CGRectZero];


        cell.identifier = @"MyView";
    }


    [cell setStringValue:[NSString stringWithFormat:@"Row %d", row + 1]];

    return cell;
}

解决方案

It's simple!

I had been (somewhat intentionally) trying to leak a variable (because I was too lazy to make an instance variable...writing quick code here), but of course ARC took care of that leak for me, causing the whole thing to blow up.

So, I just needed to make a strong property so the object I was trying to have stick around (which object was acting as my tableView's delegate and dataSource) would not be prematurely released.

这篇关于基于视图的NSTableView EXC_BAD_ACCESS对狮子与ARC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆