Wrox Programmer Forums
|
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
 
Old June 11th, 2003, 02:08 PM
IM IM is offline
Registered User
 
Join Date: Jun 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
 
Old June 11th, 2003, 02:53 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
Code:
after
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
 
Old June 11th, 2003, 03:56 PM
IM IM is offline
Registered User
 
Join Date: Jun 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Code:
after
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
 
Old June 11th, 2003, 04:17 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
 
Old June 12th, 2003, 01:08 PM
IM IM is offline
Registered User
 
Join Date: Jun 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


 
Old June 12th, 2003, 01:35 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Error in Chapter 11 Sample Code josevi BOOK: Professional SharePoint 2007 Development ISBN: 978-0-470-11756-9 1 February 1st, 2008 01:43 PM
Chapter 11 Code Examples ablinco BOOK: Professional VB 2005 ISBN: 0-7645-7536-8 0 October 9th, 2006 03:56 AM
Where is source code for Chapter 11? benethridge BOOK: Professional Java Development with the Spring Framework 1 September 5th, 2006 08:58 PM
Chapter 11 Code Problems vbswshare BOOK: Professional VB 2005 ISBN: 0-7645-7536-8 3 April 9th, 2006 08:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.