Memory leak in a small code with dealloc
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;
@synth开发者_如何学JAVAesize surname;
@synthesize email;
@synthesize telephone;
@synthesize image;
- (void) dealloc
{
[name release];
[surname release];
[email release];
[telephone release];
[image release];
[super dealloc];
}
@end
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.
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.
精彩评论