0

我创建了处理按钮的新类。它什么都不做(只是为了测试,方法很干净)。它在启动后被释放,当我单击按钮时,应用程序因 EXC_BAD_ACCESS 而崩溃。这是我的 .h 类文件:

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

实现文件:

@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

控制台列表(NSZombieEnabled 已打开):

[11724:207] * -[pagechanger performSelector:withObject:withObject:]: 消息发送到已释放实例 0x4b35900

这是“info malloc-history address ”返回的内容:

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

我正在尝试修改 Apple 的代码(ZoomingPDFViewer)。我找不到在那里添加新按钮的任何方法......所以我以编程方式创建了一个新窗口并将子视图添加到其中。但是我不知道这段代码是否有帮助......

    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];

您可以在此处获取原始代码

在没有 EXC_BAD_ACCESS 的情况下如何让我的方法正常工作?

4

1 回答 1

1

解决方案归结为:

在这种情况下,什么负责管理pagechanger实例的生命周期?使用目标/动作时,调用者不会保留目标。pagechanger因此,当目标注册到调用者时,您需要一些东西来保存引用。

=====

就细节而言:没有足够的相关代码发布来开始诊断您的特定问题的详细信息。

于 2011-02-06T04:47:29.767 回答