【问题标题】:Giving Potential Memory Leak warning and app crashes给出潜在的内存泄漏警告和应用程序崩溃
【发布时间】:2013-03-16 00:01:56
【问题描述】:

我正在使用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 中崩溃。

我该如何解决?

谢谢......

【问题讨论】:

  • @Maulik - 感谢您的编辑。也请给出问题的答案...!!! Mogs.. :-)
  • 你试过@autoreleasepool吗?
  • @Ganapathy - 不,我没有尝试过 autoreleaseool。怎么用?
  • 这个位图数据是什么?
  • @user2135853:哇……你的崩溃日志是什么?

标签: iphone ios objective-c xcode memory-leaks


【解决方案1】:
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);

应该放在前面

return context;

【讨论】:

  • 感谢您的回复。但它正在崩溃。
  • 我认为这是错误的。如果您在返回之前释放上下文。将返回什么?
  • 是的,我想应该使用某种自动释放池。
【解决方案2】:

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);
  }

        }

【讨论】:

  • 再次感谢您的回复。但它仍然崩溃并给出消息“*** - [CALayer release]:发送到已释放实例0x3493b0的消息”
猜你喜欢
  • 2012-10-20
  • 1970-01-01
  • 1970-01-01
  • 2018-04-04
  • 2012-05-28
  • 2011-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多