0

我正在使用协议,并且正在泄漏,这是我的代码:

+ (id<GMGridViewLayoutStrategy>)strategyFromType:(GMGridViewLayoutStrategyType)type
{
    id<GMGridViewLayoutStrategy> strategy = nil;

    switch (type) {
        case GMGridViewLayoutVertical:
            strategy = [[GMGridViewLayoutVerticalStrategy alloc] init];
            break;
        case GMGridViewLayoutHorizontal:
            strategy = [[GMGridViewLayoutHorizontalStrategy alloc] init];
            break;
        case GMGridViewLayoutHorizontalPagedLTR:
            strategy = [[GMGridViewLayoutHorizontalPagedLTRStrategy alloc] init];
            break;
        case GMGridViewLayoutHorizontalPagedTTB:
            strategy = [[GMGridViewLayoutHorizontalPagedTTBStrategy alloc] init];
            break;
    }

    return strategy;
}

如果我做自动释放,那么它会崩溃。那么我现在该怎么办?请帮助我...谢谢...

4

2 回答 2

2

在你的strategyFromType功能

return [strategy autorelease];

当您调用此函数时,请使用retain

GMGridViewLayoutStrategy *strat = [[YourClassName strategyFromType:yourType] retain];

使用strat变量后

[strat release];
于 2012-05-22T06:56:52.300 回答
0
-(id<GMGridViewLayoutStrategy>)strategyFromType:(GMGridViewLayoutStrategyType)type
{id<GMGridViewLayoutStrategy> strategy = nil;

switch (type) {
    case GMGridViewLayoutVertical:
        strategy = [[GMGridViewLayoutVerticalStrategy alloc] init];
        break;
    case GMGridViewLayoutHorizontal:
        strategy = [[GMGridViewLayoutHorizontalStrategy alloc] init];
        break;
    case GMGridViewLayoutHorizontalPagedLTR:
        strategy = [[GMGridViewLayoutHorizontalPagedLTRStrategy alloc] init];
        break;
    case GMGridViewLayoutHorizontalPagedTTB:
        strategy = [[GMGridViewLayoutHorizontalPagedTTBStrategy alloc] init];
        break;
}

return [strategy autorelease];
}

在调用方..

GMGridViewLayoutStrategy *obj = [[YourClassName strategyFromType:yourType] retain];

使用“obj”后释放它。

于 2012-05-22T07:06:51.110 回答