开发者

Calling an external Class Method is causing Crash objective-c

I have two classes, using Box2d,Cocos2d.

Construct.mm and Level1.mm

Construct contains all the methods for creating different objects in the box2d physics engine

Level1 contains the information on the plotting of objects.

In the construct implementation I have:

Construct.mm

-(void) someInitMethod{  
Level1 *level1 = [[Level1 alloc] init];  
[level1 mapping];  
}  
-(void) someCreateRectMethod:(argue)ments{  
//create rect  
}

In the Level1 implementation I have:

Level1.mm

-(void) mapping{  
Construct *constr;  
if (constr == nil) constr = [[Construct alloc] init];  
[constr someCreateRectMethod:(argue)ments];  
}

Now, I know the allocation of Level1 *level1 is working fine. It jumps to -(void)mapping{ }.

My problem is in creating the Construct *constr object and initialising it. Without the if(constr == nil) statement. The simulator will die straight away, before even posting any error report.

If i put the if(constr == nil) and then try to call [constr someCreateRectMethod:arguement];

The console reports:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Level1 someCreateRectMethod:]: unrecognized selector sent to instance 0x5557fc0'

I think this is because its not allocing, therefore trying to send to self (Level1) rather than Construct.

Why isnt my Construct *constr class object not allocing? Like I said, no error reporting it occurring.. I have imported the Construct.h file. I've spent the last 4 hours trying to g开发者_运维技巧et this to work.

EDIT

I just made Layer 1 inherit from Construct. And instead of calling [constr someCreateRectMethod]; I simply call [super someCreateRectMethod];

This should theoretically work, but Simulator crashes and no error reporting is logged..

Once again, Thanks for your time.

Oliver.


You probably don't end up creating constr - the initial value is undefined and therefore very unlikely to be nil. If your code otherwise terminates at [[constr alloc] init] then it seems likely that you've failed to subclass NSObject (which you should) and not implemented init (which you often want to).


Okay, I've found the problem, an infinite loop is being executed When I call
[level1 mapping]. I added an NSLog statement to the Level1 method mapping to see the console filling up with NSLogs.

This infinite loop is only created when I try to init the Construct *constr object...

Construct.mm

-(id) init
{
    if( (self=[super init])) {
        //ALLOCS
        _Eggs    = [[NSMutableArray alloc] init];
        _Block1s = [[NSMutableArray alloc] init];
        _Block2s = [[NSMutableArray alloc] init];

        //Begin Mapping of environment
        Level1 *level1 = [[[Level1 alloc] init] autorelease];
        [level1 mapping];
         }
   return self;
}

Level1.mm

@implementation Level1

-(void) mapping{

    NSLog (@"Mapping");
    Construct *constr = [[Construct alloc]init];
    return;
}

@end  

Console:

2011-03-28 09:43:24.980 Construct[14661:207] Mapping  
2011-03-28 09:43:24.981 Construct[14661:207] Mapping  
2011-03-28 09:43:24.982 Construct[14661:207] Mapping  
2011-03-28 09:43:24.982 Construct[14661:207] Mapping  
2011-03-28 09:43:24.983 Construct[14661:207] Mapping  
2011-03-28 09:43:24.983 Construct[14661:207] Mapping  
2011-03-28 09:43:24.984 Construct[14661:207] Mapping  
2011-03-28 09:43:24.985 Construct[14661:207] Mapping  
2011-03-28 09:43:24.985 Construct[14661:207] Mapping  
2011-03-28 09:43:24.986 Construct[14661:207] Mapping  
2011-03-28 09:43:24.986 Construct[14661:207] Mapping  
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜