search

Home  >  Q&A  >  body text

ios - Memory management: CFStringRef manual release problem

Recently I was looking at third-party source code, which involved some data types in the Core Foundation framework, and encountered some memory management crashes. I have forgotten many things about C voice. I hope to get guidance from the master here.

question:

//在网上例子上看到的这种方式创建的一个CFString的数组
CFStringRef keys[2];
keys[0] = CFSTR("key1");
keys[1] = CFSTR("key2");
//所以Core Foundation框架中的对象都不支持ARC,需要手动释放
CFRelease(keys);

As long as it runs, the following crash will occur

But I tried the code below, created a CFStringRef and then released it manually, it was no problem

CFStringRef aSTR = CFSTR("A STRING");
CFRelease(aSTR);

Actually, I am still confused about the method of creating arrays that I see on the Internet. I checked the usage of the Core Foundation framework on the Internet, but there are still relatively few, and I have no way to start.

Waiting online, hoping to get an effective answer, thank you~

phpcn_u1582phpcn_u15822781 days ago873

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-06-10 09:50:13

    keys is a pointer variable and is stored in the stack. It does not need to be released manually.
    keys[0] keys[1] is CFStr and needs to be released manually

    reply
    0
  • Cancelreply