React-native-keychain: Memory leaks in iOS

Created on 10 Dec 2018  路  6Comments  路  Source: oblador/react-native-keychain

When I'm using below code in my app.

Keychain.getGenericPassword()
      .then(credentials => {
        if (credentials) {
          this.setState({
            password: credentials.password
          });
        } else {
          return Keychain.setGenericPassword(
            'test',
            'randomSecret'
          ).then(() => {
            this.setState({
              password: 'test'
            });
            Promise.resolve('randomSecret')
          });
        }
      })
      .catch(error => {
        this.setState({
          password: error.message
        });
        Promise.reject(error)
      });

I'm getting memory leaks. I'm using Xcode instrumentation to identify this leaks. See attached screenshots.
screen shot 2018-12-10 at 1 26 39 pm
screen shot 2018-12-10 at 1 26 50 pm
This leaks increases continuously until the app is an active.

Below is the app leak area.
OSStatus osStatus = SecItemCopyMatching((__bridge CFDictionaryRef) query, (CFTypeRef*)&foundTypeRef);

Let me know whether I'm doing anything wrong.

Most helpful comment

Update: The leak was actually coming from foundTypeRef, which is a Core Foundation object that we created but didn't release after we were done using it. I fixed it in #188

All 6 comments

I'm seeing this as well. I found an answer on StackOverflow that seems to explain the issue. Here's an excerpt:

You receive the keychain object from a CF-style function with Copy in its name. Therefore it has a +1 reference count, and you have the responsibility of explicitly releasing it when you're done using it. It is never released by your sample code, so it's leaking.

Screenshot of the leak from my app:
image

That could be the problem here, because Instruments is showing Retain & Release Count: +1. Once we're done with osStatus, maybe CFRelease(osStatus) will do the trick?

Is there a fix for this?

@Bautista-Baiocchi-lora when I have some time I'll try the CFRelease(osStatus) to see if it resolves the issue. I'm not very experienced with memory management though, so I'm not sure if this is the correct fix. @oblador do you know if this is the right approach?

Update: The leak was actually coming from foundTypeRef, which is a Core Foundation object that we created but didn't release after we were done using it. I fixed it in #188

@rajivshah3 Thanks for fixing the issue.

Update: The leak was actually coming from foundTypeRef, which is a Core Foundation object that we created but didn't release after we were done using it. I fixed it in #188

Great find! Thank you for the fix.

Was this page helpful?
0 / 5 - 0 ratings