views:

112

answers:

2

Hello everyone,

in my app I'm trying to process an YUV422 image from a webcam. but I'm getting a huge memory leak. below you can see a sample of simplified code of my app. if I disable the "(m1..." line in the function, there is no leak. (but the images are not being processed). I've tried locks, pools, etc but nothing changed. I'm relatively new to cocoa so all these square brackets look funny/scary to me ;-)
is it a problem with using "char
"? in my old linux+c++ app there was no problem. but I was using "unsigned char*", no threads, and I have never checked for leaks...

global:

...
char m [640*480];

"main":

...
[NSThread detachNewThreadSelector:@selector(processOutputBuffer) toTarget:self withObject:nil];
...

function1:

- (void)processOutputBuffer {
[NSThread setThreadPriority:0.4];
[lock lock];
...
Ptr outputBufferBaseAddress = (Ptr)CVPixelBufferGetBaseAddress(outputBuffer);
CVPixelBufferLockBaseAddress(outputBuffer, 0);
[self yuv422_to_y8uv8:outputBufferBaseAddress m1:m];
...
}

function2:

- (void) yuv422_to_y8uv8:(char *)image m1:(char *)m1 {
 int x,y;
 for (y = 0; y < 480; y++)
  for (x = 0; x < 640; x++)
   {
   *(m1 + (640 * y) + (x))=*(image + (640*2 * y) + (x*2)+1);
   }
}
A: 

A lame answer... I know. But in case no better answer comes to you. You can always put the function in a C file and make it plain c.

Might even be nice to see if it solves the leak. If not, then the problem lies somewhere else.

Toad
I might want to try that, but unfortunately I "must" go towards cocoa, not get "back" to c/c++<br/>but ..... of course ..... if nothing helps, I'll try that too. thanks.
ravyr
so.... it turned out that I will not try this, because the leak was completely elsewhere. anyway - thanks for your time and "good will"
ravyr
A: 

if I disable the "*(m1..." line in the function, there is no leak.

Untrue. That's just an assignment. Either there is no leak anyway, or that is not the cause of the leak.

You can use Instruments to look for objects (both plain memory allocations and Cocoa objects) that get leaked, and to diagnose the leaks.

is it a problem with using "char *"?

No. Types do not cause leaks. Incorrect memory management causes leaks.

in my old linux+c++ app there was no problem. but I was using "unsigned char*", no threads, and I have never checked for leaks...

It's possible that you introduced the leak when adding threads, or when adding Cocoa code. It's equally possible that the leak was always there and you never saw it before. Only when you find the problem with Instruments or another tool will you know for sure.

You might also try running the Clang Static Analyzer. It can detect some code patterns that cause leaks, among other things.

Peter Hosey
static analyzer found some issues, but none with the leaks;instruments (v1.5 from xcode v3.1) found a huge leak (only when that line was enabled) but...... it said that CVObject takes 496 B (it didn't make sense to me becouse this particular line doesn't contain any CVObject (whatever that is) )LeakedBlocks>XxXXXXXHistory>Category=CVObject, EventType=malloc, size=496, ResponsibleLibrary=CoreVideo, ResponsiblCaller=CVObject::alloc(..long, ..const, ..long, ..long).....thank you for any suggestions.....
ravyr
496 bytes is not a “huge leak”. Try sorting the list by net object count, then look for classes that are neither Cocoa nor Core Foundation.
Peter Hosey
thank you for your answer. it helped me a lot - I finally started to search in the right direction using right tools...#net did not show the leak just like I have mistaken the 496B as the "huge leak"..but, observing another instruments run running, I have noticed that there are "GeneralBlock-618496" with relatively small #net count, but giant NetBytes - after 1minute run over 500MB. ReponsibleCallers of these instances are: CVPixelBufferBacking::initWithPixelBufferDescription(...)so now I know that I was lost, and I suppose I'm back on track now ;-)
ravyr
this looks similar to my current problem:http://lists.apple.com/archives/quicktime-api/2008/Jul/msg00161.html
ravyr
Make sure you release `outputBuffer`, if necessary (it depends on how you obtained it, which you don't show).
Peter Hosey
yes, that was the problem. I've fixed it another way : I've moved the CVPixelBufferCreate and CVPixelBufferRef declaration out of the Threads function - to the global. All (3) buffers are created only once and in the runtime everything is fine. thank you for your help
ravyr