Objective C problem

Apprentice
Posts: 6
Joined: 2007.12
Post: #1
I have an object that isn't responding to its own method.

My object Column looks like this.

Column.h
Code:
#import <Cocoa/Cocoa.h>
#import "fitsio.h"
#import "FitsException.h"

@interface Column : NSObject
{
    fitsfile *handle;
    NSString *columnName;
    int columnType;
    int numRows;
    int colNum;
}

-(Column *)initWithHandle:(fitsfile *)hand withNumRows:(int)rows withColNum:(int)num;

-(NSString *)columnName;
-(int)columnType;
-(int)numRows;
-(int)colNum;

-(void)setColumnName;
@end

Column.m
Code:
#import "Column.h"
#import "FitsException.h"
#import "FitsCOL_NOT_FOUNDException.h"
#import "fitsio.h"


@implementation Column
-(Column *)initWithHandle:(fitsfile *)hand withNumRows:(int)rows withColNum:(int)num
{
    self = [super init];
    handle = hand;
    numRows = rows;
    colNum = num;
    
    [self setColumnName];
    NSLog(columnName);
    NSLog(@"%@", [self columnName]);
    return self;
}

-(NSString *)columnName
{
    NSLog(@"ColumnName Called");
    return columnName;
}

-(int)columnType
{
    return columnType;
}

-(int)numRows
{
    return columnType;
}

-(int)colNum
{
    return colNum;
}

-(void)setColumnName
{
    int status = noErr;
    char name[80];
    char *templt;
    int number;
    
    sprintf(templt, "%d", colNum + 1);
    
    fits_get_colname(handle, CASEINSEN, templt, name, &number, &status);
    if(status)
        if(status == COL_NOT_FOUND)
            @throw [[FitsCOL_NOT_FOUNDException alloc] init];
        else
            @throw [[FitsException alloc] initWithStatus:status];
            
    columnName = [NSString stringWithCString:name encoding:NSASCIIStringEncoding];
}
@end

It is crashing when I try to call [self columnName]. This method isn't getting called and my crash log looks like this.

Code:
...

Thread 0 Crashed:
0   libobjc.A.dylib                   0xfffeff18 objc_msgSend_rtp + 24
1   com.psl.iFits                     0x000056e0 -[Column initWithHandle:withNumRows:withColNum:] + 100 (Column.m:24)
...

I think it has something to do with the late binding nature of Objective C. But I can't figure out why it's crashing or how to fix it. I'm using 10.5.1
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
It's nothing to do with late binding in ObjC, You just used sprintf incorrectly which is trashing the programs memory and things break from that point on, use NSString stringWithFormat instead of sprintf.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Apprentice
Posts: 6
Joined: 2007.12
Post: #3
Quote:It's nothing to do with late binding in ObjC, You just used sprintf incorrectly which is trashing the programs memory and things break from that point on, use NSString stringWithFormat instead of sprintf.

Your right sprintf was the problem. char *templt wasn't getting memory allocated for it.

Thanks
Quote this message in a reply
Member
Posts: 245
Joined: 2005.11
Post: #4
Also, the stringWithCString: method returns an autoreleased object, so you'll have to retain it yourself or it will disappear at the end of this time round the application run loop. Or you could use
Code:
columnName = [[NSString alloc] initWithCString:name encoding:NSASCIIStringEncoding]
You should really also check if there is already a column name set and release it if there is before setting the new one. This will be easier if you set columnName = nil in your init method before calling setColumnName.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
Setting stuff to nil in -init is redundant; ObjC guarantees that all fields of an object are initialized to no-bits-on (NULL / nil / 0 / 0.0 / ...).
Quote this message in a reply
Post Reply