2013-03-26 76 views
1

我做的使用ScreenCaptureView.为潜在的内存泄漏的警告和应用程序崩溃

我用下面的代码录像;

-(CGContextRef) createBitmapContextOfSize:(CGSize) size 

    { CGContextRef context = NULL;  CGColorSpaceRef colorSpace;  int    bitmapByteCount; 

     int bitmapBytesPerRow; 

     bitmapBytesPerRow = (size.width * 4);  
     bitmapByteCount  = (bitmapBytesPerRow * size.height); 
     colorSpace = CGColorSpaceCreateDeviceRGB(); 
     if (bitmapData != NULL) 
     {  
      free(bitmapData); 
     } 

     bitmapData = malloc(bitmapByteCount); 
      if (bitmapData == NULL) 
     {  
       fprintf (stderr, "Memory not allocated!"); 
       return context=NULL;  
     }  

    context = CGBitmapContextCreate (bitmapData, size.width, size.height, 8, bitmapBytesPerRow,colorSpace, kCGImageAlphaNoneSkipFirst); 

    CGContextSetAllowsAntialiasing(context,NO); 
     if (context== NULL) 
     { 
     free (bitmapData); 
     fprintf (stderr, "Context not created!"); 
     return NULL; 
    } 

    return context; 

    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace); 
} 

但它给我潜在的内存泄漏警告和应用程序崩溃。

它在ipod中正常工作,但会在ipad中崩溃。

我该如何解决它?

感谢......

+0

@Maulik - 感谢您的编辑。请回答这个问题...... !!! Mogs .. :-) – Manthan 2013-03-26 09:06:00

+0

你用@autoreleasepool试过了吗? – Ganapathy 2013-03-26 09:07:57

+0

@Ganapathy - 不,我还没有尝试过autoreleaseool。如何使用它? – Manthan 2013-03-26 09:09:15

回答

1
CGContextRelease(context); 
CGColorSpaceRelease(colorSpace); 

应该放在前

return context; 
+0

感谢您的回复。但它正在崩溃。 – Manthan 2013-03-26 09:12:53

+0

这是错误的我想。如果您在返回之前释放上下文。将返回什么? – 2013-03-26 13:53:11

+0

是的,我想应该使用某种autoreleasepool。 – Cosmin 2013-03-26 13:56:48

0

CGContextRef内部包含一些内存分配。所以只需使用@autoreleasepool尝试一次。

  -(CGContextRef) createBitmapContextOfSize:(CGSize) size 

      { 
    @autoreleasepool 
    { 

      CGContextRef context = NULL;  
       CGColorSpaceRef colorSpace;  
        int bitmapByteCount; 

       int bitmapBytesPerRow; 

       bitmapBytesPerRow = (size.width * 4);  
       bitmapByteCount  = (bitmapBytesPerRow * size.height); 
       colorSpace = CGColorSpaceCreateDeviceRGB(); 
       if (bitmapData != NULL) 
       {  
        free(bitmapData); 
       } 

       bitmapData = malloc(bitmapByteCount); 
        if (bitmapData == NULL) 
       {  
         fprintf (stderr, "Memory not allocated!"); 
         return context=NULL;  
       }  

      context = CGBitmapContextCreate (bitmapData, size.width, size.height, 8, bitmapBytesPerRow,colorSpace, kCGImageAlphaNoneSkipFirst); 

      CGContextSetAllowsAntialiasing(context,NO); 
       if (context== NULL) 
       { 
       free (bitmapData); 
       fprintf (stderr, "Context not created!"); 
       return NULL; 
      } 
      return context; 

      CGContextRelease(context); 
      CGColorSpaceRelease(colorSpace); 
    } 

     } 
+0

它给最后3行上下文和colorSpace上的错误 – Manthan 2013-03-26 09:21:02

+0

答案更新。使用它并再次尝试。 – Ganapathy 2013-03-26 09:25:54

+0

再次感谢您的回复。但它仍然崩溃,并给messave“*** - [CALayer发布]:消息发送到释放实例0x3493b0” – Manthan 2013-03-26 09:34:29