0

请帮助我,这个代码是否正确?我的意思是,我们真的需要在这个类中使用 dealloc 方法吗,为什么需要或不需要呢?如果我们在这里不使用 dealloc 会不会是内存泄漏?谢谢!

    #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
4

2 回答 2

3

代码是正确的,是的,如果你没有dealloc,就会有内存泄漏。

如果您设置姓氏或电子邮件,则保留该字符串。然后可以释放 MyData 实例,并且在没有 dealloc 的情况下,姓氏或电子邮件字符串仍然存在,但现在您无法引用它 - 泄漏。

于 2010-10-29T09:38:38.993 回答
0

请再次说明您的问题。您是否只声明对象?如果您这样做,您无需释放它们。如果您要在类的任何部分声明并分配它们的内存,您将需要 dealloc 方法来释放内存。

所以要小心内存管理,因为如果你不释放它们,它会提供内存泄漏,如果你要释放或释放任何对象而不分配内存,它会让你的应用程序崩溃。

于 2010-10-29T10:32:13.177 回答