Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory spike problem and the fix #32

Open
Samx2015 opened this issue Jan 6, 2019 · 0 comments
Open

Memory spike problem and the fix #32

Samx2015 opened this issue Jan 6, 2019 · 0 comments
Assignees

Comments

@Samx2015
Copy link

Samx2015 commented Jan 6, 2019

When encrypting/decrypting large files, in our case, the memory usage spikes to 1.5G and app quickly crashes. We found the problem to be caused by RNCryptorEngine:addData:error creating 48K NSData objects that are not released fast enough. The fix is pretty simple: to add @autoreleaseblock to the two functions that use RNCryptorEngine:addData:error:

RNEncryptor:

- (void)addData:(NSData *)data
{
    if (self.isFinished) {
        return;
    }
    
    dispatch_async(self.queue, ^{
        @autoreleasepool { // this fixes the memory issue
            if (!self.haveWrittenHeader) {
                NSData *header = [self header];
                [self.outData setData:header];
                if (self.hasHMAC) {
                    CCHmacUpdate(&self->_HMACContext, [header bytes], [header length]);
                }
                self.haveWrittenHeader = YES;
            }
            
            NSError *error = nil;
            NSData *encryptedData = [self.engine addData:data error:&error];
            if (!encryptedData) {
                [self cleanupAndNotifyWithError:error];
            }
            if (self.hasHMAC) {
                CCHmacUpdate(&self->_HMACContext, encryptedData.bytes, encryptedData.length);
            }
            
            [self.outData appendData:encryptedData];
            
            dispatch_sync(self.responseQueue, ^{
                self.handler(self, self.outData);
            });
            [self.outData setLength:0];
        }
    });
}

RNDecryptor:

- (void)decryptData:(NSData *)data
{
    dispatch_async(self.queue, ^{
        @autoreleasepool { // this fixes the memory issue
            if (self.hasHMAC) {
                CCHmacUpdate(&self->_HMACContext, data.bytes, data.length);
            }
            
            NSError *error = nil;
            NSData *decryptedData = [self.engine addData:data error:&error];
            
            if (!decryptedData) {
                [self cleanupAndNotifyWithError:error];
                return;
            }
            
            [self.outData appendData:decryptedData];
            
            dispatch_sync(self.responseQueue, ^{
                self.handler(self, self.outData);
            });
            [self.outData setLength:0];
        }
    });
}
@rnapier rnapier self-assigned this Apr 20, 2019
@rnapier rnapier transferred this issue from RNCryptor/RNCryptor Apr 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants