[2005] Marshal.ReleaseComObject
Hi all,
I have a COM component on my test server that my application uses to connect to a database.
I am trying to set up a single class to interact with the object. I am using the Marshal.ReleaseComObject method to "free up" the object once I'm done with it.
The problem is that this works the first time, but doesn't work on subsequent tries. Am I using the method improperly?
For example, in one of my classes, I send an SQL statement to the function that executes the queries, the query is executed, then I call the function to release the object. However, if I try to execute another query, I get nothing. Am I using this wrong?
I hovered over the top of the class where I instantiate the object, and I get a "Attempted to read or write protected memory...." error.
edit: I should note this is a web application, but I'm not sure if that matters. If so, mods feel free to move this to the appropriate section.
Re: [2005] Marshal.ReleaseComObject
I have never had much luck with Marshal.ReleaseComObject by itself. In VS 2003, it was recomended to use Marshal.ReleaseComObject in a loop until your reference count was zero.
Code:
While System.Runtime.InteropServices.Marshal.ReleaseComObject(o) > 0 End While
VS 2005 now has a method to do the looping for you.
Code:
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(o)
Try using this method for your release.