Memory Leaks for typedef struct

十年热恋 提交于 2020-01-13 11:00:10

问题


I m trying to remove memory leaks. Below is my code.

currentTime = CFAbsoluteTimeGetCurrent();

CFTimeZoneRef currentTimeZone = CFTimeZoneCopyDefault();

todaysDate = CFAbsoluteTimeGetGregorianDate(currentTime, currentTimeZone);


[currentTimeZone release];
currentTimeZone = nil;

Warning:::/myclass.m:87: warning: invalid receiver type 'CFTimeZoneRef'

how to release memory for typedef const struct?


回答1:


With CFxxxCopyxxx you should use CFRelease.

CFRelease(currentTimeZone);

Edit:

There are CoreFoundation classes that are Toll-Free bridged and your original statement was not leaking but just needed a cast. I would still recommend using CFRelease since you were working directly with CoreFoundation.

[(NSTimeZone*)currentTimeZone release];



回答2:


From the docs:

If you own an object, it is your responsibility to relinquish ownership (using CFRelease) when you have finished with it.




回答3:


You're using a CoreFoundation call, so instead of using Cocoa's release, you will want to call:

CFRelease( currentTimeZone);

in order to release the copied data.




回答4:


try relesing addressBook after allPeople

CFRelease(allPeople); CFRelease(addressBook);

This worked for me.



来源:https://stackoverflow.com/questions/6679815/memory-leaks-for-typedef-struct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!