Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS应用程序中Objective-C代码的内存泄漏_Ios_Objective C - Fatal编程技术网

iOS应用程序中Objective-C代码的内存泄漏

iOS应用程序中Objective-C代码的内存泄漏,ios,objective-c,Ios,Objective C,我的代码正在消耗内存。我添加了这个函数,它似乎是所有问题的原因,因为当我不调用它时,我就不会用完它 裁剪图像是Objective-C中的一个函数。如何释放拍卖中使用的内存,以便在功能结束时在退出之前清除所有内容 -(void) crop: (CVImageBufferRef)sampleBuffer { int cropX0, cropY0, cropHeight, cropWidth, outWidth, outHeight; cropHeight = 720; cr

我的代码正在消耗内存。我添加了这个函数,它似乎是所有问题的原因,因为当我不调用它时,我就不会用完它

裁剪图像是Objective-C中的一个函数。如何释放拍卖中使用的内存,以便在功能结束时在退出之前清除所有内容

-(void) crop: (CVImageBufferRef)sampleBuffer
{
    int cropX0, cropY0, cropHeight, cropWidth, outWidth, outHeight;

    cropHeight = 720;
    cropWidth = 1280;
    cropX0 = 0;
    cropY0 = 0;

    outWidth = 1280;
    outHeight = 720;

    CVPixelBufferLockBaseAddress(sampleBuffer,0);
    void *baseAddress = CVPixelBufferGetBaseAddress(sampleBuffer);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(sampleBuffer);

    vImage_Buffer inBuff;
    inBuff.height = cropHeight;
    inBuff.width = cropWidth;
    inBuff.rowBytes = bytesPerRow;

    int startpos = cropY0*bytesPerRow+4*cropX0;
    inBuff.data = baseAddress+startpos;

    unsigned char *outImg= (unsigned char*)malloc(4*outWidth*outHeight);
    vImage_Buffer outBuff = {outImg, outHeight, outWidth, 4*outWidth};

    vImage_Error err = vImageScale_ARGB8888(&inBuff, &outBuff, NULL, 0);
    if (err != kvImageNoError)
    {
        NSLog(@" error %ld", err);
    }
    else
    {
        NSLog(@"Success");
    }


    CVPixelBufferRef pixelBuffer = NULL;
    OSStatus result = CVPixelBufferCreateWithBytes(kCFAllocatorDefault,
                                                   inBuff.width,
                                                   inBuff.height,
                                                   kCVPixelFormatType_32BGRA,
                                                   outImg,
                                                   bytesPerRow,
                                                   NULL,
                                                   NULL,
                                                   NULL,
                                                   &pixelBuffer);


    CVPixelBufferUnlockBaseAddress(sampleBuffer,0);

}
免费(outImg); 由于没有释放分配的内存,因此在结尾处丢失。
这在嵌入式编程中是一个很好的实践,在这里也是因为您有常量大小的像素维度来使用常量矩阵,您可以在函数顶部声明并初始化为零

unsigned char*outImg=(unsigned char*)malloc(4*outWidth*outHeight)哪里有你的匹配免费?放在一边-如果这是在C;为什么你要把它标记为C++和ObjuleC?你已经用两种不同的语言标记了它,但是错过了一种!C、C++和ObjuleC是不同的语言。您的标记、问题正文和示例代码在您询问的语言上不一致。不,它不是C,它是Objective-C,标记是对的,描述是错的。Objective-C是C的超集。因此声明您的“C非常基本”这意味着你的Objective-C也是基本的。也许可以解释为什么:)malloc需要与free匹配。