 |
BOOK: Professional VB.NET, 2nd Edition or 2003  | This is the forum to discuss the Wrox book Professional VB.NET, 2nd Edition by Fred Barwell, Richard Case, Bill Forgey, Billy Hollis, Tim McCarthy, Jonathan Pinnock, Richard Blair, Jonathan Crossland, Whitney Hankison, Rockford Lhotka, Jan D. Narkiewicz, Rama Ramachandran, Matthew Reynolds, John Roth, Bill Sheldon, Bill Sempf; ISBN: 9780764544002 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Professional VB.NET, 2nd Edition or 2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 11th, 2003, 02:08 PM
|
|
Registered User
|
|
Join Date: Jun 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Chapter 11 Code
Hi,
In this chapter we build data access component.
Also we have a 'tester' application to test the component.
In the 'btnTest_Click' event of the 'tester' app we initialize the component, work with it, and dispose the component.
Code:
'Call dispose to free db connection (just in case)
'objSQL.Dispose()
Why the call to dispose() method has been commented?
Why I receive an error when I uncomment and make the call to dispose()?
Here is the text of the error:
"An unhandled exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll
Additional information: Cannot access a disposed object named 'ServicedComponent'"
I would appreciate your answer  ,
IM
|
|

June 11th, 2003, 02:53 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
I don't have the book, or the code, so I can't really tell, but isn't it possible that the objSQL object is still needed
the commented call to Dispose()?
Dispose effectively (explicitly) kills an object and removes it from memory. If it's needed later, an error will occur.
Imar
|
|

June 11th, 2003, 03:56 PM
|
|
Registered User
|
|
Join Date: Jun 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Imar,
Thank you for responding.
The code can be downloaded from the following FTP site:
ftp://ftp.wrox.com/download/code/Professional/7167.zip
I don't see that the objSQL object is still needed. There is something else (in the component code) there.
This Public Dispose() method was implemented specifically, so the client code could have a chance to close the db connection (opened by the component) and terminate the object.
If you get a chance could you look at the code and share your thoughts?
Thank you,
IM
Quote:
quote:Originally posted by Imar
Hi there,
I don't have the book, or the code, so I can't really tell, but isn't it possible that the objSQL object is still needed
the commented call to Dispose()?
Dispose effectively (explicitly) kills an object and removes it from memory. If it's needed later, an error will occur.
Imar
|
|
|

June 11th, 2003, 04:17 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hmmm weird. After a quick scan, it seems to me (without actually running the code) that this may be caused by the call to MyBase.Dispose() method in the Dispose() method of the SQL Server component. That's actually the only thing I can see related to ServicedComponent.
Does the error go away when you comment out that line? If so, there may be a reason why you can't / shouldn't call the Dispose() method on a ServicedComponent. Does this article help??
http://www.dotnet247.com/247reference/msgs/2/12230.aspx
Cheers,
Imar
|
|

June 12th, 2003, 01:08 PM
|
|
Registered User
|
|
Join Date: Jun 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Imar,
Could you explain why you think the call to MyBase.Dispose() method in the Dispose() method of the SQL Server component is extraneous, and why you see it "...related to ServicedComponent".
I also changed the sequence of execution.
I put the MyBase.Dispose() call after GC.SuppressFinalize(Me).
Now the error is gone.
Code:
Public Overloads Sub Dispose()
If m_bDisposed = False Then
Try
'Free up the database connection resource by
'calling its Dispose method
m_objConn.Dispose()
Finally
'Because this Dispose method has done the
'necessary cleanup,
'prevent the Finalize method from being called.
GC.SuppressFinalize(Me)
'Let our class know that Dispose() has
'been called
m_bDisposed = True
'Call the base type's Dispose() method.
MyBase.Dispose()
End Try
End If
End Sub
Could you comment on this change? Do you see any potential flaws associated with the change?
Thanks,
IM
Quote:
quote:Originally posted by Imar
Hmmm weird. After a quick scan, it seems to me (without actually running the code) that this may be caused by the call to MyBase.Dispose() method in the Dispose() method of the SQL Server component. That's actually the only thing I can see related to ServicedComponent.
Does the error go away when you comment out that line? If so, there may be a reason why you can't / shouldn't call the Dispose() method on a ServicedComponent. Does this article help??
http://www.dotnet247.com/247reference/msgs/2/12230.aspx
Cheers,
Imar
|
|
|

June 12th, 2003, 01:35 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
To be honest, I don't know. It was just a wild guess ;)
I looked at the Dispose method, and saw that the object it was in inherits from ServicedComponent. No other code in the Dispose method seemed to be related to the ServicedComponent object, so I assumed it had to be the MyBase.Dispose() call causing the error. The MyBase refers to the parent of the object, the ServicedComponent.
I never said it was extraneous, BTW. I just tried to find out where the error occurred. AFAIK, you should be able to call the Dispose() method on the Base class without getting an error.
I could be wrong, but I don't think there is a problem with calling the MyBase.Dispose() method last, although it may interfere with GC.SuppressFinalize. Check out the MSDN docs about SuppressFinalize for more info (if there is any).
Cheers,
Imar
|
|
 |