简体   繁体   中英

Thread 1: Program received signal: “EXC_BAD_ACCESS”

I just have a simple code to practice Object C.. I am not sure why I can this "WARNING"? My code is below

#import <Foundation/Foundation.h>


@interface MyClass : NSObject {
@private
    NSDate *mdate;
}

@property (retain) NSDate *mdate;

@end

==================================

#import "MyClass.h"


@implementation MyClass

@synthesize mdate;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
        mdate = [[NSDate date] autorelease];
    }

    return self;
}

- (void)dealloc
{
    [super dealloc];
}

@end

=============================================

#import <Foundation/Foundation.h>
#import "MyClass.h"

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    MyClass *mclass = [[MyClass alloc]init];
    NSDate *myBirthday;
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"yyyy/MM/dd"];
    myBirthday=[dateFormat dateFromString:@"1990/09/02"];

    [mclass setMdate:myBirthday];
    NSLog(@"My Birthday is %@",[mclass mdate]);
    // insert code here...
    NSLog(@"Hello, World!");

    [mclass release];
    //[dateFormat release];

    [pool drain];
    return 0;
}

and in [pool drain] -> i got the message after I ran.

I am really newbie on Object C. Could someone please explain what I missed? I think this cause my memory management(?) btw, I was writing this for console.

Mdate is over released.

In general, your memory management is quite wrong. Read the "Cocoa memory management guide" as it explains the relatively simple rules clearly.

you should not release the object which are neither alloced or init by you.

mdate = [[NSDate date] autorelease]; //Wrong statement.

In your init function of MyClass , you should not call autorelease on the NSDate object, which you don't create, you get it from iOS framework and iOS own the responsibility to release it.

Here is the case of your mdate object overreleased .

mdate = [NSDate date]; //Correct statement.

Read the Apple Memory Management Programming Guide

If your.h file is defined like this:

#import <Foundation/Foundation.h>

@interface MyClass : NSObject {
@private
    NSDate *mdate;
}

@property (retain) NSDate *mdate;

@end

Then your.m file should look like this:

#import "MyClass.h"

@implementation MyClass

@synthesize mdate;

- (id)init
{
    self = [super init];
    if (self) {
        // mdate = [[NSDate date] autorelease];  WRONG
        // mdate = [NSDate date];  WRONG

        mdate = [[NSDate date] retain];  CORRECT
        // mdate = [[NSDate alloc] init];  CORRECT
        // mdate = [[[[NSDate alloc] init] autorelease] retain]; CORRECT (but weird)

        // self.mdate = [NSDate date];  CORRECT

    }
    return self;
}

- (void)dealloc
{
    [mdate release]; // NECESSARY
    [super dealloc];
}

@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM