5

Is it possible to manually clear out the contents of an object from memory?

In particular, I'm dealing with NSData. I've tried using data.length = 0 and data.setData(NSData).

I know ARC will come in and clean up after it is out of scope to whom it belongs, but is it possible to manually force this process when I want?

Alexander
  • 59,041
  • 12
  • 98
  • 151
David Biga
  • 2,763
  • 8
  • 38
  • 61

1 Answers1

4

I think you have some misconceptions about ARC I'd like to clear up. The goal of ARC is is to ensure memory leaks don't occur. It's responsible for tracking the object over its life cycle, and ensuring it's "freed" when no references remain to it.

It's important to note that the memory being "freed" does not imply "writing over it all with 0s".

It simply means that memory will be designated as unused. The freed memory becomes a candidate for allocation when the system needs to allocate memory to new objects.

There's no guarentee, however, that this reallocation will happen, thus it's very possible for your freed memory to contain your original data, and never be overwritten.

Alexander
  • 59,041
  • 12
  • 98
  • 151
  • So even after taking an object and overwriting the contents of itself `.setdata(nsdata())` it does not guarantee that the actual memory will be overwritten with the new value? Or are you referring to when you let the object go entirely? – David Biga Jun 10 '16 at 17:23
  • I don't know which `setdata` method you're talking about, but most likely it updates an internal instance variable of the receiver that holds a reference to NSData. Setting a new value to it will make ARC free the old NSData object it used to reference (if nothing else is referencing that NSData object), but there's no guarantee it'll be zeroed out. – Alexander Jun 10 '16 at 17:31
  • The NSMutableData SetData() - https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableData_Class/#//apple_ref/occ/instm/NSMutableData/setData: – David Biga Jun 10 '16 at 17:32
  • This may be of interest http://stackoverflow.com/questions/27715985/secure-memory-for-swift-objects – Alexander Jun 10 '16 at 17:33
  • This would essentially do the same exact thing as `resetBytesInRange` right? The difference here is `setData` would clear our the values and reset it to a 0 length? – David Biga Jun 10 '16 at 17:34
  • I don't know about the underlying implementation, but unless the documentation makes any promises about it, I wouldn't trust it. It could for example be storing those bytes in an array on heap, and just changing the reference to point to a new array, leaving the old array freed but not cleared – Alexander Jun 10 '16 at 17:35
  • 1
    My prior comment was about `setData(_:)`. I believe `resetBytesInRange(_:)` will do what you want. It'll zero out the sensitive part of the object, ARC will later free it, and leave the non-sensitive parts untouched until the memory is reallocated. – Alexander Jun 10 '16 at 18:10