请帮我找到这段 iOS 代码中的两个漏洞

发布于 2025-01-07 00:35:39 字数 1019 浏览 0 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝咒 2025-01-14 00:35:39

泄漏的行是上面的行: self.movi​​e = [[myMovie alloc]init];

将其更改为 self.movi​​e = [[[myMovie alloc]init] autorelease];< /code> 或添加 [self.movi​​e 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文