Frank84

Old Memory Clearing Hack

Aug 2nd, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (id)init
  2. {
  3.     self = [super init];
  4.     if (self != nil)
  5.     {
  6.         [self schedule:@selector(updateMemory:) interval:0.1];
  7.     }
  8.    
  9.     return self;
  10. }
  11.  
  12. - (void)updateMemory:(ccTime)dt
  13. {
  14.     static long allocated = 0;
  15.     static void* allocatedPtr = NULL;
  16.     static long size = 1024*1024;
  17.     //NSLog(@"Ticking...");
  18.     allocated = allocated + size;
  19.    
  20.     if (allocatedPtr)
  21.     {
  22.             free(allocatedPtr);
  23.         }
  24.    
  25.         allocatedPtr = malloc(allocated);
  26.    
  27.         if (!allocatedPtr)
  28.     {
  29.             NSLog(@"out of memory at %ld", allocated);
  30.         NSLog(@"%@", [self formatMemory:allocated]);
  31.             [self unschedule:@selector(updateMemory:)];
  32.         [self loadMainMenuScene:0];
  33.         return;
  34.         }
  35.  
  36.     for (long i=0; i<allocated; i += 4096)
  37.     {
  38.         *(char*)(allocatedPtr + i) = 'a';
  39.     }
  40.  
  41.     if (allocated > 40*1024*1024)
  42.     {
  43.         NSLog(@"Reached %ld. Terminating memory ballooning", allocated);
  44.             [self unschedule:@selector(updateMemory:)];
  45.         free(allocatedPtr);
  46.         [self loadMainMenuScene:0];
  47.         return;
  48.     }
  49. }
Add Comment
Please, Sign In to add comment