Disposing SPSite and SPWeb Objects

Nov 2, 2008 at 6:39 AM
When first writing some tests I used a using statement to try and grab the SPWeb object from SPContext. When I use using, I receive an asp.net error message that says I'm attempting to access the object after it's disposed. I'm just curious if behind the scenes your code safely dispositions any object that requires such or if it's just the way it's being compiled. Am I safe using SPSite and SPWeb as used in the starting example? I feel like the answer's yes but I want to make sure.
Coordinator
Nov 29, 2008 at 8:34 AM
Hello Boyles,

"I used a using statement to try and grab the SPWeb object from SPContext" : that's something very bad :)
With or without CodeTesterWebPart, SPContext objects should never be disposed from your code, it's done by WSS itself at the end of each request. 
You're safe to use those objetcts in the webpart as long as they are not disposed. The webpart is simply compiling and running the TestMethod, there is no hidden mechanism there :)