我从下面的代码中收到了一堆内存泄漏...我试图以更好的方式重写它,但实际上无法想到其他任何东西。建议将不胜感激。下面是邮件发送到的对象。
Packet *packet;
packet = [PacketAudioBuffer packetWithData:data];
PacketAudioBuffer.m文件 - >
+ (id)packetWithData:(NSData *)data
{
int totalSize = [data length];
int packetNumber = [data pm_int32AtOffset:4];
return [[self alloc] initWithAudioBufferData:data totalSize:totalSize packetNumber:packetNumber];
}
- (id)initWithAudioBufferData:(NSData *)data
totalSize:(UInt32)totalSize
packetNumber:(UInt32)packetNumber
{
if ((self = [super initWithType:PacketTypeAudioBuffer]))
{
// this is where audioBufferData is set.
self.audioBufferData = data;
self.totalSize = totalSize;
self.packetNumber = packetNumber;
}
return self;
}
答案 0 :(得分:0)
+ (id)packetWithData:(NSData *)data
{
int totalSize = [data length];
int packetNumber = [data pm_int32AtOffset:4];
return [[[self alloc] initWithAudioBufferData:data totalSize:totalSize packetNumber:packetNumber] autorelease];
}
答案 1 :(得分:0)
听起来你没有使用ARC,所以从一个非新的方法返回alloc
的结果,分配,保留或复制而不自动释放它是不正确的。添加自动释放到packetWithData:
的返回语句。
另外,我建议您阅读内存管理指南。时间不长,这个东西真的很基础。
答案 2 :(得分:0)
您不需要实现的类方法。尝试简化如下:
- (id)initWithAudioBufferData:(NSData *)data
{
if ((self = [super initWithType:PacketTypeAudioBuffer]))
{
// this is where audioBufferData is set.
self.audioBufferData = data;
self.totalSize = [data length];
self.packetNumber = [data pm_int32AtOffset:4];
}
return self;
}
并称之为:
PacketAudioBuffer *packet = [[PacketAudioBuffer alloc] initWithAudioBufferData:data];
希望这有帮助。