Passing an indirect pointer to an Objective-C pointer to "CFReadStreamRef *" is prohibited by ARC

I want to use CFStreamCreatePairWithSocketToHost with NSInput - and OutputStream s. I have two ivars NSInputStream *_inputStream and NSOutputStream *_outputStream .

The following are two error messages:

 CFStreamCreatePairWithSocketToHost(NULL, (__bridge_retained CFStringRef)self.hostname, self.port, (CFReadStreamRef *)&_inputStream, (CFWriteStreamRef *)&_outputStream); 

error: pointing an indirect pointer to an Objective-C pointer to ' CFReadStreamRef * ' (aka ' struct __CFReadStream ** ') is not allowed using ARC Error: casting an indirect pointer to an Objective-C pointer to ' CFWriteStreamRef * ' (aka ' struct __CFWriteStream ** ') is prohibited using ARC

How can i fix this? I tried using __bridge , but I got similar error messages.

+6
source share
2 answers

Pass a pointer to the actual variables CFReadStreamRef and CFWriteStreamRef , and then pass when assigned to your NSTypes. Primarily:

 CFThingRef cfThing = NULL; CFFunctionGetThing(&cfThing); NSThing * nsThing = cfThing; 

Of course, you will need to add a suitable bridge binding and perform all the relevant reference counting operations for cfThing .

+4
source

Think about what ARC does and what __bridge cast does: ARC takes responsibility for maintaining / releasing NSObjects. The __bridge transfer transfers this responsibility. For example, __bridge_retained saves an NSString, discards a pointer to CFStringRef, and is responsible for performing the corresponding release operation on those using CFStringRef.

This cannot work with a pointer to NSObject * or a pointer to a base ref. A pointer points to a memory location. Any NSObject * or Core Foundation objects can be stored there.

You need two variables: one for NSInputStream * and one for CFReadStreamRef. Use the appropriate bridge to move NSInputStream * to CFReadStreamRef. Now you have what you want and can pass the address.

0
source

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


All Articles