Re: CFDictionarySetValue and ARC
Re: CFDictionarySetValue and ARC
- Subject: Re: CFDictionarySetValue and ARC
- From: Kyle Sluder <email@hidden>
- Date: Mon, 17 Oct 2011 14:16:04 -0700
On Mon, Oct 17, 2011 at 1:56 PM, Brent Fulgham <email@hidden> wrote:
> I have a routine that creates a CFSocketRef which will later be
> assigned to the member variable of an object:
> ==========================================================
> - (CFSocketRef)createDataSocketMonitor:(int)socket
> withCallback:(CFSocketCallBack)socketCallBack
> ofCallBackType:(CFSocketCallBackType)cbType
> {
> CFSocketContext context = {0, (__bridge void*)self, NULL, NULL, NULL };
> CFSocketRef runSocket =
> CFSocketCreateWithNative(kCFAllocatorDefault, socket, cbType,
> socketCallBack, &context);
>
> if (!runSocket)
> {
> // ... Error logging, etc.
> return 0;
> }
>
> CFRunLoopSourceRef rls =
> CFSocketCreateRunLoopSource(kCFAllocatorDefault, runSocket, 0);
> if (!rls)
> {
> // ... Error logging, etc.
> CFSocketInvalidate(runSocket);
> CFRelease(runSocket);
> return 0;
> }
>
> CFRunLoopAddSource(CFRunLoopGetCurrent (), rls, kCFRunLoopDefaultMode);
> CFRelease(rls);
>
> return runSocket;
> }
> ==========================================================
>
> LLVM complains here that runSocket leaves with a +1 state, and might
> be leaked. However, I'm pretty sure I don't want to release inside
> this method because the socket might get cleaned up before my caller
> can CFRetain the return value.
>
> Is there a better approach to returning a CoreFoundation type from a
> routine like this?
Your method should be named -newDataSocketMonitor:... since you are
handing off a +1 reference to the caller. The caller can then deal
with the object as appropriate.
--Kyle Sluder
_______________________________________________
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