0

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

音频处理器.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

网络通讯.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)];

现在它会生成类型为 Malloc 1,00KB 和 Malloc 2,00 KB 的疯狂分配,其中NSThread callStackSymbolNSThread callStackReturnAddresses. 还有什么问题?

4

1 回答 1

0

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

const char *s = [dataTo UTF8String];
[outputStream write:s maxLength:strlen(s)];
于 2013-04-02T12:34:13.413 回答