BloodMagic
BloodMagic copied to clipboard
Poor errors handling and memory management
For example :
- (NSSet *)protocols {
if (!_protocols) {
Protocol *__unsafe_unretained *protocols;
uint protocolsCount = 0;
protocols = class_copyProtocolList(_objcClass, &protocolsCount);
NSMutableSet *mutableProtocols = [NSMutableSet setWithCapacity:protocolsCount];
for (uint protocolIndex = 0; protocolIndex != protocolsCount; ++protocolIndex) {
Protocol *protocol = protocols[protocolIndex];
[mutableProtocols addObject:protocol];
}
// What makes you sure "protocols" variable is not NULL ? free(protocols);
_protocols = [NSSet setWithSet:mutableProtocols];
}
return _protocols;
}
- (void)collectClasses
{
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
uint classesCount;
const char *imageName = class_getImageName(object_getClass(self));
const char **classNames = objc_copyClassNamesForImage(imageName, &classesCount);
for (uint index = 0; index < classesCount; index++) {
Class nextClass = objc_getClass(classNames[index]);
// what if push_back() throws an exception? _cachedClasses.push_back(nextClass);
// you'll never reach ::free() statement
}
free(classNames);
Premature optimization?
What should I do in case when [NSArray new]
returns nil
?
I understand what you mean, but, IMHO, there a lot of cases when something might be broken, should we cover all them?
Anyway, if you see a big problem with this part of code - feel free to send a PR.
It's not about optimizations. It's rather about proper resources management (memory) and avoiding crashes.
What should I do in case when [NSArray new] returns nil?
Nothing since it is safe to send messages to a nil
object
Still, the case with invoking ::free( NULL )
is completely different since it will eventually lead to a crash.
there a lot of cases when something might be broken, should we cover all them?
Critical crashes should be addressed in no doubt. Still, you are the maintainer and it's up to you setting the priorities.
Thank you, I've got your point and will check and fix such issues.
7.20.3.2 The free function
Synopsis
#include <stdlib.h> void free(void *ptr); Description
The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs.
See ISO-IEC 9899.