Wednesday, May 23, 2012
Where is my Object ?
New blog, first post. Starting with an error message. kind of a bad signal!!
Do you have such kind of beliefs? I think I don’t (ummm… I’m not sure but… sure I don’t)
Did you see above error message any time? If you are like me, almost daily you will be getting this. But why I’m showing this here? I hope you will understand “why” by end of this post.
I thought of telling about basics of classes/objects in this post but I hate blue print, instance definitions of class and object. May be those are apt to describe them but I got bored with them. Also examples on objects like car, account and employee are like pseudo philosophy. Looks logical on the paper but not useful in the end.
Shall I ask you one question?
Are you comfortable with the below stuff?
Now can you tell me what happens to the object “yourBlog”? If your answer is, “It is also “nothing” then please read on otherwise you can freely skip this post.
I feel that we can better understand classes and objects when we see them rather than think about them. What I mean by seeing an object?
Can you visualize what happens inside your computer when the below line of code executes? (In this blog, I will use the term computer rather than framework/program for my own happiness)
I know you are trying to say “creates myBlog in the memory.”
Of course it does but can you see how it looks like?
It is like this.
Our computer, to make its own life simple, named two places in the RAM called stack and heap. It holds variables in the stack and real objects in the heap. Our variable in the stack just stores address of the real object but not the object itself. (Note : In .net, heap may be called as "managed heap" but my goal is to present the idea. If it meets that requirement I'm fine. Later, you can refer your "Tech Guru's" books/articles.)
Is it applicable to all the variable types?
“No”.
Dim count as integer =9
Here “Heap” will not come into picture. Address of the count and value “9” both will be stored in the stack. I will describe this in detail in later posts but for now we will just concentrate on object type variables.
Next comes this line.
Can you see how it looks like in your computer?
It will be like this.
Can you see the difference between previous and this image? Only change is “New” keyword. Right? Your computer creates real object in the heap only when it encounters “New”. Till that time your variable in stack holds null (it is obvious correct? if there is no object what can it hold?)
Let us come to the below line.
Let me show you the picture again.
We know our object variables in the stack holds address of the objects. This line yourBlog=myBlog, copies address stored in the myBlog variable to yourBlog variable. Now both are pointing to the same object in the heap.
Let us come to the climax now. What happens after this line?
It is like this. You told your computer to clear address of the myBlog variable. It is cleared. This line has nothing to do with yourBlog variable. It still points to the Blog object.
Though myBlog address copied to yourBlog, state of myBlog has no impact on the yourBlog. Is it making sense to you?
Finally one finishing touch. What happens after execution of the complete method below?
It will be like this
myBlog, yourBlog variables are gone but your object will be in the heap still. It is duty of your garbage collector to clean it from the heap.
Now, shall we go to our first error message?
Can you visualize in which kind of scenario this error may occur?
Any guesses?
Ideally internal meaning of that message is
“Object variable (which is created in the stack) not set to an instance of an object (which supposed to be in the heap but not there)”
To show it pictorially
To show this in code
Dim yourBlog As Blog (New key word is not there. No object created in the heap) yourBlog.Publish() (Object itself is not there but you are calling a method from it)
Clear?
Shall I sign off?
Please post your comments if you have any questions or if it is not clear.
Subscribe to:
Post Comments (Atom)







waiting for the next post.......
ReplyDeleteThank you and sorry for the delay.Bit lazy you know...
ReplyDeleteI learnt something from this.Thank u Ajay.
ReplyDelete-Srikanth.