我有一些不透明的类型,并实现了模仿 Core Foundation 的引用计数语义。除了 clang 警告我半有效的潜在泄漏之外,这已经足够好用了。
MyTypeRef MyTypeRefCreateWithSomething(…) {
MyTypeRef value = MyTypeRefCreate(…);
MyTypeRef adjustedValue = MyTypeRefCreate(…);
MyTypeRefRelease(value);
return adjustedValue; // clang says that this might be a leak of `value`…
// which is only true if something goes wrong with reference counting.
}
我正在寻找类似attribute_cf_consumed
或简单的方法来制作我自己的版本。
作为参考,这就是我目前使警告静音的方式。它有效,但在我看来,它与只留下警告一样糟糕。(我 ifdef围绕's call to的if
语句)MytypeRelease
free
void MyTypeRelease(MyTypeRef degree) {
((MyMutableTypeRef)degree)->refCount--;
#ifndef __clang_analyzer__
if ((degree->refCount <= 0) && !MyTypeIsNull(degree)) {
#endif /*__clang_analyzer__*/
free((CTJUMTMutableScaleDegree)degree);
#ifndef __clang_analyzer__
}
#endif /*__clang_analyzer__*/
}