Re: NSData dataWithBytes:length:
Re: NSData dataWithBytes:length:
- Subject: Re: NSData dataWithBytes:length:
- From: Drew McCormack <email@hidden>
- Date: Thu, 3 Jul 2003 21:11:47 +0200
On Thursday, July 3, 2003, at 08:03 PM, Brent Gulanowski wrote:
I need some help with memory management. I am led to understand that
factory methods provide autoreleased objects.
However, according to MallocDebug and top, memory used up by NSData
objects created using +dataWithBytes:length: seems never to be
released. I wrote a test program:
#import <Foundation/Foundation.h>
#include <unistd.h>
int main (int argc, const char * argv[]) {
int i;
size_t length;
char *bytes;
NSData *data;
for(i = 0; i< 1000; i++ ) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
length = sizeof(char)*1000;
bytes = (char *)malloc(length);
data = [NSData dataWithBytes:bytes length:length];
This copies the bytes into the NSData object. That is, it deep copies
the bytes. So after this method, you have the bytes in the NSData
object, and those that you malloc-ed. You need a free() for your malloc
to avoid a memory leak.
Drew
----------------------------------
Dr. Drew McCormack
Trade Strategist (www.trade-strategist.com)
Stock Market strategy design platform for Mac OS X.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.