Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jul 28, 2012 12:37 AM by alvingeorge
Jul 27, 2012 06:08 PM|LINK
I know that if i have a using block like
myresource will be disposed .
But if my using block is
Will myresource be disposed?
I am wondering if a similar using block is causing memory lead in the app.
Jul 27, 2012 06:21 PM|LINK
your second example is a bad practise and should be avoided. It can cause all sorts of problems. You can find aditional information in MSDN: using Statement (C# Reference).
Jul 27, 2012 06:26 PM|LINK
dispose will be called. The use statement is internally using try/catch/finally so finaly will be executed in all the cases
Jul 27, 2012 06:33 PM|LINK
I read the the info in your link.
The full code looks like
function GetResource() As SomeResource
Dim myvar As int = GetResource() .Execute()
Because I didn't write the code, the first time I saw the code, I thought it would an exception would be thrown when
Dim myvar As int = GetResource() .Execute() is called
because myresource should be already disposed.
We didn't find exception. Instead, we got a huge memory leak problem, which i am not sure if the above code contributes.
Jul 27, 2012 06:36 PM|LINK
thanks alvingeorge for your answer.
If dispose will be called, does it mean I cannot use myresource because it is already disposed?
or i still can use myresource, and it will be disposed until no obj still hold a reference to it?
Jul 27, 2012 07:02 PM|LINK
As I've said it can be responsible for all sort of things. What you are doing is bad practice and should be avoided by all cost. It's also hard to say what exactly is happening because garbage collector is responsible for disposing the memory. It can cause
unexpected behaviour. What that means is that in some cases your code could work in some it won't. Impossible to say. Bottom line is that you must change that code. Only then you will be able to proceed with your debugging about memory leak. One can not say
for sure that (only) that might be responsible for problems with memory in your application.
Since you are programming in Visual Basic, it's childs play, one does not have to worry about memory leaks as long as it is coding inside standards. Memory allocation, relocation, disposal is taken care for you. It's not like you are coding in C/C++. I'm
guessing that line of code isn't the only problem in your application.
Jul 28, 2012 12:37 AM|LINK
yes it is already disposed means marked for Garbage collector and all other steps in your dispose method of that object will be executed , it may release a connection to database or making some other properties null. so it may create null reference error
or some other unknow errors