objective-c - 内存泄漏 NSBlockOperation
问题描述
我用在该操作中声明的对象声明了 NSBlockOperation。由于内存问题,我的应用程序经常崩溃。感谢任何提示与一个很好的解释,这花了几个小时仍然没有成功。
运行时:内存问题 -(5 种泄露类型):1 个NSExactBlockVariable实例泄露
- (EMUserInfoOperation*)loadingLocalModelOperationWithColor:(EMOutfitColor)outfitColor gender:(EMGender)gender {
__block EMUserInfoOperation* operation = [EMUserInfoOperation blockOperationWithBlock:^{
NSURL* remoteURL = [NSURL URLWithString:self.settings[kEMRemoteUrlKey]];
EMOutfitModel* model = nil;
if (remoteURL == nil) {
model = [[EMDomainDataLoader sharedLoader] loadEmbededOutfitNamed:self.name gender:gender];
} else {
model = [[EMDomainDataLoader sharedLoader] loadCachedOutfitNamed:self.name withVersion:self.version gender:gender];
}
[model syncApplyTextureFromPath:[self texturePathForColor:outfitColor] textureSampler:EMTextureSamplerColor];
NSString *alphaPath = [self texturePathForAlpha];
if(alphaPath.length > 0) {
[model syncApplyTextureFromPath:alphaPath textureSampler:EMTextureSamplerAlpha];
}
operation.userInfo = model;
}];
return operation;
}
解决方案
我猜你的EMUserInfoOperation
对象对创建操作的块有很强的引用。而且这个块也有对EMUserInfoOperation
对象的强引用,因为它捕获了operation
变量。所以你有一个保留周期。
您可以EMUserInfoOperation
通过执行以下操作使块仅弱引用对象:
EMUserInfoOperation* operation;
__block __weak typeof(operation) weakOperation;
weakOperation = operation = [EMUserInfoOperation blockOperationWithBlock:^{
typeof(operation) strongOperation = weakOperation;
if (strongOperation) {
// ...
strongOperation.userInfo = model;
}
}];
return operation;
推荐阅读
- python - 如何使用正则表达式返回字符串中匹配项的列表?
- android - Firebase 应用分发 - 找不到 APK。确保首先通过运行 ./gradlew assemble[Variant] 构建
- java - service.SecurityServiceImpl 中的字段 authenticationManager 需要一个 'org.springframework.security.authentication.AuthenticationManager' 类型的 bean
- python - 具有高级重写和简化功能的 SymPy
- c++ - 保证统一支撑数组初始化的复制省略 - 自 C++17 以来这不应该是强制性的吗?
- python - 如何将不同的列表减少到完全相同的长度?
- reactjs - 我不能在 .js 文件中使用 html 标签(jsx 代码)
- snakemake - 大型工作流的 DAG 完成后 Snakemake 挂起
- hadoop - 物理节点和虚拟节点有什么区别?
- java - Java -Executable JAR- 创建 FileWriter 时出错:FileNotFoundException 访问被拒绝