3

I am trying to Run the code but its reporting the memory leaks when using static analyzer. on this line as Potential leak of an object stored into 'encodedData'

return encodedData;

Check image here

Konrad Lindenbach
  • 4,911
  • 1
  • 26
  • 28

3 Answers3

4

use __bridge_transfer

Using __bridge_transfer ensures that ARC will release the object for you. Without __bridge_transfer, you must release the returned object manually.

Muhammad Adnan
  • 2,668
  • 15
  • 27
1

__bridge,__bridge_transfer keywords are used to tell to ARC system how to handle your non-objective-c pointers. In essence, if you use __bridge, you are telling to ARC not to deal with the ownership of the converted pointer because you will free it from non-objective-c code, most likely with a free() or a CFRelease... type function. __bridge_transfer, on the other hand, transfers the ownership to ARC and ARC will free your objective-c (and thus also the original non-objective-c) object via the standard release mechanism when the references to that object hits zero.

Reference

Community
  • 1
  • 1
Chetan
  • 2,004
  • 1
  • 13
  • 21
0

The problem is that you create your string using CoreFoundation methods. And by default ARC doesn't know what to do with it. So, you're responsible for either manually managing the memory for the created object (using CFRelease for example), or handing it over to ARC.

The later is, I believe, the way to go in your case. You can do it, as others have already noted, using __bridge_transfer.

FreeNickname
  • 7,398
  • 2
  • 30
  • 60