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.
iOS

Avatar of undefined
Last Comment
pgnatyuk

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
pgnatyuk

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Your help has saved me hundreds of hours of internet surfing.
fblack61