从阵列中删除时访问不良

时间:2012-05-12 04:03:33

标签: objective-c xcode

所以我的代码编译,当我在xcode中运行我的iphone应用程序时崩溃喷出大量内存错误抱怨访问不良,我不知道这意味着什么。我想要做的就是删除我创建的实验室我的超级视图使用数组中的计数器,结果比我想象的要难得多。我非常擅长对象c和iphone开发,我们将非常感谢任何帮助。

#import "FlipsideViewController.h"

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> 
{
    /*This stuff creates a timer */
    IBOutlet UILabel *opponentsBlue;
    NSTimer *timer;
    int redBlue;
    int count;
    /*Stuff for making a label creator */
    CGPoint startPoint;
    int xStuff, yStuff;

    /*array for storing wards*/
    NSMutableArray *wardArray;

}

@property CGPoint startPoint;

- (IBAction)startRedBlue:(id)sender;

- (IBAction)removeWard:(id)
sender;
- (void)countdown;
-(id)init;

@end

#import "MainViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

@synthesize startPoint;

- (void)countdown 
{
    if (redBlue < 2) {

        [timer invalidate];
        timer = nil;
    }
    redBlue -= 1;
    opponentsBlue.text = [NSString stringWithFormat:@"%i", redBlue];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *theTouch = [touches anyObject];
    startPoint = [theTouch locationInView:self.view];

}

-(id)init{
    int count = 0;
    return self;
}

- (IBAction)startRedBlue:(id)sender 
{
    UIButton *wardButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    wardButton.frame = CGRectMake((startPoint.x - 5), (startPoint.y - 5), 10, 10);
    [wardButton setTitle:@"180" forState:UIControlStateNormal];
    //add targets and actions
    /*[wardButton addTarget:self action:@selector() forControlEvents:<#(UIControlEvents)#>*/
    //add to a view
    [self.view addSubview:wardButton];

    if (!wardArray) {
        wardArray = [NSMutableArray array];
    } 
    if (wardArray){
        [self->wardArray addObject: wardButton];
        count++;
    }

    NSLog(@"This elemnt = %@", wardArray);


}
- (IBAction)removeWard:(id)sender 
{

    NSLog(@"The count is %@", count);
    [[wardArray objectAtIndex:count] removeFromSuperview];

    count--;

    NSLog(@"This elemnt = %@", wardArray);


}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

#pragma mark - Flipside View

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
    [self dismissModalViewControllerAnimated:YES];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showAlternate"]) {
        [[segue destinationViewController] setDelegate:self];
    }
}

@end

1 个答案:

答案 0 :(得分:0)

我猜测这不是ARC(自动引用计数)。

wardArray未被保留,因此在创建后很快就会被释放。

您可以尝试将[NSMutableArray array]替换为[[NSMutableArray alloc] init]。这样它只会泄漏记忆而不是留下僵尸。

您也可以阅读Objective-C内存管理。