Custom class becomes deallocated just after start. EXC_BAD_ACCESS

715 views Asked by At

I have created new class which handles button. It does nothing (just for test, method is clean). It is deallocated just after launch and when I click button app crashes with EXC_BAD_ACCESS. This is my .h file of the class:

@interface pagechanger : NSObject {
}
- (IBAction)gonext:(id)sender;
@end

Implementation file:

@implementation pagechanger
- (IBAction)gonext:(id)sender{
// blablabla, some code which is even hasn't been read, breakpoints were not touched.
}

-(void)dealloc{
    NSLog(@" pc released");
    [super dealloc];
}
@end

Console listing (NSZombieEnabled is turned on):

[11724:207] * -[pagechanger performSelector:withObject:withObject:]: message sent to deallocated instance 0x4b35900

This is what "info malloc-history address" returns:

Alloc: Block address: 0x04b35900 length: 16
Stack - pthread: 0xa0180540 number of frames: 36
    0: 0x9154e103 in malloc_zone_calloc
    1: 0x9154e05a in calloc
    2: 0x105cd0f in _internal_class_createInstanceFromZone
    3: 0x105f87d in class_createInstance
    4: 0xe2cff8 in +[NSObject(NSObject) allocWithZone:]
    5: 0xe2cdfa in +[NSObject(NSObject) alloc]
    6: 0x4ab205 in -[UIClassSwapper initWithCoder:]
    7: 0x5919e4 in UINibDecoderDecodeObjectForValue
    8: 0x592693 in -[UINibDecoder decodeObjectForKey:]
    9: 0x4aaf43 in -[UIRuntimeConnection initWithCoder:]
   10: 0x4ab4b1 in -[UIRuntimeEventConnection initWithCoder:]
   11: 0x5919e4 in UINibDecoderDecodeObjectForValue
   12: 0x5912dc in UINibDecoderDecodeObjectForValue
   13: 0x592693 in -[UINibDecoder decodeObjectForKey:]
   14: 0x4aa200 in -[UINib instantiateWithOwner:options:]
   15: 0x4ac081 in -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:]
   16: 0x364a94 in -[UIViewController _loadViewFromNibNamed:bundle:]
   17: 0x362709 in -[UIViewController loadView]
   18: 0x2607 in -[ZoomingPDFViewerViewController loadView] at /Users/ruzard/Desktop/ZoomingPDFViewer/Classes/ZoomingPDFViewerViewController.m:92
   19: 0x3625e3 in -[UIViewController view]
   20: 0x2231 in -[ZoomingPDFViewerAppDelegate application:didFinishLaunchingWithOptions:] at /Users/ruzard/Desktop/ZoomingPDFViewer/Classes/ZoomingPDFViewerAppDelegate.m:65
   21: 0x2b51fa in -[UIApplication _callInitializationDelegatesForURL:payload:suspended:]
   22: 0x2b755e in -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:]
   23: 0x2c1db2 in -[UIApplication handleEvent:withNewEvent:]
   24: 0x2ba202 in -[UIApplication sendEvent:]
   25: 0x2bf732 in _UIApplicationHandleEvent
   26: 0x183ea36 in PurpleEventCallback
   27: 0xeea064 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__
   28: 0xe4a6f7 in __CFRunLoopDoSource1
   29: 0xe47983 in __CFRunLoopRun
   30: 0xe47240 in CFRunLoopRunSpecific
   31: 0xe47161 in CFRunLoopRunInMode
   32: 0x2b6fa8 in -[UIApplication _run]
   33: 0x2c342e in UIApplicationMain
   34: 0x21c4 in main at /Users/ruzard/Desktop/ZoomingPDFViewer/main.m:54
   35: 0x2155 in start

I am trying to modificate Apple's code (ZoomingPDFViewer). I couldn't find any way to add new buttons there... so I made a new window programmatically and added subview into it. However I don't know whether this code can help or not...

    UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window) {
    window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 980, 500, 500)];
    window.hidden = NO;
    [window becomeKeyWindow];
    window.windowLevel = UIWindowLevelStatusBar;
}
[window addSubview:myheader];

You can get orginal code here.

How can I get my method working without EXC_BAD_ACCESS?

1

There are 1 answers

0
justin On BEST ANSWER

the solution comes down to this:

what is responsible for managing the lifetime of the pagechanger instance in this case? when using target/action, the target is not retained by the invoker. therefore, you'll need something to hold a reference of the pagechanger while the target is registered with the invoker.

=====

as far as specifics: there's not enough relevant code posted to begin diagnosing the details of your specific issue.