troubleshooting Question

Massive allocations with with CFString while writing to NSOutputStream in iOS

Avatar of SynthSubs
SynthSubs asked on
iOS
1 Comment1 Solution173 ViewsLast Modified:
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

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

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

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
pgnatyuk

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros