自定义类在启动后立即被释放。 EXC_BAD_ACCESS
我创建了处理按钮的新类。它什么也不做(只是为了测试,方法很干净)。它在启动后立即被释放,当我单击按钮时,应用程序崩溃并显示 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] * -[换页器 执行选择器: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 的情况下让我的方法正常工作?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案归结为:
在这种情况下,什么负责管理 pagechanger 实例的生命周期?当使用目标/操作时,调用者不会保留目标。因此,当目标向调用者注册时,您需要一些东西来保存 pagechanger 的引用。
=====
就具体情况而言:没有发布足够的相关代码来开始诊断您的具体问题的详细信息。
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 thepagechanger
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.