[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Life of instance variables?
From: |
Albert Chun-Chieh Huang |
Subject: |
Re: Life of instance variables? |
Date: |
25 May 2003 16:21:46 +0800 |
User-agent: |
Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 |
Kim Shrier <kim@tinker.com> writes:
> It would be nice ro see the source of the init method for Node and MyNode.
> I am going to assume your code is something like this:
>
[stuff deleted]
>
> If you have done that, your instance variable will be properly initialized
> and its value should be valid as long as the instance is not freed. Any
> value you set with setNodeState should stay around until you change it with
> setNodeState.
>
> If you are seeing a value of 14 for state and you are sure that you are not
> setting it to 14 after you have set it to 1, then I would make sure that you
> are passing the instance of MyNode whose state was set to 1 into your
> Channel
> instance.
>
> Kim
Dear Kim,
Thanks for your reply. My -init is like this:
MyNode:
- initNodeWithAddress: (unsigned int)theAddress
{
self = [super initNodeWithAddress: theAddress];
// Other portion of initialization
return self;
}
Node:
- initNodeWithAddress: (unsigned int)theAddress
{
self = [super init];
state = 14;
// other portion of initialization
return self;
}
And one thing should be noted here, but I don't know if it's
important. In Channel, the instance of MyNode is from an NSArray,
which is an id array of instances of MyNode, and it is derived from:
// In Node
[theChannel registerAsReceiver: self
atFrequency: theFreq
fromFrequency: registeredFreq];
NSLog(@"Node %x state = %d", theBD_ADDR, state);
theChannel is an instance of Channel.
The situation I mentioned only occurs at -receivePacket: method. The
NSLog line works well, I mean, state is 1. But later when theChannel
issue the statement [aNode receivePacket: aPacket], the value of state
is not what I expect. Is it possible that I misuse the variable
"self"?
--
Chun-Chieh Huang, aka Albert | E-mail: jjhuang AT cm.nctu.edu.tw
黃俊傑 |
Department of Computer Science |
National Tsing Hua University | MIME/ASCII/PDF/PostScript are welcome!
HsinChu, Taiwan | NO MS WORD DOC FILE, PLEASE!