写入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
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)];
现在它会生成类型为Malloc 1,00KB的疯狂分配,以及带有NSThread callStackSymbol
和NSThread callStackReturnAddresses
的Malloc 2,00 KB。还有什么问题?
答案 0 :(得分:0)
您是否尝试过发送C字符串?
const char *s = [dataTo UTF8String];
[outputStream write:s maxLength:strlen(s)];