未释放内存泄漏 class_copyPropertyList
memory leak on not freed class_copyPropertyList
因此,分析我的应用程序时存在一些漏洞。一个是 objc_property_t *properties = class_copyPropertyList(self.class, NULL);
没有被释放。
但是当我添加 free(properties)
alloc: *** error for object 0x10b773f58: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
代码看起来像这样:
- (void) encodeWithCoder:(NSCoder *)aCoder {
objc_property_t *properties = class_copyPropertyList(self.class, NULL);
while (*properties != '[=11=]') {
NSString *pname = [NSString stringWithUTF8String:property_getName(*properties)];
const char * attrs = property_getAttributes(*properties);
// do something here and set the value to be encoded
}
properties ++;
}
free(properties);
}
An array of pointers of type objc_property_t describing the properties declared by the class. Any properties declared by superclasses are not included. The array contains *outCount pointers followed by a NULL terminator. You must free the array with free().
代码有效,但有漏洞。现在当我添加 free(properties)
它崩溃了。
您正在递增循环中的 properties
指针,因此在您调用 free(properties)
时它不再引用相同的内存地址。因此 pointer being freed was not allocated
消息。
您需要以非破坏性方式迭代属性,或者在循环之前将原始指针值复制到另一个指针变量。
类似
unsigned int count;
objc_property_t *properties = class_copyPropertyList([self class], &count);
for (unsigned int i = 0; i < count; i++) {
objc_property_t property = properties[i];
// ...
}
free(properties);
或
objc_property_t *properties = class_copyPropertyList([self class], NULL);
objc_property_t *iterationPointer = properties;
while (*iterationPointer != NULL) {
// ...
iterationPointer++;
}
free(properties);
因此,分析我的应用程序时存在一些漏洞。一个是 objc_property_t *properties = class_copyPropertyList(self.class, NULL);
没有被释放。
但是当我添加 free(properties)
alloc: *** error for object 0x10b773f58: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
代码看起来像这样:
- (void) encodeWithCoder:(NSCoder *)aCoder {
objc_property_t *properties = class_copyPropertyList(self.class, NULL);
while (*properties != '[=11=]') {
NSString *pname = [NSString stringWithUTF8String:property_getName(*properties)];
const char * attrs = property_getAttributes(*properties);
// do something here and set the value to be encoded
}
properties ++;
}
free(properties);
}
An array of pointers of type objc_property_t describing the properties declared by the class. Any properties declared by superclasses are not included. The array contains *outCount pointers followed by a NULL terminator. You must free the array with free().
代码有效,但有漏洞。现在当我添加 free(properties)
它崩溃了。
您正在递增循环中的 properties
指针,因此在您调用 free(properties)
时它不再引用相同的内存地址。因此 pointer being freed was not allocated
消息。
您需要以非破坏性方式迭代属性,或者在循环之前将原始指针值复制到另一个指针变量。
类似
unsigned int count;
objc_property_t *properties = class_copyPropertyList([self class], &count);
for (unsigned int i = 0; i < count; i++) {
objc_property_t property = properties[i];
// ...
}
free(properties);
或
objc_property_t *properties = class_copyPropertyList([self class], NULL);
objc_property_t *iterationPointer = properties;
while (*iterationPointer != NULL) {
// ...
iterationPointer++;
}
free(properties);