Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于自动内存管理的理解 #7

Closed
zedzhao opened this issue Dec 22, 2016 · 1 comment
Closed

关于自动内存管理的理解 #7

zedzhao opened this issue Dec 22, 2016 · 1 comment

Comments

@zedzhao
Copy link

zedzhao commented Dec 22, 2016

想确认下理解的对不对,
判断 retainCount = 2的对象是应该被release的对象的依据是, 在themeColorPool中有retain了一次, 然后在 copy的时候又retain了一次, 而其他正在使用的对象的retainCount一定是大于2的。
这样理解对么?

// 遍历主题色池(移除应该被回收的对象)
for (NSDictionary *dict in [[self themeColorPool] copy]) {
// 取出key
NSString *objectKey = nil;
for (NSString *key in [dict allKeys]) {
if (![key isEqualToString:PYTHEME_COLOR_ARGS_KEY]) {
objectKey = key;
break;
}
}
// 取出对象
id object = [dict valueForKey:objectKey];
// 取出对象的引用计数
NSInteger retainCount = [[object valueForKey:@"retainCount"] integerValue];
if (retainCount == 2) { // 对象应该被回收了
[[self themeColorPool] removeObject:dict];
}
}

@ko1o
Copy link
Owner

ko1o commented Dec 22, 2016

@zedzhao 使用retainCount 还是不够合理的,现在最新的方案是采用NSMapTable对对象采取弱引用,具体讨论见:#2

@zedzhao zedzhao closed this as completed Dec 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants