discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Best way to have a byte array + NSZone questions


From: Richard Frith-Macdonald
Subject: Re: Best way to have a byte array + NSZone questions
Date: Thu, 6 Sep 2001 12:15:15 +0000

On Thursday, September 6, 2001, at 10:13 AM, Aurelien wrote:

I have to access a raw byte array from within an object (it's a @protected instance variable). What's the best way to do that ? With NSData ?

Not sure what you mean ... do you mean that you want to store it in the object? If so, you could use NSData, or (if a fixed size array) just have a C array instance variable, or have your object allocate and deallocate the memory and have a pointer to it as your instance
variable.

If it is to be passed to other objects and other parts of your program, use NSData/NSMutableData.
If it is private and fixed size, use an array.
Otherwise, use a pointer if performance is a major issue, NSData for convenience otherwise.

When examining how to do it, I wondered what was the use of the NSXallocWithZone* methods, and what an NSZone object was. It's not documented in Apple's Foundation doc. Also when you NSXAlloc, you have to specify an NSZone pointer, but how do you get one ?

It's documented in the sections on types structures and functions ... NSZone not a class/object.
See the various zone manipulation functions.

Also, generally speaking, I'd like to know if I'm not totally mistaken saying that simply, you have to send a retain message to an object when you don't want that object to be released when the object that created the given object is itself released. It means that the + <objectName> methods (those that statically return instances of a given class), return objects that could potentially be instantly released. Do you have to send a retain message to those objects right after getting them then ? What is the object that actually "owns" such statically created objects ?

While it's technically true that an object returned from something like [NSArray + array] could be instantly released according to Apples guidelines, in practice that would be useless since it could be released before
you have a chance to retain it!

In reality, such objects are autoreleased before they are returned to you. This means that they are 'owned' by the current autorelease pool, and you have until the pool is deallocated in which to safely retain them - however it's good practice to retain them immediately
for code clrity.  If there is no autorelease pool in existence when the
objects are autoreleased, they are memory leaks (and GNUstep logs a warning).




reply via email to

[Prev in Thread] Current Thread [Next in Thread]