我正在解析 XML 文件中的数据,并相信我找到了一个保留周期。我没有最好的理解如何解决它。简而言之,我正在解析一个块中的一堆浮点数,这导致我的内存使用量超过了 250 MB。不好。这是代码:
[TBXML iterateElementsForQuery:@"library_animations" fromElement:rootElement withBlock:^(TBXMLElement *anElement) {
[self parseLibraryAnimationsElement:anElement];
}];
上面的方法调用了下面的方法:
- (void)parseLibraryAnimationsElement:(TBXMLElement *)element
{
[TBXML iterateElementsForQuery:@"animation" fromElement:element withBlock:^(TBXMLElement *anElement) {
TBXMLElement *extraElement = [TBXML childElementNamed:@"extra" parentElement:anElement];
TBXMLElement *techniqueElement = [TBXML childElementNamed:@"technique" parentElement:extraElement];
// grab float data
**NSData *floatData = [self extractFloatArrayFromElement:techniqueElement];**
// append data per animation ID
NSString *key = [NSString stringWithFormat:@"#%@", [TBXML valueOfAttributeNamed:@"id" forElement:anElement]];
[self.root.sortedAnimationKeys addObject:key];
// use float data to store color information for this frame
for (COLLADAMeshGeometry *geometry in self.root.geometries.allValues) {
AGLKMesh *mesh = geometry.mesh;
NSMutableData *dynamicColorData = [NSMutableData data];
GLKVector3 *newColorInfo = (GLKVector3 *)[floatData bytes];
// cycle through vertices for this mesh and use color float array information
for (int i = 0; i < mesh.numberOfVertices; i++) {
AGLKMeshVertex *staticVertex = [mesh vertexAtIndex:i];
GLKVector3 newColor = newColorInfo[staticVertex->colorIndex];
GLKVector4 color = GLKVector4Make(newColor.x, newColor.y, newColor.z, 1.0);
[dynamicColorData appendBytes:&color length:sizeof(color)];
}
// get key and store in animation dictionary on the mesh
[mesh.animationBufferDictionary setObject:dynamicColorData forKey:key];
}
}];
// sort animation keys
if (self.root.sortedAnimationKeys.count > 0) {
[self.root.sortedAnimationKeys sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
}
** ... ** 之间的方法也在下面。这就是我认为问题所在。字符串未正确释放。
- (NSData *)extractFloatArrayFromElement:(TBXMLElement *)element
{
// element is <source>
NSMutableData *floatData = [NSMutableData data];
TBXMLElement *floatElement = [TBXML childElementNamed:@"float_array" parentElement:element];
NSString *stringValues = [TBXML textForElement:floatElement];
NSArray *values = [stringValues componentsSeparatedByString:@" "];
for (NSString *value in values) {
float floatValue = [value floatValue];
[floatData appendBytes:&floatValue length:sizeof(floatValue)];
}
return floatData;
}
我说它在这里的某个地方,因为当我使用分配工具时,我会得到大量的 CFStrings,如下面的屏幕截图所示。我不确定这是否有足够的信息,但如果有人能看到任何问题,请告诉我。否则,也许我可以朝着正确的方向迈出一步。
当我深入到 CFString(不可变)行时,它有许多行是在[NSString componentsSeparatedByString:]
.