Preloading SKTextureAtlases causes memory problems and crash

234 views Asked by At

I'm making an adventure type game. While making this game I experienced memory leaks, so I decided to create a preloader, which I called SKPreloader.

In my GameViewController:

_preloader = [[SKPreloader alloc]init];
[_preloader setDelegate:self];
[_preloader preloadTextureAtlasesForScene:1];

In the SKPreloader:

(void)preloadTextureAtlas:(SKTextureAtlas *)atlas{
[atlas preloadWithCompletionHandler:^void {
    NSLog(@"laden...");
    if([atlas isEqual:_atlasFotoDistort]){
        NSLog(@"loaded foto");
        [self preloadTextureAtlas:_atlasFotoDistortWoman];
    }else if([atlas isEqual:_atlasFotoDistortWoman]){
        NSLog(@"loaded foto woman");
        [self preloadTextureAtlas:_atlasTV];
    }else if ([atlas isEqual:_atlasTV]){
        NSLog(@"loaded tv1");
        [self preloadTextureAtlas:_atlasTV2];
    }else if ([atlas isEqual:_atlasTV2]){
        NSLog(@"loaded tv2");
        [self preloadTextureAtlas:_atlasTV3];
    }else if ([atlas isEqual:_atlasTV3]){
        NSLog(@"loaded tv3");
        [self preloadTextureAtlas:_atlasDementia];
    }else if ([atlas isEqual:_atlasDementia]){
        NSLog(@"loaded dementia");
        [self preloadTextureAtlas:_atlasDementiaKeuken];
    }else if ([atlas isEqual:_atlasDementiaKeuken]){
        NSLog(@"loaded keuken");
        [self preloadTextureAtlas:_atlasGordijnLinks];
    }else if ([atlas isEqual:_atlasGordijnLinks]){
        NSLog(@"loaded gordijnlinks");
        [self preloadTextureAtlas:_atlasGordijnRechts];
    }else if ([atlas isEqual:_atlasGordijnRechts]){
        NSLog(@"loaded gordijnrechts");
        [self preloadTextureAtlas:_atlasHond];
    }else if ([atlas isEqual:_atlasHond]){
        NSLog(@"loaded hond");
        [self preloadTextureAtlas:_atlasHondKop];
    }else if ([atlas isEqual:_atlasHondKop]){
        NSLog(@"loaded hond kop");
        [self preloadRestOfTextureAtlasesForScene:1];
    }
}];
}


- (void)preloadRestOfTextureAtlasesForScene:(int)sceneNumber{

NSArray *arrayOfAtlases = [self restofTextureAtlasesForScene:sceneNumber];

[SKTextureAtlas preloadTextureAtlases:arrayOfAtlases withCompletionHandler:^void{
    NSLog(@"loaded all");
    [_delegate didFinishPreloading];
}];
}

The texts prefixed by _ are SKTextureAtlases.

The delegate is read in the GameViewController and starts the first scene. When the SKPreloader reaches the method preloadRestOfTextureAtlases, the memory usage is at 240mb and stays there which ultimately causes a crash.

Any help on how I could fix this problems and still preload the atlases?

Addition: the bigest atlas is 16mb. All the small atlases are preloaded in the last array.

0

There are 0 answers