首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >目标-c:在不造成内存泄漏的情况下生成大量特定类实例的正确方法?

目标-c:在不造成内存泄漏的情况下生成大量特定类实例的正确方法?
EN

Stack Overflow用户
提问于 2014-03-06 00:06:05
回答 1查看 161关注 0票数 0

我正在做一个简单的SpriteKit游戏。它有一个随机生成的世界,由线条组成。这些行由NHRLineNode类表示。在级别开始时,我会同时生成这些行,其中有两个for循环,一个用于屏幕的每一侧。这个很好用。除了游戏的主要游戏场景外,还有一个“游戏上方”屏幕,在游戏之间显示和显示你的得分等等,还有一个主菜单场景。当我玩游戏,死,看屏幕上的游戏,然后再玩的时候,问题就来了。看看Xcode中的内存使用情况,当我第一次从菜单场景开始游戏时,内存使用量似乎会上升,在整个游戏过程中保持稳定,然后在我死后跳到6-10 MB。这个内存永远不会恢复,应用程序每次播放时都会使用越来越多的内存。我认为这是因为生成平台的for循环只是创建NHRLineNode类的一个新实例,正确定位,然后再执行一次。这就是导致我记忆问题的原因吗?还是更有可能出现在现场的游戏中?

相关片段:

生成平台的for循环:

代码语言:javascript
复制
    int previousXVal1 = -10;
    int previousYVal1 = 425;
    int newXPosition = 0;
    //How many do you want?

    int numToGen = 100;
    for(int i = 1; i<=numToGen; i++) {



        NHRLineNode *lineGen = [NHRLineNode initAtPosition:CGPointMake(previousXVal1 + arc4random_uniform(85), previousYVal1 - 75)];
        [worldNode addChild:lineGen];
        if(lineGen.position.x > 390) {
            newXPosition = lineGen.position.x - 100; //That should bring it onscreen!
            lineGen.position = CGPointMake(newXPosition, lineGen.position.y); //Make the new position

        } else if (lineGen.position.x < 100) {
            newXPosition = lineGen.position.x + 100; //That will bring it onscreen!!
            lineGen.position = CGPointMake(newXPosition, lineGen.position.y);
                        }
        previousXVal1 = lineGen.position.x;
        previousYVal1 = lineGen.position.y;


    }
    //This creates the lines on the right side (performing the inverse calculation on the x pos
    //int numToGen = 10;

    int previousXVal2 = 350;
    int previousYVal2 = 525;
    for(int i = 1; i<=numToGen; i++) {


        NHRLineNode *lineGen = [NHRLineNode initAtPosition:CGPointMake(previousXVal2 - arc4random_uniform(85), previousYVal2 - 75)];
        [worldNode addChild:lineGen];
        if(lineGen.position.x > 390) { //It is partially off-screeb=n
            newXPosition = lineGen.position.x - 100; //That should bring it onscreen!
            lineGen.position = CGPointMake(newXPosition, lineGen.position.y); //Make the new position
        } else if (lineGen.position.x < 100) { //It is partially off-screen
            newXPosition = lineGen.position.x + 100; //That will bring it onscreen!!
            lineGen.position = CGPointMake(newXPosition, lineGen.position.y);
        }

        previousXVal2 = lineGen.position.x;
        previousYVal2 = lineGen.position.y;



    }

initAtPosition方法NHRLineNode:

代码语言:javascript
复制
+(id)initAtPosition:(CGPoint)point {
//This is all the properties of one of the lines in the level
SKSpriteNode *theLine = [SKSpriteNode spriteNodeWithImageNamed:@"newLine"];
//The physics body is slightly smaller than the image itself -- why idk
theLine.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(75,5)];
//It is not affected by gravity
theLine.physicsBody.dynamic = NO;
//Its position is the point given us when the function was called
theLine.position = point;
//Return it for further positioning by the generator
return theLine;
}

GameOverScene:http://pastebin.com/wUpguueb的整个实现

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-06 14:06:00

即使您有ARC在您的一方,有时您需要给予它“激励”释放对象,特别是当创建许多在一个循环。实际上,这只是与提供范围有关,因此ARC理解它可能会释放已分配的实例。

尝试用@autoreleasepool { …}包装for循环的主体(即不环绕整个for循环)--也就是说,如果@autoreleasepool块是for循环的唯一语句。

让我们知道它是否有帮助!在迭代地将数据导入核心数据时,我通常需要这样做。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22212173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档