Re: Avoiding leaks with "initWithCGImage"
Re: Avoiding leaks with "initWithCGImage"
- Subject: Re: Avoiding leaks with "initWithCGImage"
- From: Rob Petrovec via Cocoa-dev <email@hidden>
- Date: Thu, 24 Aug 2023 08:37:53 -0600
ARC only affects Objective-C objects. It has no effect on CF objects like
CGImageRefs, CFArrayRefs, CFDictionaryRefs etc. If you play with CF objects in
an ARC app you still need to release them. You don’t need to release Obj-C
objects and the compiler will warn you if you try to.
—Rob
> On Aug 23, 2023, at 11:44 PM, Sandor Szatmari via Cocoa-dev
> <email@hidden> wrote:
>
> You mention ARC… aren’t you not supposed to explicitly release objects under
> ARC? Am I misunderstanding?
>
> Sandor
>
>> On Aug 23, 2023, at 18:48, JPH via Cocoa-dev <email@hidden>
>> wrote:
>>
>> Hello friends,
>>
>> The enclosed procedure is producing a NSImage from another NSImage, by
>> cropping in a NSRect.
>> The resulting sub-Image feeds an imageView in the APP Interface and the
>> may change frequently, then being hopefully disposed by ARC
>> The last line of the procedure :
>> NSImage *subImage = [[NSImage alloc] initWithCGImage:subImageRef
>> size:NSZeroSize];
>> Generates a leak problem.
>>
>> it is quite unclear if i should release or not the subImageRef.
>>
>> If I release, the app crashes soon after.
>> If I don’t release, Instrument/Leaks reports a leak of subImageRef after
>> each call.
>>
>> Obviously, ARC never releases subImageRef which, as far as I
>> understand, is owned by the subImage after the call.
>>
>> Digging the web , apple doc, and various AI, did not provide a solution to
>> this dilemme.
>>
>> I would greatly appreciate some help on this
>>
>> Happy summer..
>> JP
>>
>> C
>> //=============================================================================
>> + (NSImage *)getSubImageFromImage:(NSImage *)image atRect:(NSRect)rect
>> //=============================================================================
>> {
>> // Convert the NSImage to a CGImageRef
>> CGImageRef imageRef= nil, subImageRef= nil;
>> imageRef = [image CGImageForProposedRect:NULL context:nil hints:nil];
>> if (imageRef == nil ) { return nil; } // Failed to create CGImageRef
>> // Get the subimage from the CGImageRef
>> subImageRef = CGImageCreateWithImageInRect(imageRef, rect);
>> CFRelease(imageRef);
>> if(subImageRef == nil ) { return nil; } // Failed to create subImageRef
>> // Convert the subimage CGImageRef to an NSImage
>> NSImage *subImage = [[NSImage alloc] initWithCGImage:subImageRef
>> size:NSZeroSize];
>> // CFRelease(subImageRef); // it is quite unclear if i should release the
>> subImageRef
>> //If I release, the app crashes soon after.
>> // If i dont release Instrument/Leaks report a leak at next call.
>> // The subImage goes to a NSbutton in the interface, and will also soon be
>> replaced by another one, producing a leak of subImageRef
>>
>> return subImage;
>> }// getSubImageFromImage
>> _______________________________________________
>>
>> Cocoa-dev mailing list (email@hidden)
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden