Andreas Falkenhahn
2016-08-26 15:15:39 UTC
Does anybody have an idea why the following code causes a "Segmentation fault" and thus
an immediate program termination? Interestingly, the code runs just fine and the
NSTableView appears correctly and is functional. The "Segmentation fault" occurs when
buttonPressed() is left and control returns to the main run loop. Thus, I suspect that
the error is probably related to autoreleasing, e.g. releasing a resource twice or
something. But I don't really see anything that's wrong with my code. Sometimes I also
get this message instead of the segmentation fault:
[NSRectSet tableView:objectValueForTableColumn:row:]:unrecognized selector sent to instance 0x100153060
But most of the time it just crashes with a segmentation fault. The code itself is
really simple and straightforward, here it is:
- (void)buttonPressed
{
NSRect rect = NSMakeRect(100, 100, 320 + 2 * 20, 78 + 200 + 20);
NSWindow *win = [[NSWindow alloc] initWithContentRect:rect styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO];
id listDelegate = [[MyListDelegate alloc] init];
NSScrollView *scrollview = [[NSScrollView alloc] initWithFrame:NSMakeRect(20, 52, rect.size.width - 2 * 20, 200)];
NSTableView *tableview = [[NSTableView alloc] initWithFrame:NSMakeRect(0, 0, rect.size.width - 2 * 20 - 16, 200)];
NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:@"Column"];
[tableview addTableColumn:column];
[tableview setDelegate:listDelegate];
[tableview setDataSource:listDelegate];
[scrollview setDocumentView:tableview];
[[win contentView] addSubview:scrollview];
[scrollview release];
[tableview release];
[column release];
id buttonDelegateOK = [[MyButtonDelegate alloc] init];
NSButton *ok = [[NSButton alloc] initWithFrame:NSMakeRect(rect.size.width - 82 - 14, 12, 82, 32)];
[ok setTitle:@"OK"];
[ok setButtonType:NSMomentaryPushInButton];
[ok setBezelStyle:NSRoundedBezelStyle];
[ok setKeyEquivalent:@"\r"];
[ok setTarget:buttonDelegateOK];
[ok setAction:@selector(buttonPressed)];
[[win contentView] addSubview:ok];
[ok release];
[NSApp runModalForWindow:win];
[win orderOut:nil];
[win release];
[listDelegate release];
[buttonDelegateOK release];
}
-----------------------------------------------------------------------------------------------
The list delegate is also minimal and looks like this:
@interface MyListDelegate : NSObject
@end
@implementation MyListDelegate
- (int)numberOfRowsInTableView:(NSTableView *)_tableView
{
return 3;
}
- (id)tableView:(NSTableView *)_tableView objectValueForTableColumn:(NSTableColumn *) tableColumn row:(int)row
{
return @"Foobar";
}
@end
-----------------------------------------------------------------------------------------------
I've already spent several hours trying to find out why this particular code results
in a "Segmentation fault" but I just don't see it.
Can anybody help? Thanks in advance!
Full minimal demo program is attached for reference.
an immediate program termination? Interestingly, the code runs just fine and the
NSTableView appears correctly and is functional. The "Segmentation fault" occurs when
buttonPressed() is left and control returns to the main run loop. Thus, I suspect that
the error is probably related to autoreleasing, e.g. releasing a resource twice or
something. But I don't really see anything that's wrong with my code. Sometimes I also
get this message instead of the segmentation fault:
[NSRectSet tableView:objectValueForTableColumn:row:]:unrecognized selector sent to instance 0x100153060
But most of the time it just crashes with a segmentation fault. The code itself is
really simple and straightforward, here it is:
- (void)buttonPressed
{
NSRect rect = NSMakeRect(100, 100, 320 + 2 * 20, 78 + 200 + 20);
NSWindow *win = [[NSWindow alloc] initWithContentRect:rect styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO];
id listDelegate = [[MyListDelegate alloc] init];
NSScrollView *scrollview = [[NSScrollView alloc] initWithFrame:NSMakeRect(20, 52, rect.size.width - 2 * 20, 200)];
NSTableView *tableview = [[NSTableView alloc] initWithFrame:NSMakeRect(0, 0, rect.size.width - 2 * 20 - 16, 200)];
NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:@"Column"];
[tableview addTableColumn:column];
[tableview setDelegate:listDelegate];
[tableview setDataSource:listDelegate];
[scrollview setDocumentView:tableview];
[[win contentView] addSubview:scrollview];
[scrollview release];
[tableview release];
[column release];
id buttonDelegateOK = [[MyButtonDelegate alloc] init];
NSButton *ok = [[NSButton alloc] initWithFrame:NSMakeRect(rect.size.width - 82 - 14, 12, 82, 32)];
[ok setTitle:@"OK"];
[ok setButtonType:NSMomentaryPushInButton];
[ok setBezelStyle:NSRoundedBezelStyle];
[ok setKeyEquivalent:@"\r"];
[ok setTarget:buttonDelegateOK];
[ok setAction:@selector(buttonPressed)];
[[win contentView] addSubview:ok];
[ok release];
[NSApp runModalForWindow:win];
[win orderOut:nil];
[win release];
[listDelegate release];
[buttonDelegateOK release];
}
-----------------------------------------------------------------------------------------------
The list delegate is also minimal and looks like this:
@interface MyListDelegate : NSObject
@end
@implementation MyListDelegate
- (int)numberOfRowsInTableView:(NSTableView *)_tableView
{
return 3;
}
- (id)tableView:(NSTableView *)_tableView objectValueForTableColumn:(NSTableColumn *) tableColumn row:(int)row
{
return @"Foobar";
}
@end
-----------------------------------------------------------------------------------------------
I've already spent several hours trying to find out why this particular code results
in a "Segmentation fault" but I just don't see it.
Can anybody help? Thanks in advance!
Full minimal demo program is attached for reference.
--
Best regards,
Andreas Falkenhahn mailto:***@falkenhahn.com
Best regards,
Andreas Falkenhahn mailto:***@falkenhahn.com