【问题标题】:Massive allocations with with CFString while writing to NSOutputStream in iOS在 iOS 中写入 NSOutputStream 时使用 CFString 进行大量分配
【发布时间】:2013-03-23 13:44:48
【问题描述】:

我在写入 NSOutputStream 时遇到大量分配问题。我一起使用了几个类,这些类应该将数据从 AudioUnit 流式传输到远程服务器。代码如下:

AudioProcessor.m

Data *data = [Data sharedData]; //it's shared data I use to store information for the final socket

intFromBuffer = audioBufferList->mBuffers[0].mData;
NSUInteger length = audioBufferList->mBuffers[0].mDataByteSize;

NSMutableData *dataOut = [[NSMutableData alloc] initWithCapacity:0];
[dataOut appendBytes:(const void *)intFromBuffer length:length * sizeof(SInt16)];

[data setOutput:dataOut]; //it writes data to the shared data

数据.m

@implementation Data
NSMutableData *output;
NSMutableData *input;

@synthesize output;
@synthesize input;

+(id)sharedData {
static Data *sharedData = nil;

@synchronized(self) {
    if (sharedData == nil) {
        sharedData = [[self alloc] init];
    }
    return sharedData;
  }
}

-(void) setOutput:(NSMutableData*)outputt{
    output = outputt;
}

-(void) setInput:(NSMutableData*)inputt{
    input = inputt;
}

@end

NetworkCommunication.m

- (void) observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context {
//NSLog(@"changed!!!!");
if ([keyPath isEqualToString:@"output"]) {
    Data *data = [Data sharedData];
    [self writeOut:data.output];

    }
}

- (void)writeOut:(NSString *)s {
    NSString *dataTo = [NSString stringWithFormat:@"%@\n", s];
    NSData *data = [[NSData alloc] initWithData:[dataTo dataUsingEncoding:NSASCIIStringEncoding]];
    [outputStream write:[data bytes] maxLength:[data length]];
}

它通常会创建大量数据,在 2.5 分钟内,虚拟内存中大约有 200 MB。我试图简单地将stringWithFormat: 直接传递给我的writeOut: 函数,但是它什么也没提供,不知道如何处理它。

如果有人问我为什么使用 stringWithFormat,仅仅是因为我需要输入\n 让服务器读取消息。

非常感谢任何帮助。

编辑 1:

我以不同的方式系它。在我的 AudioProcessor.m 中(因为这是创建的数据 - 在录音机回调中)我这样设置:

dataOut = [[NSMutableData alloc] initWithCapacity:0];
[dataOut appendBytes:(const void *)intFromBuffer length:length * sizeof(SInt16)];

net = [NetworkCommunication init];

const char *s = [dataOut bytes];

[[net outputStream] write:s maxLength:strlen(s)];

现在它使用 NSThread callStackSymbolNSThread callStackReturnAddresses 生成 Malloc 1,00KB 和 Malloc 2,00 KB 类型的疯狂分配。还有什么问题?

【问题讨论】:

    标签: ios sockets memory-management nsstring


    【解决方案1】:

    您是否尝试发送 C 字符串?

    const char *s = [dataTo UTF8String];
    [outputStream write:s maxLength:strlen(s)];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      相关资源
      最近更新 更多