简体   繁体   English

内存泄漏与unsigned char *

[英]Memory leak with unsigned char *

I have a method where I return unsigned char * : 我有一个方法,我返回unsigned char *

- (unsigned char *)calledMethod {
    ...
    unsigned char *result = (unsigned char*) calloc(size * 4, sizeof(unsigned char));
    ...
    return result;
}

The problem is, when I call this method: 问题是,当我调用这个方法时:

unsigned char *myVariable = [myClass calledMethod];

I get a memory leak. 我得到了内存泄漏。

How should I free the memory? 我该如何释放记忆?

I've already tried free(myVariable) , but no use. 我已经尝试过free(myVariable) ,但没有用。 If I would call free(result) in my calledMethod method, there would be no leak, but then I cannot return it. 如果我在我的calledMethod方法中调用free(result) ,就不会有泄漏,但是我无法返回它。 Thanks in advance! 提前致谢!

EDIT: 编辑:

The problem is, I am storing in that myVariable the pixel data of a UIImage (4 components for each CGPoint :rgba). 问题是,我存储了myVariable UIImage的像素数据(每个CGPoint 4个组件:rgba)。 I have to modify some of the pixels and reconstruct the image. 我必须修改一些像素并重建图像。 I am accessing the data of a point like this: 我正在访问这样一个点的数据:

int byteIndex = (bytesPerRow * point.y) + point.x * bytesPerPixel;
CGFloat red   = (myVariable[byteIndex]     * 1.0) / 255.0;
CGFloat green = (myVariable[byteIndex + 1] * 1.0) / 255.0;
CGFloat blue  = (myVariable[byteIndex + 2] * 1.0) / 255.0;
CGFloat alpha = (myVariable[byteIndex + 3] * 1.0) / 255.0;

If I use NSMutableData , what would be the length parameter in [NSMutableData dataWithBytes:(const void *) length:(NSUInteger)] and how would I modify the data of a given CGPoint ? 如果我使用NSMutableData ,那么[NSMutableData dataWithBytes:(const void *) length:(NSUInteger)]length参数是什么?我将如何修改给定CGPoint的数据?

free() is the correct function to call to free up the memory. free()是调用以释放内存的正确函数。 If you still have the memory leak, you are either not calling free() at all, or you are not calling it enough. 如果你仍然有内存泄漏,你要么根本没有调用free() ,要么就是你没有调用它。 For example, the following would leak: 例如,以下内容会泄漏:

myVariable = [foo calledMethod];
// some stuff
myVariable = [foo calledMethod];
// more stuff
free(myVariable);

Might I suggest that, instead of using a raw buffer like this, you use an NSMutableData . 我可能会建议你使用NSMutableData ,而不是像这样使用原始缓冲区。 That way, you'll get the benefit of whatever Objective-C memory management method you are using. 这样,您将获得您正在使用的任何Objective-C内存管理方法的好处。

What you are doing is correct. 你在做什么是正确的。 You return an malloc-ed block of memory that you assign to myVariable and later free it using free(myVariable) . 您返回一个malloc-ed内存块,您将其分配给myVariable ,然后使用free(myVariable)释放它。 The code that you are showing us is not responsible for the leak. 您向我们展示的代码不对泄漏负责。 Perhaps it lies in the ... ?. 也许它在于... What does static analysis tell you? 静态分析告诉你什么?

Calling free when you are done will deallocate the memory. 呼唤free ,当你完成会回收内存。 Are you sure you instrumented things correctly when you tried calling free(myVariable) ? 当你尝试调用free(myVariable)时,你确定你正确地检测了一些东西吗? That should work. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM