Xcode 指示对象潜在泄漏

发布于 2024-12-02 22:18:27 字数 1039 浏览 1 评论 0原文

首先我是一个n00b。经过长时间的尝试和研究,我决定寻求一些外部帮助。 我的项目: 我为孩子们做了一本书。现在我正在分析我的代码并尝试消除一些泄漏(一段时间后发生 1 级 + 2 级崩溃)。 这是我的代码

- (void)loadView {

    _oben = YES;
    _unten = NO;

    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 

    UIImage *cover = [UIImage imageNamed:@"Umschlag.png"]; //Here it says "Potential leak..
    //..allocated on line 141 (thats at self.view = [[UIView alloc] initWithFrame:...
    image = [[UIImageView alloc] initWithImage:cover];
    image.frame = CGRectMake(0, 0, 768, 1024);
    [self.view addSubview:image];
    [image release];

    UITextView *text1 = [[UITextView alloc] initWithFrame:CGRectMake(184, 700, 400, 100)];
    text1.backgroundColor = [UIColor clearColor];
    text1.textAlignment = UITextAlignmentCenter;
    text1.text = NSLocalizedString(@"CoverTextKey1", nil);
    [self.view addSubview:text1];
    [text1 release];

    [self addButtonNext];
    [self addSwipeDown];
    [self addSwipeUp];
}

有什么想法吗? 如果有人能帮助我,那就太酷了! 预先感谢普朗基

First of all i am a n00b. After long time of trying and research i decided to get some external help.
My Project:
i made a book for children. Now i am analyzing my code and try to get rid of some leaks (level 1 + 2 Crash after a while).
Here is my Code

- (void)loadView {

    _oben = YES;
    _unten = NO;

    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 

    UIImage *cover = [UIImage imageNamed:@"Umschlag.png"]; //Here it says "Potential leak..
    //..allocated on line 141 (thats at self.view = [[UIView alloc] initWithFrame:...
    image = [[UIImageView alloc] initWithImage:cover];
    image.frame = CGRectMake(0, 0, 768, 1024);
    [self.view addSubview:image];
    [image release];

    UITextView *text1 = [[UITextView alloc] initWithFrame:CGRectMake(184, 700, 400, 100)];
    text1.backgroundColor = [UIColor clearColor];
    text1.textAlignment = UITextAlignmentCenter;
    text1.text = NSLocalizedString(@"CoverTextKey1", nil);
    [self.view addSubview:text1];
    [text1 release];

    [self addButtonNext];
    [self addSwipeDown];
    [self addSwipeUp];
}

Any ideas?
It would be really cool if anyone could help me!
Thanks in advance Planky

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

妞丶爷亲个 2024-12-09 22:18:27

第 141 行分配的潜在泄漏:

self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

该行是过度保留对象,因为alloc-init返回一个保留对象(+1),并且属性设置器也保留该对象(+2)。

您可以使用临时变量...

UIView *temp = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = temp;
[temp release];

...或 autorelease 来解决此问题:

self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]; 

Potential leak allocated on line 141:

self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

That line is over-retaining the object because alloc-init returns a retained object (+1) and the property setter also retains the object (+2).

You can use a temporary variable...

UIView *temp = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = temp;
[temp release];

...or autorelease to fix this:

self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]; 
夏至、离别 2024-12-09 22:18:27

请改用 + (UIImage *)imageWithContentsOfFile:(NSString *)path- (id)initWithContentsOfFile:(NSString *)path 。它们不会缓存图像,imageNamed: 会缓存图像。

Use + (UIImage *)imageWithContentsOfFile:(NSString *)path or - (id)initWithContentsOfFile:(NSString *)path instead. They don't cache the image, imageNamed: does.

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