Reputation: 1128
I have come across an interesting question.
I have this piece of code (don't ask why I need to do something like this):
CFDataRef data = ... get data from somewhere
SSLContextRef sslContext;
CFDataGetBytes(data, CFRangeMake(0, sizeof(SSLContextRef)), (UInt8 *)&sslContext);
Now I don't know what to do with sslContext
. As I understand I have made a byte copy of SSLContextRef and I need to free that memory after I used it.
So here comes a question: how can I properly free memory?
Again as I understand I can't do CFRelease because I didn't increment reference count when I got(copied) the object and if I simply try to do free(sslContext)
I get crash.
I would appreciate if someone could explain how it should work.
EDIT: Thanks to user gaige. He has pointed that in the question I have copied only reference to SSLContextRef. As I understand if I do:
UInt8 *buffer = malloc(sizeof(SSLContext));
CFDataGetBytes(data, CFRangeMake(0, sizeof(SSLContext)), buffer);
I then I can just do free(buffer);
without any problem (provided that I didn't do any CFRetain/CFRelease logic). Please correct me if I am wrong.
Upvotes: 0
Views: 588
Reputation: 17491
In this case, you copied sizeof(SSLContextRef)
bytes of data from the CFDataRef
pointed at by data
, you didn't increase any reference counts, nor did you copy any data other than the pointer to the SSLContext
structure. (SSLContextRef
is declared as a pointer to struct SSLContext
).
The data you copied ended up in sslContext
in your current stack frame, and thus doesn't need any special curation by you in order to make it go away.
In short, you don't need to do anything because no data was copied in the heap.
Upvotes: 1