Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

iphone - memory management and class member variable

I have the following code:

  @interface Reservation : RKObject {
    NSNumber * _uid;
    NSNumber * _did;
    NSNumber * _rid;
    NSString* _origin;
    NSString* _destination;
    NSString* _time_range;
    NSDate * _start_date;
    NSDate * _end_date;
}

@property (nonatomic, retain) NSDate * start_date;
@property (nonatomic, retain) NSDate * end_date;
@property (nonatomic, retain) NSNumber * uid;
@property (nonatomic, retain) NSNumber * did;
@property (nonatomic, retain) NSNumber * rid;
@property (nonatomic, retain) NSString * time_range;
@property (nonatomic, retain) NSString * origin;
@property (nonatomic, retain) NSString * destination;
@end

#import "Reservation.h"

@implementation Reservation
@synthesize time_range= _time_range;
@synthesize origin=_origin;
@synthesize destination=_destination;
@synthesize uid=_uid;
@synthesize did=_did;
@synthesize rid=_rid;
@synthesize start_date=_start_date;
@synthesize end_date=_end_date;


+ (NSDictionary*) elementToPropertyMappings {
    return [NSDictionary dictionaryWithKeysAndObjects:
            @"TIME_RANGE", @"time_range",
            @"ORIGIN_ADDRESS", @"origin",
            @"DESTINATION_ADDRESS", @"destination",
            @"UID", @"uid",
            @"DID", @"did",
            @"RID", @"rid",
            @"START_DATETIME", @"start_date",
            @"END_DATETIME", @"end_date",
            nil]; 
}


/*
+ (NSDictionary*)elementToRelationshipMappings {
    return [NSDictionary dictionaryWithKeysAndObjects:
            @"NSDate", @"start_date",
            @"NSDate", @"end_date",
            nil];
}
 */

- (void)dealloc
{
    [_start_date release];
    [_end_date release];
    [_uid release];
    [_did release];
    [_rid release];
    [_origin release];
    [_destination release];
    [_time_range release];
    //[_deal release];
    //[_route release];
    [super dealloc];
}

@end

@interface ReservationViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, RKObjectLoaderDelegate>{
    Reservation * myres;
}

@property (nonatomic, retain) Reservation * myres;

#import "ReservationViewController.h"


@implementation ReservationViewController
@synthesize myres;

- (IBAction)makeReservation:(id)sender
{
    self.myres = [[[Reservation alloc] init] autorelease];  
    myres.start_date = [df dateFromString:[NSString stringWithFormat:@"%@ %@", [self.data objectForKey:@"date"], [self.data objectForKey:@"start_time"]]];
    myres.end_date = [df dateFromString:[NSString stringWithFormat:@"%@ %@", [self.data objectForKey:@"date"], [self.data objectForKey:@"end_time"]]];
    NSLog(@"date is %@", myres.start_date);
}

- (void)objectLoader: (RKObjectLoader*) objectLoader didLoadObjects:(NSArray *)objects {
        Interval * res_int = [[Interval alloc] init];
        res_int.date1 = myres.start_date; //why is it null here
        res_int.date2 = myres.end_date; //why is it null here
        res_int.rid =  [[objects objectAtIndex:0] rid];
        [[LocationDictionary sharedLocationDictionary] addLocationtoDictionary:[self.data objectForKey:@"route"] withKey:res_int];

}

The question is why is res_int.date1 null when I tried printing it out? When I print myres.start_date inside the method makeReservation, it prints appropriately Can someone explain this to me and how to fix it?

UPDATE:

I have done the following at my setter:

-(void) setStart_date:(NSDate *) boo
{
     NSAssert(boo != nil, @"why is this nil");
    _start_date = boo;    

}

at the end the stack trace I got is:

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'why is this nil'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x019ac5a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x01b00313 objc_exception_throw + 44
    2   CoreFoundation                      0x01964ef8 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x014013bb -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   Project                            0x00012d42 -[Reservation setStart_date:] + 194
    5   Foundation                          0x0137e5e5 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
    6   Project                           0x0004c7b3 -[RKObjectMapper updateModel:ifNewPropertyValue:forPropertyNamed:] + 172
    7   Project                            0x0004cd55 -[RKObjectMapper setPropertiesOfModel:fromElements:] + 754
    8   Project                            0x0004d8f3 -[RKObjectMapper updateModel:fromElements:] + 50
    9   Project                            0x0004bb7a -[RKObjectMapper mapObject:fromDictionary:] + 201
    10  Project                            0x0004ba42 -[RKObjectMapper mapObject:fromString:] + 148
    11  Project                            0x0004927b -[RKObjectLoader processLoadModelsInBackground:] + 537
    12  Foundation                          0x01370cf4 -[NSThread main] + 81
    13  Foundation                          0x01370c80 __NSThread__main__ + 1387
    14  libSystem.B.dylib                   0x931a085d _pthread_start + 345
    15  libSystem.B.dylib                   0x931a06e2 thread_start + 34
)
terminate called after throwing an instance of 'NSException'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What is the calling sequence of makeReservation (from the IBAction) and objectLoader? Is it possible that objectLoader is called before makeReservation so myres hasn't been initialized yet?

What is the interface definition for the did property of Reservation? Your line:

myres.did = [[NSNumber alloc] initWithInt:[[[data objectForKey:@"discount"] did] intValue]];

If it defined as (nonatomic, retain) you may be retaining the NSNumber object without releasing it. If so, change the line to:

myres.did = [NSNumber numberWithInt:[[[data objectForKey:@"discount"] did] intValue]];

You should also uncomment your line:

//[res_int release]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...