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).
Please "Mark As Answer" if the post helped you.
mitja.gti | www.mitjagti.com
Marked as answer by Allen Li - MSFT on Aug 02, 2012 02:14 AM
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.
Please "Mark As Answer" if the post helped you.
mitja.gti | www.mitjagti.com
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
winner616
Member
25 Points
8 Posts
return inside of using statement, will dispose be called.
Jul 27, 2012 06:08 PM|LINK
I know that if i have a using block like
using myresource
return 0
end using
myresource will be disposed .
But if my using block is
using myresource
return myresource
end using
Will myresource be disposed?
I am wondering if a similar using block is causing memory lead in the app.
thanks
mitja.GTI
Star
11157 Points
2094 Posts
Re: return inside of using statement, will dispose be called.
Jul 27, 2012 06:21 PM|LINK
Hi winner616
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).
mitja.gti | www.mitjagti.com
alvingeorge
Participant
925 Points
203 Posts
Re: return inside of using statement, will dispose be called.
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
winner616
Member
25 Points
8 Posts
Re: return inside of using statement, will dispose be called.
Jul 27, 2012 06:33 PM|LINK
thanks mitja.GTI]
I read the the info in your link.
The full code looks like
function GetResource() As SomeResource
using myresource
return myresource
end using
end function
Sub SomeMethod()
Dim myvar As int = GetResource() .Execute()
End Sub
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.
winner616
Member
25 Points
8 Posts
Re: return inside of using statement, will dispose be called.
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?
mitja.GTI
Star
11157 Points
2094 Posts
Re: return inside of using statement, will dispose be called.
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.
mitja.gti | www.mitjagti.com
alvingeorge
Participant
925 Points
203 Posts
Re: return inside of using statement, will dispose be called.
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