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.

Why a bus error?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Seward S. - 28 Apr 2007 20:45 GMT
I'm trying to learn Objective C, and am running into a problem I don't
seem to be able to solve by myself.
The following test code compiles OK, but when I run it, it gives me a
bus error.  I've indicated where it prints, and where it seems to
develop the problem. Can anyone point me in the right direction?
Thanks!

//Atest.m
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSAutoreleasePool.h>
#import <stdio.h>

@interface TestObject: NSObject
{
    NSString *testName;
}
-(id) initWithName: (NSString *) name;
-(NSString *) returnName;
@end

@implementation TestObject;
-(id) initWithName: (NSString *) name
{
    self = [super init];
    if (self) {
        testName = [[NSString alloc] initWithString: name];
        printf("\nThe title of the test object is %s\n", [testName
UTF8String]);   //This prints OK
    }
}
-(NSString *) returnName
{
    return testName;
}
@end

int main (int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    TestObject *myTestObject = [[TestObject alloc] initWithName:
@"Test"];
    NSString *myTestName;
    myTestName = [myTestObject returnName];
    printf("/n A second test of the test Object is %s\n", [myTestName
UTF8String]);  //This does not print
    [myTestObject release];
    [pool release];
    return 0;
}

Here is the compilation, and the result of running it...

seward-salvages-computer:~/Kochan sewardsalvage$ gcc -framework cocoa
Atest.m -o t
seward-salvages-computer:~/Kochan sewardsalvage$ ./t

The title of the test object is Test
Bus error
Gregory Weston - 28 Apr 2007 22:39 GMT
> I'm trying to learn Objective C, and am running into a problem I don't
> seem to be able to solve by myself.
[quoted text clipped - 12 lines]
>     }
> }

Your init never actually returns the object. Add 'return self;' as the
last line and you should be set.

In the future, adding a -Wall to your compile command would be helpful.
Patrick Machielse - 28 Apr 2007 23:02 GMT
> The following test code compiles OK, but when I run it, it gives me a
> bus error.  I've indicated where it prints, and where it seems to
> develop the problem. Can anyone point me in the right direction?

> -(id) initWithName: (NSString *) name
> {
[quoted text clipped - 4 lines]
> UTF8String]);   //This prints OK
>   }

=>> At this point you need to 'return self'

> }

It's probably a good idea to turn on as many warnings as possible while
your learning. And possibly later on as well ;-)

When you compile your code using Xcode you'll get a warning: "control
reaches end of non-void function". If you can't use Xcode, the gcc flag
for this specific warning is -Wreturn-type. It's not included in -Wall.

patrick
Patrick Machielse - 28 Apr 2007 23:06 GMT
> If you can't use Xcode, the gcc flag for this specific warning is
> -Wreturn-type. It's not included in -Wall.

Correction: it *IS* included in -Wall. As it should be...

patrick
Seward S. - 29 Apr 2007 00:01 GMT
> > If you can't use Xcode, the gcc flag for this specific warning is
> > -Wreturn-type. It's not included in -Wall.
>
> Correction: it *IS* included in -Wall. As it should be...
>
> patrick

Thank you both..."return self" did the trick.  I'll be using -Wall in
the future.

Steve
 
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.