Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / April 2007



Tip: Looking for answers? Try searching our database.

Bus error at [pool release]

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Thomas  Andrews - 29 Apr 2007 15:35 GMT
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
Patrick Machielse - 29 Apr 2007 15:49 GMT
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
Sherm Pendley - 29 Apr 2007 18:25 GMT
> 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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.