在此功能中,根据Xcode的仪器泄漏工具,我在两个位置发生内存泄漏。
我没有启用ARC并且无法激活它,所以我必须以另一种方式管理它。
+(NSArray *)changePolygonFrom:(NSArray *)polygon size:(float)size{
int nmrOfPoints = [polygon count];
double x[nmrOfPoints];
double y[nmrOfPoints];
for (int i = 0; i < nmrOfPoints; i++) {
CGPoint p = [[polygon objectAtIndex:i] CGPointValue];
x[i] = p.x;
y[i] = p.y;
}
insetPolygon(x,y,nmrOfPoints,size);
NSMutableArray *rPolygon = [[NSMutableArray alloc] initWithCapacity:nmrOfPoints];
for(int i = 0; i < nmrOfPoints; i++){
CGPoint p = CGPointMake(x[i], y[i]);
NSValue *val = [NSValue valueWithCGPoint:p];
[rPolygon addObject:val];
}
return rPolygon;
}
这就是我所说的
-(void)fixFuzArrays{
if (fuzzFix) {
largeFuzzPolygon = [Triangulation changePolygonFrom:normalPolygon size:(shortestDistance/2)];
smallFuzzPolygon = [Triangulation changePolygonFrom:normalPolygon size:(shortestDistance/6)];
}else{
largeFuzzPolygon = [Triangulation changePolygonFrom:normalPolygon size:-(shortestDistance/2)];
smallFuzzPolygon = [Triangulation changePolygonFrom:normalPolygon size:-(shortestDistance/6)];
}
}
如果我在rPolygon上使用自动发布,我会在对象iFrame和oFrame上获得BAD_ACCESS,这是前一个函数的返回产品
-(void)drawAreaFuzzWithOuterFrame:(NSArray *)oFrame andInnerFrame:(NSArray *)iFrame withColor:(ccColor4B)c{
for(int i = 0; i < [oFrame count]; i++){
CGPoint oP1 = [[oFrame objectAtIndex:i] CGPointValue];
CGPoint iP1 = [[iFrame objectAtIndex:i] CGPointValue];
CGPoint oP2;
CGPoint iP2;
if(i == [oFrame count]-1){
oP2 = [[oFrame objectAtIndex:0] CGPointValue];
iP2 = [[iFrame objectAtIndex:0] CGPointValue];
}else{
oP2 = [[oFrame objectAtIndex:i+1] CGPointValue];
iP2 = [[iFrame objectAtIndex:i+1] CGPointValue];
}
GLfloat vertices[] = {
oP1.x,oP1.y,
oP2.x,oP2.y,
iP1.x,iP1.y,
iP2.x,iP2.y,
};
GLubyte colors[] = {
c.r,c.g,c.b,c.a,
c.r,c.g,c.b,c.a,
c.r,c.g,c.b,0,
c.r,c.g,c.b,0,
};
BEdrawGLShapeWithVertices(vertices, colors);
}
}
答案 0 :(得分:1)
您必须在返回数组之前自动释放数组,然后将数组保留在接收它的方法中
将autorelease
添加到NSMutableArray
NSMutableArray *rPolygon = [[[NSMutableArray alloc] initWithCapacity:nmrOfPoints] autorelease];
然后将retain保留在使用您的函数的方法上,但您必须稍后将其释放
NSArray *myArray = [[MyClass changePolygonFrom:array size:size] retain];
不要忘记稍后发布myArray
答案 1 :(得分:0)
您必须将NSMutableArray标记为自动释放:
NSMutableArray *rPolygon = [[[NSMutableArray alloc] initWithCapacity:nmrOfPoints] autorelease];
如果你不这样做,你就是创建它,保留它,然后返回它。它从未发布过。
答案 2 :(得分:0)
在声明 rPolygon 时忘了自动释放:
NSMutableArray *rPolygon = [[[NSMutableArray alloc] initWithCapacity:nmrOfPoints] autorelease];
修改强>
您似乎没有保留+(NSArray *)changePolygonFrom:(NSArray *)polygon size:(float)size
方法的结果。
尝试更改保留largeFuzzPolygon
和smallFuzzPolygon
的代码(请记得在dealloc方法中释放它们):
-(void)fixFuzArrays{
[largeFuzzPolygon release];
[smallFuzzPolygon release];
if (fuzzFix) {
largeFuzzPolygon = [[Triangulation changePolygonFrom:normalPolygon size:(shortestDistance/2)] retain];
smallFuzzPolygon = [[Triangulation changePolygonFrom:normalPolygon size:(shortestDistance/6)] retain];
}else{
largeFuzzPolygon = [[Triangulation changePolygonFrom:normalPolygon size:-(shortestDistance/2)] retain];
smallFuzzPolygon = [[Triangulation changePolygonFrom:normalPolygon size:-(shortestDistance/6)] retain];
}
}
或更好我建议您合成它们并使用此代码:
-(void)fixFuzArrays{
if (fuzzFix) {
self.largeFuzzPolygon = [[Triangulation changePolygonFrom:normalPolygon size:(shortestDistance/2)] retain];
self.smallFuzzPolygon = [[Triangulation changePolygonFrom:normalPolygon size:(shortestDistance/6)] retain];
}else{
self.largeFuzzPolygon = [[Triangulation changePolygonFrom:normalPolygon size:-(shortestDistance/2)] retain];
self.smallFuzzPolygon = [[Triangulation changePolygonFrom:normalPolygon size:-(shortestDistance/6)] retain];
}
}