iOS - 子视图在恢复时崩溃

发布于 2024-11-03 04:39:10 字数 6629 浏览 6 评论 0原文

我有几个由导航控制器控制的子视图。其中之一在简历上崩溃了。它是一个通过推进一系列图像和平移手势控制器将模型“旋转”360 度(想想地球)的类。

一切都很好,直到我按下那个讨厌的主页按钮。当我按下主页按钮时,直到图像在 UIView 上设置后才会崩溃......至少在大多数情况下是这样。有时它会改变一次,然后在第二次改变时崩溃,显然只是为了让我的生活更有趣。

您能对此提供任何见解都会很棒。

提前致谢。

//  RotationController.m
//

#import "RotationController.h"

@implementation RotationController

@synthesize myRotationView;
@synthesize imageName;
@synthesize scheme;
int maxFrame;
int currentX;

- (void)viewDidLoad {
    [super viewDidLoad];

    //self.title = @"Cutaway View";

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPiece:)];
    [myRotationView addGestureRecognizer:panGesture];
    [panGesture release];
}

-(void)setup:(int) currentNode{

    if (currentNode <= 2)
    {
        currentX = 0;
        maxFrame = 199;
        scheme = @"Trailer1Rotation_";
        self.title = @"Trailer One Cutaway View";
        [self.myRotationView setImage:[UIImage imageNamed:@"Trailer1Rotation_000.jpg"]];
    }
    else if (currentNode > 2 && currentNode <= 5)
    {
        currentX = 0;
        maxFrame = 199;
        scheme = @"spinTrailer2_00";
        self.title = @"Trailer Two Cutaway View";
        [self.myRotationView setImage:[UIImage imageNamed:@"spinTrailer2_00000.jpg"]];
    }
    else if (currentNode > 5 && currentNode <= 8)
    {
        currentX = 0;
        maxFrame = 199;
        scheme = @"spinTrailer3_00";
        self.title = @"Trailer Three Cutaway View";
        [self.myRotationView setImage:[UIImage imageNamed:@"spinTrailer3_00000.jpg"]];
    }
    else if (currentNode > 8)
    {
        currentX = 0;
        maxFrame = 199;
        scheme = @"spinTrailer4_00";
        self.title = @"Trailer Four Cutaway View";
        [self.myRotationView setImage:[UIImage imageNamed:@"spinTrailer4_00000.jpg"]];
    }
}

-(void)viewWillAppear:(BOOL)animated {

    self.myRotationView.userInteractionEnabled = YES;
}

-(void)applicationDidEnterBackground:(UIApplication *)application
{
    NSLog(@"Test - DidEnterBackGround");
}

-(void)applicationWillResignActive
{
    NSLog(@"Test - WillResignActive");
}

- (void)applicationDidBecomeActive
{
    NSLog(@"Test - DidBecomeActive");

    [self.myRotationView setImage:[UIImage imageNamed:@"Trailer1Rotation_000.jpg"]];
}

-(void)panPiece:(UIPanGestureRecognizer *) gesture{

    switch(gesture.state) {
        case UIGestureRecognizerStateChanged: {
            //CGPoint center = gesture.view.center;
            CGPoint translation = [gesture translationInView:gesture.view];

//          center = CGPointMake((center.x + translation.x),
//                               (center.y + translation.y));
//          gesture.view.center = center;
//          [gesture setTranslation:CGPointZero inView:gesture.view];

            NSLog(@"Changed");
            NSLog(@"%f", translation.x);
            NSLog(@"%f", translation.y);

            int changeX = (int)translation.x / 20;

            if (changeX == 0)
                changeX = 1;

            currentX = (int)currentX + (int)changeX;

            while (currentX > 174) {
                currentX = currentX - 174;
            }

            while (currentX < 0) {
                currentX = currentX + 174;
            }

            if (currentX < 10) {
                imageName = [NSString stringWithFormat:@"%@00%i.jpg", scheme, currentX];
                NSLog(@"%@", imageName);
            } else if (currentX < 100) {
                imageName = [NSString stringWithFormat:@"%@0%i.jpg", scheme, currentX];
                NSLog(@"%@", imageName);
            } else {
                imageName = [NSString stringWithFormat:@"%@%i.jpg", scheme, currentX];
                NSLog(@"%@", imageName);
            }

            //NSLog(@"%@", myRotationView.image);
            [self.myRotationView setImage:[[UIImage imageNamed:imageName]autorelease]];
            //NSLog(@"%@", myRotationView.image);
        }
        case UIGestureRecognizerStateBegan: {
            [gesture setTranslation:CGPointZero inView:gesture.view];

            NSLog(@"Began");

            break;
        }
        case UIGestureRecognizerStateEnded: {
            //CGPoint velocity = [gesture velocityInView:self.view];
            //The user lifted their fingers. Optionally use the velocity to contain rotating the globe automatically

            NSLog(@"Ended");

            break;
        }
        default: {
            //Something else happened. Do any cleanup you need to.
            NSLog(@"Something else happened.");
        }
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
}
*/

- (void)dealloc {
    [super dealloc];


    NSLog(@"Rotation Controller - DeAlloc");
    [myRotationView release];
    [scheme release];
    [imageName release];
}

@end

反向跟踪:

2011-04-21 14:38:39.442 MCIT[52259:207] Test - DidBecomeActive
Program received signal:  “EXC_BAD_ACCESS”.
(gdb) backtrace
#0  0x01326a78 in objc_msgSend ()
#1  0x0633d540 in ?? ()
#2  0x000138b2 in -[RotationController applicationDidBecomeActive] (self=0x6607dc0, _cmd=0x17540) at /Users/jacob/Documents/iOS Dev/MCIT/Classes/RotationController.m:83
#3  0x00002a1c in -[MCITAppDelegate applicationDidBecomeActive:] (self=0x6603f30, _cmd=0x6e25a7, application=0x6300700) at /Users/jacob/Documents/iOS Dev/MCIT/Classes/MCITAppDelegate.m:100
#4  0x002d2542 in -[UIApplication _setActivated:] ()
#5  0x002dfe18 in -[UIApplication _handleApplicationResumeEvent:] ()
#6  0x002df7d4 in -[UIApplication handleEvent:withNewEvent:] ()
#7  0x002d7202 in -[UIApplication sendEvent:] ()
#8  0x002dc732 in _UIApplicationHandleEvent ()
#9  0x01afaa36 in PurpleEventCallback ()
#10 0x01afaabd in PurpleEventSignalCallback ()
#11 0x011a601f in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
#12 0x0110428b in __CFRunLoopDoSources0 ()
#13 0x01103786 in __CFRunLoopRun ()
#14 0x01103240 in CFRunLoopRunSpecific ()
#15 0x01103161 in CFRunLoopRunInMode ()
#16 0x01af9268 in GSEventRunModal ()
#17 0x01af932d in GSEventRun ()
#18 0x002e042e in UIApplicationMain ()
#19 0x00001f0e in main (argc=1, argv=0xbffff070) at /Users/jacob/Documents/iOS Dev/MCIT/main.m:14

当图像设置时,我对测试代码的思考失败了。

- (void)applicationDidBecomeActive
{
    NSLog(@"Test - DidBecomeActive");

    [self.myRotationView setImage:[UIImage imageNamed:@"Trailer1Rotation_000.jpg"]];
}

I have several subviews that are controlled by a navigation controller. One of them is crashing on resume. It is a class that "rotates" a model 360 degrees (think globe) through advancing a series of images and a pan gesture controller.

All is well and good until I hit that pesky home button. When I hit the home button I have until the image is set on the UIView before it crashes...Most of the time at least. Some times it will change once and crash on the second change apparently just to make my life more interesting.

Any insight you could shed on this would be great.

Thanks in Advance.

//  RotationController.m
//

#import "RotationController.h"

@implementation RotationController

@synthesize myRotationView;
@synthesize imageName;
@synthesize scheme;
int maxFrame;
int currentX;

- (void)viewDidLoad {
    [super viewDidLoad];

    //self.title = @"Cutaway View";

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPiece:)];
    [myRotationView addGestureRecognizer:panGesture];
    [panGesture release];
}

-(void)setup:(int) currentNode{

    if (currentNode <= 2)
    {
        currentX = 0;
        maxFrame = 199;
        scheme = @"Trailer1Rotation_";
        self.title = @"Trailer One Cutaway View";
        [self.myRotationView setImage:[UIImage imageNamed:@"Trailer1Rotation_000.jpg"]];
    }
    else if (currentNode > 2 && currentNode <= 5)
    {
        currentX = 0;
        maxFrame = 199;
        scheme = @"spinTrailer2_00";
        self.title = @"Trailer Two Cutaway View";
        [self.myRotationView setImage:[UIImage imageNamed:@"spinTrailer2_00000.jpg"]];
    }
    else if (currentNode > 5 && currentNode <= 8)
    {
        currentX = 0;
        maxFrame = 199;
        scheme = @"spinTrailer3_00";
        self.title = @"Trailer Three Cutaway View";
        [self.myRotationView setImage:[UIImage imageNamed:@"spinTrailer3_00000.jpg"]];
    }
    else if (currentNode > 8)
    {
        currentX = 0;
        maxFrame = 199;
        scheme = @"spinTrailer4_00";
        self.title = @"Trailer Four Cutaway View";
        [self.myRotationView setImage:[UIImage imageNamed:@"spinTrailer4_00000.jpg"]];
    }
}

-(void)viewWillAppear:(BOOL)animated {

    self.myRotationView.userInteractionEnabled = YES;
}

-(void)applicationDidEnterBackground:(UIApplication *)application
{
    NSLog(@"Test - DidEnterBackGround");
}

-(void)applicationWillResignActive
{
    NSLog(@"Test - WillResignActive");
}

- (void)applicationDidBecomeActive
{
    NSLog(@"Test - DidBecomeActive");

    [self.myRotationView setImage:[UIImage imageNamed:@"Trailer1Rotation_000.jpg"]];
}

-(void)panPiece:(UIPanGestureRecognizer *) gesture{

    switch(gesture.state) {
        case UIGestureRecognizerStateChanged: {
            //CGPoint center = gesture.view.center;
            CGPoint translation = [gesture translationInView:gesture.view];

//          center = CGPointMake((center.x + translation.x),
//                               (center.y + translation.y));
//          gesture.view.center = center;
//          [gesture setTranslation:CGPointZero inView:gesture.view];

            NSLog(@"Changed");
            NSLog(@"%f", translation.x);
            NSLog(@"%f", translation.y);

            int changeX = (int)translation.x / 20;

            if (changeX == 0)
                changeX = 1;

            currentX = (int)currentX + (int)changeX;

            while (currentX > 174) {
                currentX = currentX - 174;
            }

            while (currentX < 0) {
                currentX = currentX + 174;
            }

            if (currentX < 10) {
                imageName = [NSString stringWithFormat:@"%@00%i.jpg", scheme, currentX];
                NSLog(@"%@", imageName);
            } else if (currentX < 100) {
                imageName = [NSString stringWithFormat:@"%@0%i.jpg", scheme, currentX];
                NSLog(@"%@", imageName);
            } else {
                imageName = [NSString stringWithFormat:@"%@%i.jpg", scheme, currentX];
                NSLog(@"%@", imageName);
            }

            //NSLog(@"%@", myRotationView.image);
            [self.myRotationView setImage:[[UIImage imageNamed:imageName]autorelease]];
            //NSLog(@"%@", myRotationView.image);
        }
        case UIGestureRecognizerStateBegan: {
            [gesture setTranslation:CGPointZero inView:gesture.view];

            NSLog(@"Began");

            break;
        }
        case UIGestureRecognizerStateEnded: {
            //CGPoint velocity = [gesture velocityInView:self.view];
            //The user lifted their fingers. Optionally use the velocity to contain rotating the globe automatically

            NSLog(@"Ended");

            break;
        }
        default: {
            //Something else happened. Do any cleanup you need to.
            NSLog(@"Something else happened.");
        }
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
}
*/

- (void)dealloc {
    [super dealloc];


    NSLog(@"Rotation Controller - DeAlloc");
    [myRotationView release];
    [scheme release];
    [imageName release];
}

@end

The Back Trace:

2011-04-21 14:38:39.442 MCIT[52259:207] Test - DidBecomeActive
Program received signal:  “EXC_BAD_ACCESS”.
(gdb) backtrace
#0  0x01326a78 in objc_msgSend ()
#1  0x0633d540 in ?? ()
#2  0x000138b2 in -[RotationController applicationDidBecomeActive] (self=0x6607dc0, _cmd=0x17540) at /Users/jacob/Documents/iOS Dev/MCIT/Classes/RotationController.m:83
#3  0x00002a1c in -[MCITAppDelegate applicationDidBecomeActive:] (self=0x6603f30, _cmd=0x6e25a7, application=0x6300700) at /Users/jacob/Documents/iOS Dev/MCIT/Classes/MCITAppDelegate.m:100
#4  0x002d2542 in -[UIApplication _setActivated:] ()
#5  0x002dfe18 in -[UIApplication _handleApplicationResumeEvent:] ()
#6  0x002df7d4 in -[UIApplication handleEvent:withNewEvent:] ()
#7  0x002d7202 in -[UIApplication sendEvent:] ()
#8  0x002dc732 in _UIApplicationHandleEvent ()
#9  0x01afaa36 in PurpleEventCallback ()
#10 0x01afaabd in PurpleEventSignalCallback ()
#11 0x011a601f in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
#12 0x0110428b in __CFRunLoopDoSources0 ()
#13 0x01103786 in __CFRunLoopRun ()
#14 0x01103240 in CFRunLoopRunSpecific ()
#15 0x01103161 in CFRunLoopRunInMode ()
#16 0x01af9268 in GSEventRunModal ()
#17 0x01af932d in GSEventRun ()
#18 0x002e042e in UIApplicationMain ()
#19 0x00001f0e in main (argc=1, argv=0xbffff070) at /Users/jacob/Documents/iOS Dev/MCIT/main.m:14

it's failing where I thought on my testing code... when the image is set.

- (void)applicationDidBecomeActive
{
    NSLog(@"Test - DidBecomeActive");

    [self.myRotationView setImage:[UIImage imageNamed:@"Trailer1Rotation_000.jpg"]];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美胚控场 2024-11-10 04:39:10

我发现调试这些问题的最简单方法是为可执行文件打开 NSZombieEnabled。

在 Xcode 中展开项目的“可执行文件”项,双击它。现在,单击左下角的加号 ('+') 图标并添加一个值为 YESNSZombieEnabled 变量。

您应该仅在开发期间使用此设置,否则您的应用程序将永远无法正确释放内存。您不想在启用此设置的情况下尝试交付您的应用程序。

我通过向 UIApplicationDelegate 类添加一些测试代码来检查这一点:

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
  if (getenv("NSZombieEnabled"))
  {
    NSLog(@"WARNING! NSZombieEnabled enabled!");
  }

  if (getenv("NSAutoreleaseFreedObjectCheckEnabled"))
  {
    NSLog(@"WARNING! NSAutoreleaseFreedObjectCheckEnabled enabled!");
  }

I find the easiest way to debug these problems is to turn on NSZombieEnabled for your executable.

Expand the 'Executable' item for your project in Xcode, double click on it. Now click the plus ('+') icon at the bottom left and add an NSZombieEnabled variable with value YES.

You should only use this setting during development, otherwise memory won't ever get properly freed by your app. You don't want to try and deliver your app with this setting turned on.

I check this by adding some test code to my UIApplicationDelegate class:

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
  if (getenv("NSZombieEnabled"))
  {
    NSLog(@"WARNING! NSZombieEnabled enabled!");
  }

  if (getenv("NSAutoreleaseFreedObjectCheckEnabled"))
  {
    NSLog(@"WARNING! NSAutoreleaseFreedObjectCheckEnabled enabled!");
  }
飘落散花 2024-11-10 04:39:10

首先,更改您的 dealloc 方法,使其

[super dealloc]

成为最后一行,而不是第一行。它应该始终是您在 dealloc 中执行的最后一件事。

myRotationView 是在 Nib 中创建的吗?你是在 viewDidUnload 中释放它吗?

最后,整个堆栈跟踪将告诉我们 EXC_BAD_ACCESS 发生在哪一行。

First, change your dealloc method so

[super dealloc]

is the last line, not the first line. It should always be the last thing you do in dealloc.

Is myRotationView created in a Nib? Are you releasing it in viewDidUnload?

And finally, the whole stack trace will tell us which line the EXC_BAD_ACCESS is happening on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文