La recolección de basura de los objetos de la Fundación Core
-
21-09-2019 - |
Pregunta
Al ejecutar el analizador estático en este pedazo de código:
- (id) readForeignPref
{
CFPropertyListRef matchStyle = CFPreferencesCopyAppValue(CFSTR("PBXFindMatchStyle"), CFSTR("com.apple.Xcode"));
return [(id)matchStyle autorelease];
}
produce la siguiente advertencia:
llamada a la función 'CFPreferencesCopyAppValue' devuelve un objeto de infraestructura de núcleo con un (referencia poseer) 1 retener recuento. Fundación objetos obligatorios no son automáticamente recogidos de la basura.
¿Es esta una advertencia que debería fijar con algo feo como lo siguiente:
- (id) readForeignPref
{
CFPropertyListRef matchStyle = CFPreferencesCopyAppValue(CFSTR("PBXFindMatchStyle"), CFSTR("com.apple.Xcode"));
id nsMatchStyle = [(id)matchStyle copy];
if (matchStyle) {
CFRelease(matchStyle);
}
return [nsMatchStyle autorelease];
}
O es sólo un falso positivo, dado que el objeto copiado es gratuito puente?
Solución
Prueba esto:
- (id) readForeignPref
{
CFPropertyListRef matchStyle = CFPreferencesCopyAppValue(CFSTR("PBXFindMatchStyle"), CFSTR("com.apple.Xcode"));
return [(id)CFMakeCollectable(matchStyle) autorelease];
}
Sin la CFMakeCollectable
, esto se fuga en GC, porque un CFRetain
es diferente a un -retain
ObjC. Un CFRetain
desactiva la recolección de basura de ese objeto, y necesita CFMakeCollectable
para habilitarlo.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow