Link to home
Start Free TrialLog in
Avatar of SynthSubs
SynthSubs

asked on

Massive allocations with with CFString while writing to NSOutputStream in iOS

I have problem with large number of allocations while writing to NSOutputStream. I am using few classes together that are supposed to stream data from AudioUnit to remote server. Code below:

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

Open in new window


Data.m (for singleton)

@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

Open in new window


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]];
}

Open in new window


Also NetworkCommunication is running in Network Thread in my app.

It generally creates great amount of data, which in 2,5 min was about 200 MB in virtual memory. I tried to simply pass stringWithFormat: directly to my writeOut: function however it gives nothing, not idea how to deal with it.

If someone asks why I use stringWithFormat, simply because I need to put \n for server to read the message.

Any help much appreciated.
ASKER CERTIFIED SOLUTION
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial