Re: returning NSRectArray
Re: returning NSRectArray
- Subject: Re: returning NSRectArray
- From: Shawn Erickson <email@hidden>
- Date: Tue, 23 Nov 2004 15:10:15 -0800
On Nov 23, 2004, at 2:36 PM, Björn Carlström wrote:
If i want to create a method that returns an NSRectArray, in the same
fashion as NSLayoutManager. How do I create this variable and how is
the memory it occupies freed up again?
NSRect is just a standard C structure it isn't a Cocoa/Objective-C
object. So just use malloc/free to manage its memory or use instance /
static variables to define the array. If you lookup the definition of
NSRectArray you can see it is just a standard C typedef that maps
NSRectArray to mean NSRect* (a pointer to an NSRect).
Also note that NSLayoutManager states the following which implies that
it manages the memory for the array itself and not the caller.
"The array of rectangles returned is owned by the receiver, and is
overwritten by various NSLayoutManager methods. You should never free
it and should copy it if you need to keep the values or use them after
sending other messages to the layout manager."
If I just declare a variable in my method and return it, I get a
warning that I'm returning a local variable.
Well the problem with this is that you are defining the variable in the
method (assuming you aren't tagging it as static). This variable and
any memory related to it is only valid for the life of method (or
function) it is defined in. Basically it only exists on the stack as is
valid only for the life of the that stack frame.
If you used a static variable or an instance variable to reference or
explicitly define the needed array you could return it to a caller
(assuming they don't attempt to release it on you, at least unless
expected that they do so).
One example (written in Mail.app)...
- NSRectArray someMethod:(int*)inRectCount
{
static NSRectArray rects;
...free or realloc existing rects array as needed or malloc/calloc it
if new, for example...
rects = (NSRectArray) calloc(rectCount, sizeof(NSRect));
...fill in rects as needed...
rects[0] = ...blah...
...
*inRectCount = rectCount;
return rects;
}
-Shawn
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden