Re: Static Variable Question...
Re: Static Variable Question...
- Subject: Re: Static Variable Question...
- From: "Dennis C. De Mars" <email@hidden>
- Date: Sat, 21 Jul 2001 15:35:37 -0700
on 7/21/01 10:52 AM, Joshua D. Orr at email@hidden wrote:
>
I am making a document based application, and I have a few static variables
>
for a class that are pointers to a few NSArray objects. When I run the
>
program and open a document, the program loads information from the file and
>
puts it in into NSArrays, then I have a class method that it calls and those
>
methods set the static variables to point to the NSArrays. This works just
>
fine, until I open another document. The new document works just fine, but
>
the old document does not work correctly. I think what's happening is when
>
the program loads the new document, it sets the static variables to point to
>
the new NSArrays, and both the old document and the new one point to the
>
same NSArrays. I separated the objects between documents by allocating
>
every object for that document in its zone (I use [self zone]).
>
>
I was hoping separating the objects into different zones would make the
>
class static variables separate also (one for each zone).
The "class static" variables are actually just plain old C static variables
and the storage just gets allocated once when the application starts. I'm
sure the zone stuff only applies to dynamically allocated memory.
>
>
Ether the NSDocumentController does not assign a new zone to every document
>
it makes (making everything in the same zone), or I need to somehow, when
>
calling the class methods, tell it which zone it should call the class
>
method in, or different zones will not work.
>
>
Does anyone have any ideas?
Nothing you do is going to cause more than one set of static variables for
your class to be allocated. It sounds like what you want is to have one set
of these variables per document. There are several ways to do this; the most
straightforward would be to simply subclass NSDocument and make these
variables instance variables of the subclass. The objects of the class that
uses these variables would then need a pointer to NSDocument to access these
variables...
- Dennis D.