"Learn Swift from Scratch" Study Notes (Day 62) - Memory Managed Objects and Unmanaged Objects of the Core Foundation Framework

Original article, welcome to reprint. Please indicate: Guan Dongsheng's blog

 

memory managed object

When calling the Core Foundation function to obtain an object in Swift, the object is divided into: memory managed object and memory unmanaged object.

Memory managed objects are managed by the compiler. We do not need to call the CFRetain function to obtain object ownership, nor do we need to call the CFRelease function to give up object ownership.

The way to get these memory managed objects is to use the CF_RETURNS_RETAINED or CF_RETURNS_NOT_RETAINED annotation declaration, sample code:

-(CGPathRef)makeToPath CF_RETURNS_RETAINED
{
    UIBezierPath* triangle = [UIBezierPath bezierPath];
    [triangle moveToPoint:CGPointZero];
    [triangle addLineToPoint:CGPointMake(self.view.frame.size.width,0)];
    [triangle addLineToPoint:CGPointMake(0, self.view.frame.size.height)];
    [triangle closePath];
    CGPathRef theCGPath = [triangle CGPath];
    return CGPathCreateCopy(theCGPath);
}

 

Memory managed objects are simpler to use and don't require us to do anything extra.

 

func CFStringCreateWithCString(_ alloc: CFAllocator!,
        _ cStr: UnsafePointer<Int8>,
         _ encoding: CFStringEncoding) -> CFString! //Memory managed object
 
func CFHostCreateCopy(_ alloc: CFAllocator?,
         _ host: CFHost) -> Unmanaged<CFHost> //Memory unmanaged object

 

 

 

memory unmanaged object

Memory unmanaged objects are memory that needs to be managed by the programmer. This is due to the fact that the method that gets the object is not declared with the CF_RETURNS_RETAINED or CF_RETURNS_NOT_RETAINED annotation, the compiler cannot help manage memory. In specific use, we can use the method in the previous section to determine whether it is a non-memory managed object.

Memory unmanaged objects are a bit troublesome to use, and should be handled accordingly according to the method of obtaining ownership.

1. If a function name contains Create or Copy, the caller obtains the object and also obtains the ownership of the object. The return value Unmanaged<T> needs to call the takeRetainedValue() method to obtain the object. When the caller no longer uses the object, the Swift code needs to call the CFRelease function to give up the object ownership, because Swift is ARC memory management.

 

2. If a function name contains Get, the caller will not get the ownership of the object when acquiring the object. The return value Unmanaged<T> needs to call the takeUnretainedValue() method to obtain the object.

The sample code is as follows:

 

let host: CFHost = CFHostCreateWithName(kCFAllocatorDefault,
        Ê"127.0.0.1").takeRetainedValue()
       
let hostNames: CFArray = CFHostGetNames(host, nil)!.takeUnretainedValue()
 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327061908&siteId=291194637