如何释放我想要返回的值?

时间:2021-12-06 07:25:15

I know this is fairly fundamental stuff.

我知道这是相当基本的东西。

I have a class with a function that returns the name of the month; I'm not sure how to release a value that I want to return to prevent leaks.

我有一个带有函数的类,它返回月份的名称;我不确定如何释放我想要返回的值以防止泄漏。

In the class this value is declared:

在类中声明了这个值:

static NSDateFormatter *formatter = nil;
if (formatter == nil) {
    formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MMMM yyyy"];
}

Things happen, and then:

事情发生了,然后:

if([exampleDate isEqualToDate:
    [[self creationDate] laterDate:exampleDate]])
{ //earlierDate returns the earlier date
    return(@"Examples");
}else{
    return([formatter stringFromDate:[self creationDate]]);
}

I've tried using autorelease but I'm using it incorrectly because I get a crash when I try to release it. I've also tried assigning the return value to a string, but I have the same crashing problems. Sorry for asking a question that is so fundamental, but I'd appreciate knowing how to release this properly, while still returning the value - and understanding how it works.

我尝试过使用自动释放,但我使用不正确,因为当我尝试释放它时,我遇到了崩溃。我也尝试将返回值赋给字符串,但是我遇到了同样的崩溃问题。很抱歉提出一个非常重要的问题,但我很高兴知道如何正确地发布这个问题,同时仍然返回价值 - 并了解它的工作原理。

1 个解决方案

#1


0  

there are no leaks in the return part. Since stringFromDate: is not copy, mutableCopy, retain, alloc or new it returns already an autoreleased object.

返回部分没有泄漏。由于stringFromDate:不是copy,mutableCopy,retain,alloc或new,因此它返回已经自动释放的对象。

Your NSDateFormatter will leak when the class is deallocated. If you don't want it to leak you should create a @property for it and release it in dealloc.
Local static objects will always leak.
I would not use such objects outside of singletons. Each time you create one of your classes you will leak an NSDateFormatter. Using @property is better in almost every case.

取消分配类时,NSDateFormatter将泄漏。如果您不希望它泄漏,您应该为它创建一个@property并在dealloc中释放它。本地静态对象将始终泄漏。我不会在单身人士之外使用这些物品。每次创建一个类时,都会泄漏NSDateFormatter。几乎在所有情况下,使用@property都会更好。

I've seen hacks like this but in my opinion they solve a problem you shouldn't have in the first place:

我见过这样的黑客,但在我看来,他们解决了你不应该首先遇到的问题:

- (NSString *)someDateFromString:(NSString *)str {
    static NSDateFormatter *formatter = nil;
    if (formatter == nil) {
        formatter = [[NSDateFormatter alloc] init];
    }
    if (str == nil) {
        [formatter release];
        formatter = nil;
        return nil;
    }
    // do something
    return date;
}

- (void)dealloc {
    [self someDateFromString:nil]; // will release the static date formatter.
    [super dealloc];
}

#1


0  

there are no leaks in the return part. Since stringFromDate: is not copy, mutableCopy, retain, alloc or new it returns already an autoreleased object.

返回部分没有泄漏。由于stringFromDate:不是copy,mutableCopy,retain,alloc或new,因此它返回已经自动释放的对象。

Your NSDateFormatter will leak when the class is deallocated. If you don't want it to leak you should create a @property for it and release it in dealloc.
Local static objects will always leak.
I would not use such objects outside of singletons. Each time you create one of your classes you will leak an NSDateFormatter. Using @property is better in almost every case.

取消分配类时,NSDateFormatter将泄漏。如果您不希望它泄漏,您应该为它创建一个@property并在dealloc中释放它。本地静态对象将始终泄漏。我不会在单身人士之外使用这些物品。每次创建一个类时,都会泄漏NSDateFormatter。几乎在所有情况下,使用@property都会更好。

I've seen hacks like this but in my opinion they solve a problem you shouldn't have in the first place:

我见过这样的黑客,但在我看来,他们解决了你不应该首先遇到的问题:

- (NSString *)someDateFromString:(NSString *)str {
    static NSDateFormatter *formatter = nil;
    if (formatter == nil) {
        formatter = [[NSDateFormatter alloc] init];
    }
    if (str == nil) {
        [formatter release];
        formatter = nil;
        return nil;
    }
    // do something
    return date;
}

- (void)dealloc {
    [self someDateFromString:nil]; // will release the static date formatter.
    [super dealloc];
}