Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Releasing (Xcode 1.5 & 10.3.9)?



On 7/29/05 7:03 AM, Norio Ota didst favor us with:

> I'm wondering why a released CFDataRef returns a valid value using
> CFDataGetBytePtr?
> Does CFRelease actually deallocate/free memory which is allocated and
> copied from source memory?

The contents of the memory are not overwritten just because it was freed
unless you're using some kind of special debug library that writes junk to
freed memory. It may be valid immediately after you release it, but you have
no way to know how long it will be before it's overwritten and becomes
invalid.

Larry

> If not, where and how do I free the memory that the dataRef has been
> holding?
> 
> int main ()
> {
> const char*  kMyName = "Norio Ota";
> size_t len = strlen (kMyName);
> char *  newString, *dStr;
> CFDataRef data;
> 
> newString = (char*) malloc (len + 1);
> memcpy (newString, kMyName, len);
> newString[len] = '\0';
> 
> data = CFDataCreate (kCFAllocatorDefault, newString, len +1);
> CFShow (data);
> fprintf (stderr, "newString is %s\n", newString);
> free (newString);
> 
> fprintf (stderr, "freed newString is %s\n", newString);
> 
> CFShow (data);
> CFRelease (data);
> 
> dStr = CFDataGetBytePtr (data);
> fprintf (stderr, "after releasing, stored newString is %s\n", dStr);
> 
> return 0;
> }
> 
> The log is something like below.
> 
> 
> <CFData 0x11011d0 [0xa01c00e0]>{length = 10, capacity = 10, bytes =
> 0x4e6f72696f204f746100}
> newString is Norio Ota
> freed newString is
> <CFData 0x11011d0 [0xa01c00e0]>{length = 10, capacity = 10, bytes =
> 0x4e6f72696f204f746100}
> after releasing, stored newString is Norio Ota
> 
> 
> As you can see, despite the data was released before being called
> CFDataGetBytePtr, it returns a value which seems to be valid.
> 
> Thanks,
> Norio 
> 
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Carbon-dev mailing list      (email@hidden)
> Help/Unsubscribe/Update your Subscription:
> http://lists.apple.com/mailman/options/carbon-dev/email@hidden
> 
> This email sent to email@hidden

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Carbon-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/carbon-dev/email@hidden

This email sent to email@hidden

References: 
 >Releasing (Xcode 1.5 & 10.3.9)? (From: Norio Ota <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.