Objc leak behavior i can't explain

In the next code loop, there is no memory leak (which is confirmed by viewing the loop endlessly under the "top");

NSBitmapImageRep *this_bmap = 0;

while (1) {

  CGImageRef windowImage = 
     CGWindowListCreateImage(CGRectNull,
                             kCGWindowListOptionIncludingWindow,
                             windowID, kCGWindowImageDefault);

  this_bmap = [[NSBitmapImageRep alloc] initWithCGImage:windowImage];

  [this_bmap release];
  CGImageRelease(windowImage);
}

and I would not expect that. However, when I copy the pointer to the bitmap image data, for example:

NSBitmapImageRep *this_bmap = 0;

while (1) {

  CGImageRef windowImage = 
     CGWindowListCreateImage(CGRectNull,
                             kCGWindowListOptionIncludingWindow,
                             windowID, kCGWindowImageDefault);

  this_bmap = [[NSBitmapImageRep alloc] initWithCGImage:windowImage];

  void *pixels1 = [this_bmap bitmapData];

  [this_bmap release];
  CGImageRelease(windowImage);
}

now it flows like crazy. I see that this happens quickly under the "top", and the program eventually stops.

I'm new to Objective-C, but I'm not new to programming, and I don't understand this behavior. The documentation for the bitmapData method claims that it just returns a pointer (as opposed to highlighting something), so I'm at a dead end. I found a similar question a while ago, but the only answer was “learn pools”, and I don’t see how this can help here.

Any ideas what is going on here?

+3
3

, , . (.. ), :

NSBitmapImageRep * this_bmap = 0;

while (1) {
  NSAutoreleasePool* loopPool = [NSAutoreleasePool new];
  CGImageRef windowImage = 
     CGWindowListCreateImage(CGRectNull,
                             kCGWindowListOptionIncludingWindow,
                             windowID, kCGWindowImageDefault);

  this_bmap = [[NSBitmapImageRep alloc] initWithCGImage:windowImage];

  void *pixels1 = [this_bmap bitmapData];

  [this_bmap release];
  CGImageRelease(windowImage);
  [loopPool drain];
}
+7

, , . , , , , pix1. , . 1 , .

, . .

0

"initWithCGImage:" :

, NSBitmapImageRep . cgImage, , . .

, , bitmapData, CFRelease'ed.

0

Source: https://habr.com/ru/post/1774648/


All Articles