From e766b69a221ea3a7c5589eb7323bfa58ca48c6b5 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Fri, 28 Oct 2022 00:14:07 -0700 Subject: [PATCH] Fix memory leaks We need autoreleasepools around the Objective C allocations. Signed-off-by: Christopher Snowhill --- scpipe.mm | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/scpipe.mm b/scpipe.mm index 591db44..97d404f 100644 --- a/scpipe.mm +++ b/scpipe.mm @@ -23,9 +23,11 @@ static NSFileHandle *pipe_out = nil; void put_bytes( const void * out, uint32_t size ) { - NSError *error; - NSData *data = [NSData dataWithBytes:out length:size]; - [pipe_out writeData:data error:&error]; + @autoreleasepool { + NSError *error; + NSData *data = [NSData dataWithBytes:out length:size]; + [pipe_out writeData:data error:&error]; + } } void put_code( uint32_t code ) @@ -35,13 +37,17 @@ void put_code( uint32_t code ) void get_bytes( void * in, uint32_t size ) { - NSError *error = nil; - NSData *data = [pipe_in readDataUpToLength:size error:&error]; - if(!data || error) { - memset( in, 0, size ); + NSUInteger bytesDone; + @autoreleasepool { + NSError *error = nil; + NSData *data = [pipe_in readDataUpToLength:size error:&error]; + if(!data || error) { + memset( in, 0, size ); + return; + } + bytesDone = [data length]; + memcpy(in, [data bytes], bytesDone); } - NSUInteger bytesDone = [data length]; - memcpy(in, [data bytes], bytesDone); if(bytesDone < size) { memset(((uint8_t *)in) + bytesDone, 0, size - bytesDone); }