iDevGames Forums
Objective C problem - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: Objective C problem (/thread-2898.html)



Objective C problem - PhysicsGuy - Dec 9, 2007 05:32 PM

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


Objective C problem - unknown - Dec 9, 2007 06:15 PM

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.


Objective C problem - PhysicsGuy - Dec 9, 2007 07:49 PM

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


Objective C problem - backslash - Dec 10, 2007 06:41 AM

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.


Objective C problem - OneSadCookie - Dec 10, 2007 12:33 PM

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 / ...).