... has exited due to signal 11 (SIGSEGV)

boyfarrell

Registered
Hi all.

I seem to get this error when I use a method with return type void?

Here is some code that works:
Code:
#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSString.h>

#import "Spectra.h"

int main(int argc, char *argv[])
{
	
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	
	Spectra* mySpectra;
	double num = [mySpectra setElementX: 0 To: 123.0];

	printf ("%lf",num);

        [pool release];
        return 0;

}
The above is a simple 'setter' for an array, which is the instance variable or Spectra class. Here is the @implemention:
Code:
-(double)setElementX: (unsigned int) element To: (double) elementValue;
{
	xColumn[element] = elementValue;
	return xColumn[element];
}
However, my old code that returned void gave me the signal 11 error:
Code:
#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSString.h>

#import "Spectra.h"

int main(int argc, char *argv[])
{
	
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	
	Spectra* mySpectra;
	[mySpectra setElementX: 0 To: 123.0];

	[mySpectra printElement: 0];

        [pool release];
        return 0;

}
With the method:
Code:
-(void) setElementX: (unsigned int) element: To (double) elementValue;
{
        //xColumn is the instance vaiable of the Spectra class
	xColumn[element] = elementValue;

}
Note the print method is just a very simple printf call calling the xColumn instance variable as you would imagine.

Clearly, I don't need this method to return anything, it's just clutters up my code! Any idea's on how to get around this quirk?

Daniel.
 

kainjow

Registered
"mySpectra" is never initialized. You need to set it up like "mySpectra = [[Spectra alloc] init];" or however you have it setup in your code. We'd also need to see your Spectra.h and Spectra.m code too.
 

boyfarrell

Registered
Yes, your right!

I just assumed that because we were 'pooling' the memory that we didn't have to do that. A few code examples from a book I'm reading didn't seem to alloc and init when they changes from Object to NSObject as there root...?
Code:
#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSString.h>
#import <Foundation/NSValue.h>

int main(int argc, char *argv[])
{
	
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSNumber *myNumber;

        intNumber = [NSNumber numberWithInt: 100];
        printf ("%i\n", [intNumber intValue]);

        [pool release];
        return 0;
}
 

kainjow

Registered
I'm not sure what books you're reading, but if you want to learn Objective-C for the Mac, you should get a Cocoa-based Objective-C book, or just any Cocoa beginning book. alloc and init are NSObject methods. Non-NSObject based objects are handled differently, and you will never see those in any Cocoa programming you do.

If you're not familiar with how memory management works in Cocoa, you should read up on it. It's extremely important to know how it works. I would suggest you get a handle on this before going much further.
 
Top