views:

32

answers:

2

Hi All! Assist me please, is this code correct? I mean, do we really need a dealloc method in this class, and why do or don't we? Will be a memory leak if we don't use dealloc here? Thanx!

    #import <Foundation/Foundation.h>


@interface MyData : NSObject
{
    @private
    NSString *name;
    NSString *surname;
    NSString *email;
    NSString *telephone;
    UIImage *image;
}

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *surname;
@property (nonatomic, retain) NSString *email;
@property (nonatomic, retain) NSString *telephone;
@property (nonatomic, retain) UIImage *image;
@end

#import "MyData.h"


@implementation MyData

@synthesize name;
@synthesize surname;
@synthesize email;
@synthesize telephone;
@synthesize image;

- (void) dealloc
{
    [name release];
    [surname release];
    [email release];
    [telephone release];
    [image release];    

    [super dealloc];    
}
@end
+2  A: 

The code is correct, yes there would be a memory leak if you did not have the dealloc.

If you set surname, or email, the string is retained. The MyData instance could then be free'd and without the dealloc the surname, or email string would still be hanging around but now you have no way to reference it - a leak.

mustISignUp
+1  A: 

Please specify your question again.Are you only going to declare the objects ? if u r doing this you no need to dealloc them. If you are going to declare and alloc them memory in any part of class u will need dealloc method, to free memory.

so be careful about memory management, because it provides memory leaks if you are not releasing them and if you are going to dealloc or release any object without allocing memory it will give u application crash.

singhSan
yes, I'm allocating memory for an object.
yozhik
Then you should use dealloc method to release memory occupied by objects. It will be helpful to prevent memory leaks. You are in right direction in your code.
singhSan