Memory leaks SKShapeNode-CGPathCreateWithRoundedRect导致';物体的潜在泄漏';警告

Memory leaks SKShapeNode-CGPathCreateWithRoundedRect导致';物体的潜在泄漏';警告,memory-leaks,sprite-kit,skshapenode,Memory Leaks,Sprite Kit,Skshapenode,我有一个SKShapeNode,我将该形状设置为RoundedRectangle,但当我分析代码时,我收到一条警告,指出存在对象的潜在泄漏 我的界面文件如下: #import <SpriteKit/SpriteKit.h> @interface letterSpace : SKShapeNode -(instancetype) initWithCharacter:(NSString *)character xPosition:(float)x yPosition:(float)y

我有一个SKShapeNode,我将该形状设置为RoundedRectangle,但当我分析代码时,我收到一条警告,指出存在对象的潜在泄漏

我的界面文件如下:

#import <SpriteKit/SpriteKit.h>

@interface letterSpace : SKShapeNode

-(instancetype) initWithCharacter:(NSString *)character xPosition:(float)x yPosition:(float)y device:(NSString *)device;
    @property(nonatomic, assign) BOOL occupied;
    @property(nonatomic, assign) BOOL whiteSpace;
    @property(nonatomic, retain) NSString *character;
    @property (atomic) float secondLineX;
    @property (atomic) float secondLineY;


@end
#导入
@接口字母空间:SKShapeNode
-(instancetype)initWithCharacter:(NSString*)字符xPosition:(float)x yPosition:(float)y设备:(NSString*)设备;
@财产(非原子,分配)布尔占用;
@属性(非原子,赋值)布尔空格;
@属性(非原子,保留)NSString*字符;
@属性(原子)float secondLineX;
@属性(原子)二次浮动;
@结束
然后是实现

 #import "letterSpace.h"

@implementation letterSpace
-(instancetype) initWithCharacter:(NSString *)character xPosition:(float)x yPosition:(float)y device:(NSString *)device{
    self = [super init];
        if(self){

            if ([device isEqualToString:@"IPHONE"]) {
                [self setPath:CGPathCreateWithRoundedRect(CGRectMake(0, 0, 30, 2), 1, 1, nil)]; // Potential Leak of an object on this line
                _secondLineX = 36*8;
                _secondLineY = -36;
            }else{
                [self setPath:CGPathCreateWithRoundedRect(CGRectMake(0, 0, 60, 3.5), 1, 1, nil)]; // Potential Leak of an object on this line
                _secondLineX = 575;
                _secondLineY = -71;
            }
            self.strokeColor = [UIColor clearColor];
            if ([character  isEqual: @" "]) {
               self.occupied = TRUE;
               self.fillColor = [UIColor clearColor];
               self.character = character;
               self.whiteSpace = TRUE;
            }else{
                self.whiteSpace = FALSE;
                self.occupied = FALSE;
                self.fillColor = [UIColor colorWithRed:80.0f/255.0f
                                                green:227.0f/255.0f
                                                                 blue:194.0f/255.0f
                                                                alpha:1];
            }
            if (y <8) {
                self.position = CGPointMake(x, 0);
            }
            else{
                self.position = CGPointMake(x - _secondLineX, _secondLineY);
            }


            self.name = character;
        }
        return self;
    }

@end
#导入“letterSpace.h”
@实现字母空间
-(instancetype)initWithCharacter:(NSString*)字符xPosition:(float)x yPosition:(float)y设备:(NSString*)设备{
self=[super init];
如果(自我){
如果([设备IsequalString:@“IPHONE”]){
[self-setPath:CGPathCreateWithRoundedRect(CGRectMake(0,0,30,2,1,1,nil)];//此行对象的潜在泄漏
_secondLineX=36*8;
_二线y=-36;
}否则{
[self-setPath:CGPathCreateWithRoundedRect(CGRectMake(0,0,60,3.5),1,1,nil)];//此行对象的潜在泄漏
_secondLineX=575;
_二线y=-71;
}
self.strokeColor=[UIColor clearColor];
if([字符等位:@“”){
自我占据=真实;
self.fillColor=[UIColor clearColor];
自我性格=性格;
self.whiteSpace=TRUE;
}否则{
self.whiteSpace=FALSE;
自我占据=错误;
self.fillColor=[UIColor colorWithRed:80.0f/255.0f
绿色:227.0f/255.0f
蓝色:194.0f/255.0f
α:1];
}

如果(y当使用核心基础对象时,如果调用函数*:

,则拥有一个对象。
  • 在名称中嵌入“创建”
  • 在名称中嵌入“副本”
*但是,这不适用于返回Objective-C对象的方法

如果您是对象的所有者,则必须在使用
CFRelease
(或相应的特定于类型的变体)完成对象的使用后放弃所有权

在ARC环境中,编译器不自动管理核心基础对象的生命周期。您必须调用<代码> CFRetain < /代码>和<代码> cFrRule<代码>(或对应的类型特定变体)

<>你也需要告诉编译器关于对象的所有权,如果你在ObjtoFe C和Core Foundation对象之间进行转换:

    >代码>桥/<代码>在Objto-C与核心基础之间传递指针,不进行所有权转移; >BaseGraveSt留住< /COD> >或<代码> CFBridgingRetain < /代码>将ObjuleC指针投向一个核心基础指针,并将所有权转移给您。您负责调用<代码> cFrRule<代码>或相关函数以放弃该对象的所有权;
  • \uuuuu bridge\u transfer
    CfBridgegRelease
    将非Objective-C指针移动到Objective-C,并将所有权转移到ARC。ARC负责放弃对象的所有权
因此,在你的情况下:

<代码>路径有一种类型的<代码> CgPaTRAFF ,这是从核心框架到核心基础内存管理约定的。因此,我们应用规则“如果你调用的方法有一个“创建在名称中的嵌入-你拥有它”,不管它是手动的(手动的)还是自动的都无所谓。(ARC环境不自动管理核心基础对象的生命周期)内存管理。您必须释放这个对象。

CGPathRef path = CGPathCreateWithRoundedRect(CGRectMake(0, 0, 30, 2), 1, 1, nil);
[self setPath:path];
CGPathRelease(path);
您可以使用Cocoa模拟
bezierPathWithRoundedRect:(NSRect)rect xRadius:(CGFloat)xRadius yRadius:(CGFloat)yRadius

另外,我建议您在
CGPathCreateWithRoundedRect
中使用
NULL
而不是
nil


您的漏洞可能在于您使用自定义类的方式,而不是类代码本身。