Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MOre QUestions 2

Status
Not open for further replies.

Zarcom

Programmer
May 7, 2002
1,275
0
0
CA
This is a effiency type question.

In a few of my web services I am coding this

dim stuff as new dsmydataset
'code to fill it here
return stuff


My question is that it is usually a good idea to deallocate the new
stuff = nothing
However you cannot do this in the above case. should I just ignore this or is there a way to destroy the variable after the fact.

Q2
The same type of situation as above, however instead of returning the dataset a boolean true is returned if the dataset contains anything. Presently for the check I just use
if stuff.tablename.count > 0
return true

Once again the problem of deallocating arises. Is it better to assign the .count to an int and the deallocate the object using the int in the if statment or to keep it as is.
That'l do donkey, that'l do
[bravo]
 
As for part two, it's "good programming practice" not to have multiple return statements. ie this:
Code:
if stuff.tablename.count > 0
     return true
else
     return false
end if
which leaves you with no way to clean up is not as good as this:
Code:
dim output as boolean
if stuff.tablename.count > 0
     output = true
else
     output = false
end if
return output
and the previous statements could easily be modified to be:
Code:
dim output as boolean
if stuff.tablename.count > 0
     output = true
else
     output = false
end if
stuff = nothing
return output
One way in... one way out... nice and clean.

As for your first question, all I can offer is conjecture, and how I would think it would have been designed. It would stand to reason that it would have been designed to pass the return value back by reference. If that is, in fact, the case, then one would only need to worry about cleaning up the returned variable/object, and that would be that.

However, if it's passed back by value, then yes, you've left a copy in memory. That being said, then to handle this situation, you might want to define a page_unLoad procedure that would call:
Code:
GC.Collect
which forces the garbage collector to go out, find all objects with no live handles, and destroy them.

After some quick searches, I'm unable to find which is actually the case, but it would also stand to reason that defining the aforementioned sub to call the collector really couldn't hurt to do in any case -- just in case.

ex)
Code:
private sub page_unLoad(o as object, e as eventargs) handles mybase.unload
  gc.collect()
end sub

**puts into bag o' tricks**

:)
paul
penny1.gif
penny1.gif
 
Well said Paul.

A quick question. I am using web services for this. Do you know if the dispose method would be the equivalent of a web forms "page_unload"

Thanks again for the info. That'l do donkey, that'l do
[bravo]
 
K, I probably am missing something here, but here's my take on it. Let me know if there's some aspect of garbage collection I may be missing.

Zar: I think Paul's right with his thoughtst that anything passed back is by value and not reference, meaning that a dataset is passed but the original still lingers. But I'm a tad confused as to what you're trying to do. You could just set the object to = nothing, and wait for the garbage collection to get it, right? Unless you want it gone right that second, in which case I just mis-read your question.

Also, if you want to make sure your objects are always destroyed no matter what, you may want to consider useing try/catch blocks with a Finally clause at the end. Thats how we manage our objects, so that if an error occurs, the objects are always destroyed no matter what.

Just thoughts, opinions, etc.
:)

Jack
 
Maybe I am a bit off here but, this is my take on it.

You have a web service that queries the database based on some given criteria.
That web service then returns a dataset using the return command.
At this point the code returns to the procedure that called the webservice. I do agree that the dataset is passed by value (necessary by the design of a webservice). Now the problem - well not a problem but I was curious about tweaking - is that the original as Jack said lingers.
I would like to find a way to get rid of that lingering dataset right away.
Or is it really even necessary to do so?
You can't set it to nothing before the return statment or nothing is returned :-D You can't set it afterwards because that code is not run.
Something to ponder on. I am always looking to have the best code ever written and hate to settle for less That'l do donkey, that'l do
[bravo]
 
Ahhh, see thats what I'm confused about:
You can't set it afterwards because that code will not run

it WILL run! I just tested it:

Private Function ReturnSomething()
Dim oDS as new dataset
ReturnSomething = oDS
oDS = Nothing
End Function

When my stop hits oDS = Nothing, I can still see all the properties in my output window. When my stop hits End FUnction though, that happens AFTER oDS is set to nothing, and my output window simply says it equals Nothing.

So yes: code you write AFTER returning the value WILl destroy the lingering object and will run.

jack
 
O.....
I see.....
I guess once again I assumed something without actually checking it out. Does it still give the correct dataset back? Ack never mind I can check that myself.

MOst hUmblE ApOlOgiEs That'l do donkey, that'l do
[bravo]
 
An answer to your question, yes... if a webservice has a dispose method, then that's the one you would use to clean up after yourself. As I haven't written a webservice yet, I'm not familiar with all the events, but it's unload for pages.

And I'd call it desireable, rather than necessary, to clean up those objects. The GC is going to come through every so often and clean up after you. That might be every 5 seconds, or it might be every 15, or whatever... possibly longer. There is no guarantee as to when it's going to fire. So until it does, that object is hanging around on your memory chips taking up space. The cleaner you keep it on your own, the faster your apps are going to run.

You bring up a good point.
penny1.gif
penny1.gif
 
Good point Paul, I should have worded the last sentence on my last post differently:

It should have read:
So yes: code you write AFTER returning the value will run, but setting it to Nothing will only flag the objects for destruction.


The garbage collector still needs to come around to collect them, but like he paul said, who knows when that'll happen
:)

Jack
 
That I didn't know. Thanks Jack That'l do donkey, that'l do
[bravo] Mark
 
Got another 4 you guys. I have never been real clear on deallocation. What is the difference between
object = nothing
and the method
object.dispose

I am nearing the end of developement and I want to make sure that my app isn't going to be a big resource hog with memory leaks. Like I said before I am always looking to have the best code ever written and hate to settle for less. That'l do donkey, that'l do
[bravo] Mark
 
From what I understand, the Dispose sub is there to hold any code that you want to fire when you destroy an object.

In the example I read, they had this:

Private Sub Object.Dispose()
me = nothing
End Sub

Its kinda like Session_OnEnd, where you can put any clean up code you want to fire when the session dies.

Here's the article I was reading:

hth

Jack

p.s. thanks for the star :)
 
An object is never passed ByVal...only the referenece to the object is passed by value. Therefore the underlying object is changed by changes made through the object reference passed ByVal. The difference is that the reassignment of the Byval reference to another object does not change the reference in the calling procedure, whereas an object reference passed ByRef would be reassigned.

See ByVal under Function or Sub in the VB.Net reference

Therefore if you execute any method on an object passed as an argument, either ByVal or ByRef, or an object that is to be returned in a function, you will change that object.

Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
From my help file:
A value parameter is used when the value of an argument is passed into a method, and modifications of the parameter do not impact the original argument. A value parameter refers to its own variable, one that is distinct from the variable of the corresponding argument. This variable is initialized by copying the value of the corresponding argument.


You said:
An object is never passed ByVal...only the referenece to the object is passed by value. Therefore the underlying object is changed by changes made through the object reference passed ByVal


Passing the reference to the object is byRef. The underlying object is never changed when you pass byVal.
penny1.gif
penny1.gif
 
No. An object is a Reference Type. When you pass an object reference ByVal, you pass the reference to the object (pointer to object) ByVal. When you pass an object reference ByRef, you pass a pointer to the object reference, which itself is a pointer.
See what happens.
Class objInt
Public intW As Integer = 0
End Class

Dim objW As objInt

objW = New objInt()
Call ChangeItByVal (objW)
Msgbox Cstr(objW.IntP) ' Prints 2

objW = New objInt()
Call ChangeItByRef (objW)
Msgbox Cstr(objW.IntP) ' Prints 0

Private Sub ChangeByVal(ByVal objP as objInt)
objP.intW = 2 ' Changes object itself
objP = New(objInt) ' Changes object ref in Sub
End Sub

Private Sub ChangeByRef(ByRef objP as objInt)
objP.intW = 2 ' Changes object itself
objP = New objInt() ' changes object ref in Caller
End Sub


ByVal and ByRef refer to how an argument is passed to a procedure. An Object is never an argument but an Object Reference (pointer to object) is.

Knowing the difference bewteen Reference Types and Value Types is crucial.



Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
A better explanation. When the help file talks about the "value" of a variable being passed ByVal, they do not mention that the "value" of an object variable is a "pointer" to the object.


Class objInt
Public intW As Integer = 0
End Class


Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim objW As New objInt()
objW = New objInt()
Call ChangeByVal(objW)
lblMsg.Text = lblMsg.Text & " " & CStr(objW.intW) ' Prints 2

objW = New objInt()
Call ChangeByRef(objW)
lblMsg.Text = lblMsg.Text & " " & CStr(objW.intW) ' Prints 0

End Sub


Private Sub ChangeByVal(ByVal objP As objInt)
objP.intW = 2 ' Changes object itself
objP = New objInt() ' Changes object ref in Sub
End Sub

Private Sub ChangeByRef(ByRef objP As objInt)
objP.intW = 2 ' Changes object itself
objP = New objInt() ' changes object ref in Caller
End Sub Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Hey Zar,

I just discovered something that had to do with the first question you had about code not running after the Return statement. It turns out that you might be right actually, although I think this is really ghey how it works:

Consider this function:

Private Function ReturnSomething() as integer

Option1
Return 1

Option2
ReturnSomething = 1

Dim i as integer
i = 0

End Function

Now, if you did Option1, you're right: the code WILL stop at taht point and not execute the code below it. However, if you do Option2 (instead of Return, using the FunctionName) then it will.

Sorry about the confusion.
:)

Jack
 
Give me back that star you. Haha just kidding. Thanks for clearing that up Jack. I hadn't actually tested what you had mentioned yet so I am still in the clear. That'l do donkey, that'l do
[bravo] Mark
 
Hey Zar,

I think I finally found an article that explains what the .Dispose method is really there for.

If you have an object, and lets say that it somehow uses the Clipboard and opens it. When you set it to = Nothing, it does nothing with any un-managed resources the object may have been using. So for our object, we would need to write a dispose method that would close the clipboard and release those resources to the system. THEN set object = Nothing.

If you get a chance, check out the August edition of Visual Studio magazine, they have an article on 8 performance tuning tips, which is where I got this info from.

Jack
 
Thanks Jack That'l do donkey, that'l do
[bravo] Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top