Hi I run into EXC_BAD_ACCESS in my code,
I’m trying to run a method in class PurchaseTowerMenuLayer,
method is from class UnitDatabase:
PurchaseTowerMenuLayer.m:
delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
// getUnitDatabase returns UnitDatabase class from GameScene class
[[[delegate getGameScene] getUnitDatabase] createUnit:@"t01"]; //EXC_BAD_ACCESS happens here
UnitDatabase.m:
-(void)createUnit:(NSString *)unitID
{
typeString = [unitID substringWithRange:NSMakeRange(0,1)];
numberString = [unitID substringWithRange:NSMakeRange(1,2)];
Unit *unit;
if(unitID == @"t00")
[unit setUnitID:unitID];
etc...
}
What am I doing to get this error? How should it look like?
Thanks in advance!
Alright, first separate the call
[[[delegate getGameScene] getUnitDatabase] createUnit:@"t01"];for three lines such asThis’ll allow you to pinpoint which call emits the
EXC_BAD_ACCESSand you can collapse them back after if you prefer.After you identified which line emits the error, it’s usually a matter of finding where you haven’t called
[[MyClass alloc] init];. For example, ifgetGameSceneorgetUnitDatabasereturn objects that weren’talloc‘d/init‘d (ordelegatetoo for that matter), then you want to make sure to do that at least before you return them.I can’t help much more without seeing more code but these errors often go like this. Make sure you alloc and initialize the objects you keep as members of other objects.