I'm obviously missing something here. but I have a simple Objective-C
program which is generating a bus error on the line with the [pool
release] command.
/* upbcopy.m */
#import <Cocoa/Cocoa.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSStringEncoding encoding = NSUTF8StringEncoding;
NSFileHandle *fh = [NSFileHandle fileHandleWithStandardInput];
NSData *data = [fh availableData];
NSString *string = [[NSString alloc] initWithData:data encoding:
encoding];
[data release];
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard declareTypes:
[NSArray arrayWithObject:NSStringPboardType]
owner:nil];
[pasteboard setString:string forType:NSStringPboardType];
[fh release];
[pool release];
return 0;
}
Compile with:
gcc -o upbcopy -framework Cocoa upbcopy.m
Run it with some standard input: ./upbcopy < /dev/null
GDB says the program fails at the [pool release] call. If I remove the
[pool release] call, it works fine, but my understanding is that we
need to release the pool in general.
Any help would be greatly appreciated. Thanks!
=thomas andrews
Thomas Andrews <thomasoa@gmail.com> wrote:
> I'm obviously missing something here. but I have a simple Objective-C
> program which is generating a bus error on the line with the [pool
[quoted text clipped - 22 lines]
> return 0;
> }
'data' and 'fh' are autoreleased (added to 'pool'). If you manually
release them, they will be invalid by the time pool tries to release
them. Therefore you need to remove [data release] and [fh release].
patrick
Jens Ayton - 29 Apr 2007 16:04 GMT
Patrick Machielse:
> Thomas Andrews:
>
>> I'm obviously missing something here. but I have a simple Objective-C
>> program which is generating a bus error on the line with the [pool
>> release] command.
[snip]
> 'data' and 'fh' are autoreleased (added to 'pool'). If you manually
> release them, they will be invalid by the time pool tries to release
> them. Therefore you need to remove [data release] and [fh release].
Also, read and understand Memory Management Programming Guide for Cocoa:
<http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/index.html>,
especially the section Memory Management Rules.

Signature
Jens Ayton
Thomas Andrews - 29 Apr 2007 21:09 GMT
Thanks to all.
Yes, I'm a n00b. :-)
I've been programming since the 70s, but this is my first effort at
Objective-C programming.
=thomas
> I'm obviously missing something here. but I have a simple Objective-C
> program which is generating a bus error on the line with the [pool
[quoted text clipped - 8 lines]
>
> NSFileHandle *fh = [NSFileHandle fileHandleWithStandardInput];
You did not create fh with +alloc or -copy, nor have you sent it a -retain.
> NSData *data = [fh availableData];
You did not create data with +alloc or -copy, nor have you sent it a -retain.
> NSString *string = [[NSString alloc] initWithData:data encoding:
> encoding];
> [data release];
Why are you releasing data? Nothing in the standard memory handling pattern
indicates that you're supposed to.
> NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
> [pasteboard declareTypes:
> [NSArray arrayWithObject:NSStringPboardType]
> owner:nil];
> [pasteboard setString:string forType:NSStringPboardType];
> [fh release];
Why are you releasing fh? Nothing in the standard memory handling pattern
indicates that you're supposed to.
> [pool release];
Data was probably autoreleased; fh may have been autoreleased, but it's just
as likely to be a singleton, since it's a wrapper for one of the standard
filehandles.
So now you've got at least one and maybe two objects in the pool that you've
already released, that the pool is now trying to release *again*.
sherm--

Signature
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net