Xcode 崩溃级别=1 + 2 无潜在泄漏(可能是因为动画)
首先我是一个n00b。经过长时间的尝试和研究,我决定寻求一些外部帮助。 我的项目: 我为孩子们做了一本书。在我分析了代码之后,我消除了所有潜在的泄漏,但仍然出现了 Level=1+2 崩溃。通过测试我的应用程序,我发现我的动画可能是问题所在,因为在翻阅这本书并观看大约 30 个动画后,它崩溃了。
我是不是忘记发布什么东西了? 也许你看到了我看不到的东西。 这是我的代码
- (void)addButton1 {
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(174, 100, 421, 250)];
[button1 setBackgroundImage:[UIImage imageNamed:@"button.png"]
forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(buttonPressed1)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
}
- (void)animieren1:(UIImageView *)image {
[self animationZustand1];
[UIView commitAnimations];
}
- (void)buttonPressed1 {
[self animieren1:self.animation1];
}
- (void)normalZustand2 {
[self.animation2 setImage:[UIImage imageNamed:@"muller3s.png"]];
}
- (void)initAnimation2 {
animation2 = [[UIImageView alloc] initWithFrame:
CGRectMake(173, 550, 422, 262)];
[self normalZustand2];
self.animation2.opaque = YES;
[self.view addSubview:self.animation2];
}
- (void)animationZustand2 {
NSArray *imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"muller3s.png"],
[UIImage imageNamed:@"muller4s.png"],
nil];
self.animation2.animationImages = imageArray;
self.animation2.animationDuration = 2.1;
animation2.animationRepeatCount = 1;
[self.animation2 startAnimating];
[self normalZustand2];
[imageArray release];
NSString *path = [[NSBundle mainBundle] pathForResource:@"riesel" ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
fileURLWithPath:path] error:NULL];
theAudio.volume = 0.1;
self.audioPlayer3 = theAudio;
[theAudio play];
[theAudio release];
}
有什么想法吗? 如果有人能帮助我,那就太酷了! 预先感谢普朗基
First of all i am a n00b. After long time of trying and research i decided to get some external help.
My Project:
i made a book for children. After i analyzed the code I got rid of all potential leaks, but still i have a Level=1+2 crash. with testing my app i figured out that my animations could be the probleme because after flipping through the book and watching around 30 animations it crashes.
Did i forget to release something?
maybe you see something i don´t see.
Here is my Code
- (void)addButton1 {
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(174, 100, 421, 250)];
[button1 setBackgroundImage:[UIImage imageNamed:@"button.png"]
forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(buttonPressed1)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
}
- (void)animieren1:(UIImageView *)image {
[self animationZustand1];
[UIView commitAnimations];
}
- (void)buttonPressed1 {
[self animieren1:self.animation1];
}
- (void)normalZustand2 {
[self.animation2 setImage:[UIImage imageNamed:@"muller3s.png"]];
}
- (void)initAnimation2 {
animation2 = [[UIImageView alloc] initWithFrame:
CGRectMake(173, 550, 422, 262)];
[self normalZustand2];
self.animation2.opaque = YES;
[self.view addSubview:self.animation2];
}
- (void)animationZustand2 {
NSArray *imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"muller3s.png"],
[UIImage imageNamed:@"muller4s.png"],
nil];
self.animation2.animationImages = imageArray;
self.animation2.animationDuration = 2.1;
animation2.animationRepeatCount = 1;
[self.animation2 startAnimating];
[self normalZustand2];
[imageArray release];
NSString *path = [[NSBundle mainBundle] pathForResource:@"riesel" ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL
fileURLWithPath:path] error:NULL];
theAudio.volume = 0.1;
self.audioPlayer3 = theAudio;
[theAudio play];
[theAudio release];
}
Any ideas?
It would be really cool if anyone could help me!
Thanks in advance Planky
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您不清楚什么是“1 级”崩溃。这些级别指的是内存警告 - 当您即将耗尽应用程序的内存分配时,您的应用程序会收到来自系统的通知。
您的应用程序应该通过清除不再使用的对象来响应这些通知 - 如果您不这样做,您将收到更多警告,直到您的应用程序被系统终止,因为根本没有更多内存可供您使用。
虽然内存警告可能是由泄漏引起的,但这通常不是根本问题。在绝大多数的情况下,它们是由于尝试同时将太多原始资源加载到内存中而引起的。
您需要仔细检查您的代码和结构,以找出解决此问题的最佳方法。也许当前您只是同时将书的所有 30 页加载到内存中:您需要考虑在需要时加载资源。如果某个资源对用户不可见,也许您可以将其从内存中删除,并在需要时重新加载。
在图形丰富的应用程序中,记住文件大小不等于图像在内存中占用的大小也很重要。假设您有一个 iPad 屏幕大小 (1024x768) 的 PNG 文件。当加载到内存中时,该图像将占用超过 3 MB 的内存。所有 iOS 设备都受到内存限制,有些设备比其他设备更严重:在第一代 iPad 上,单个图像可能会占用总内存分配的 5%(您的应用程序可以访问的确切数量会有所不同,并且取决于许多因素:这就是为什么您当您接近限制时收到通知)。
内存警告是比较“友好”的崩溃之一,因为 iOS 会在发生之前尝试向您的应用发出警告。因此,在控制器中监听这些通知并做出相应的响应非常重要。
It sounds like you're unclear what a 'level 1' crash is. The levels refer to memory warnings - your application receives notifications from the system when you're close to exhausting your app's memory allocation.
Your app should respond to these notifications by clearing out objects it's not using any more - if you don't, you'll receive more warnings, until your app is terminated by the system since there's simply no more memory to provide you.
Whilst memory warnings can be caused be leaks, this is typically not the root problem. In the overwhelming number of cases they are instead caused by trying to load too many raw assets into memory at the same time.
You will need to take a hard look at your code and structure to figure out the best way to cope with this. Perhaps currently you're just loading all 30 pages of your book into memory at the same time: you need to think about loading in resources as they are needed. If an asset isn't visible to the user, perhaps you could remove it from memory and reload it when needed.
It's also important in graphically rich apps to remember that file size doesn't equal the size the image takes up in memory. Suppose you have a PNG file the size of the iPad screen (1024x768). When loaded into memory, this image will take up over 3 megabytes of memory. All iOS devices are memory constrained, some more than others: on a first generation iPad that single image could have used up 5% of your total memory allocation (the exact amount your app has access to varies and depends on many factors: hence why you receive notifications when you're nearing the limit).
Memory warnings are one of the more 'friendly' crashes, because iOS will try to tell warn your app before they happen. So it's very important you listen out for these notifications in your controllers, and respond accordingly.