请帮我找到这段 iOS 代码中的两个漏洞
在我的 viewController.m
中,我有这样的代码:
self.movie = [[myMovie alloc]init];
self.movie.name = @"Iron man 2"; \\this line leaks
...
nameLbl = [[UILabel alloc] initWithFrame:CGRectMake(30, 20, 200, 20)]; \\this line leaks
nameLbl.backgroundColor = [UIColor clearColor];
在 viewController.h
中,我有这样的代码:
@interface ViewController : UIViewController
{
myMovie * movie;
UILabel * nameLbl;
}
@property (nonatomic, retain) myMovie * movie;
@property (nonatomic, retain) UILabel * nameLbl;
还有 myMovie.h:
{
NSString* name;
}
@property (nonatomic, retain) NSString* name;
myMovie.m:
#import "myMovie.h"
@implementation myMovie
@synthesize name, gross, desc;
-(void) dealloc
{
self.name = nil;
self.gross = nil;
self.desc = nil;
[super dealloc];
}
@end
当然这只是必要的代码。我不明白为什么它会泄漏。我不知道这是否是原因,但我的应用程序崩溃了。
In my viewController.m
I have this code:
self.movie = [[myMovie alloc]init];
self.movie.name = @"Iron man 2"; \\this line leaks
...
nameLbl = [[UILabel alloc] initWithFrame:CGRectMake(30, 20, 200, 20)]; \\this line leaks
nameLbl.backgroundColor = [UIColor clearColor];
In viewController.h
I have this code:
@interface ViewController : UIViewController
{
myMovie * movie;
UILabel * nameLbl;
}
@property (nonatomic, retain) myMovie * movie;
@property (nonatomic, retain) UILabel * nameLbl;
And myMovie.h:
{
NSString* name;
}
@property (nonatomic, retain) NSString* name;
myMovie.m:
#import "myMovie.h"
@implementation myMovie
@synthesize name, gross, desc;
-(void) dealloc
{
self.name = nil;
self.gross = nil;
self.desc = nil;
[super dealloc];
}
@end
Of course this is only the necessary code. I can't figure out why it is leaking. I don't know if this is the cause but my application crashes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
泄漏的行是上面的行:
self.movie = [[myMovie alloc]init];
将其更改为
self.movie = [[[myMovie alloc]init] autorelease];< /code> 或添加
[self.movie release];
作为紧随其后的行。The line that's leaking is the one above:
self.movie = [[myMovie alloc]init];
Change it to
self.movie = [[[myMovie alloc]init] autorelease];
or add[self.movie release];
as the line immediately afterwards.