1

So, i created an object that will just hold 6 variables.

#import <Foundation/Foundation.h>

@interface UserInfoData : NSObject 
{
    NSNumber *dealerID;
    NSNumber *growerID;
    NSNumber *UID;
    NSNumber *uLevel;
    NSString *fName;
    NSString *lName;
    NSString *state;
}

@property (nonatomic, retain) NSNumber *dealerID;
@property (nonatomic, retain) NSNumber *growerID;
@property (nonatomic, retain) NSNumber *UID;
@property (nonatomic, retain) NSNumber *uLevel;
@property (nonatomic, retain) NSString *fName;
@property (nonatomic, retain) NSString *lName;
@property (nonatomic, retain) NSString *state;

@end

#import "UserInfoData.h"

@implementation UserInfoData

@synthesize dealerID;
@synthesize growerID;
@synthesize UID;
@synthesize uLevel;
@synthesize fName;
@synthesize lName;
@synthesize state;

-(void) dealloc
{
    [dealerID release];
    [growerID release];
    [UID release];
    [uLevel release];
    [fName release];
    [lName release];
    [state release];

    [super dealloc];
}

@end

View Controller A

.h file:

@class UserInfoData;

.m file:

#import "UserInfoData.h"

iPhoneAppDelegate *dataCenter = (iPhoneAppDelegate *) [[UIApplication sharedApplication] delegate];


    UserInfoData *userInfoData = [[[UserInfoData alloc] init];

    userInfoData.dealerID = [NSNumber numberWithInt:[[userInfo objectForKey:@"dealerid"] intValue]];
    userInfoData.growerID = [NSNumber numberWithInt:[[userInfo objectForKey:@"growerid"] intValue]];
    userInfoData.UID = [NSNumber numberWithInt:[[userInfo objectForKey:@"uid"] intValue]];;
    userInfoData.uLevel = [NSNumber numberWithInt:[[userInfo objectForKey:@"ulevel"] intValue]];;
    userInfoData.fName = [userInfo objectForKey:@"f_name"];
    userInfoData.lName = [userInfo objectForKey:@"l_name"];
    userInfoData.state = [userInfo objectForKey:@"state_prov"];

    NSArray *userInfoArray = [NSArray arrayWithObjects:userInfoData, nil];

    dataCenter.userInfoList = userInfoArray;

    [userInfoData release];

i save the data to my AppDelegate which i use as a data center. i need this data in another view controller. i also repeat this process of initializing and releasing in 2 other functions.

View Controller B

.h file:

@class UserInfoData;

.m file:

#import "UserInfoData.h"
UserInfoData *userInfoData = [[[UserInfoData alloc] init] autorelease];
userInfoData = [dataCenter.userInfoList objectAtIndex:0];
//do stuff here
[userInfoData release];

by manually releasing the objects, this would cause issues when i tried to retrieve it from my datacenter in view controller B, i would get the wrong memory location and get the wrong data (which crashed the program).

when i switched and used only autorelease when initializing the object, it works fine. what am i missing about memory management? i do not have leaks, but i do have a bunch of "Incorrect decrement of the reference count of an object that is not owned at this point by the caller" which i can not track down (xcode does not tell me which object, the line its on, or give me a blue indicator on the line number in the code pane like it does for warnings and errors). could those be causing memory pointing to the wrong memory locations? but if that is happening, why would autoreleasing them fix it?

1 Answer 1

2
UserInfoData *userInfoData = [[[UserInfoData alloc] init] autorelease];

You're creating a new object here for no reason. You throw it away in the next line. You only need to alloc/init if you're creating a new object, not every time you declare a variable.

userInfoData = [dataCenter.userInfoList objectAtIndex:0];

You're throwing away the object you just created and assigning a different object to the variable. You're not retaining this different object, so you shouldn't release it.

//do stuff here
[userInfoData release];

You're releasing the different object, but you don't own it as you didn't retain it.

The correct code is

UserInfoData *userInfoData = [dataCenter.userInfoList objectAtIndex:0];

You're not taking ownership by retaining it, so you don't release it.

Sign up to request clarification or add additional context in comments.

3 Comments

you're completely right. i still don't fully understand what happening here though. i though creating the object with init'ing it created the variable then i had to assign something to the variable. akin to int b; b = 1;
@Log139: Simply declaring a variable creates the variable, just like you do with int b. It just doesn't contain an object at that point. allocing an object creates an object, not a variable.
so then you only ever need to alloc an object once? then is there any reason to alloc anything other then custom objects?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.