Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 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 February 14th, 2006, 08:10 AM
Registered User
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Disposing / Releasing objects

Hello all,

Have a doubt with releasing objects once I am done with them.

Am developing a Windows Forms MDI database application (Oracle as the backend).

I create various DataReader, DataAdapter, DataSet objects in various child forms (where each child form is a "module" in the application). How and when do I release these objects. Will they be destroyed when the window is closed automatically or do I need to say object.dispose or set them = NULL ?

Example.
I have a "Search for Customers" module which the user invokes from a menu item. This window calls a database procedure which returns a resultset using a refcursor type. Then the search results datagrid is populated using the dataset.

Now the Command, DataSet, DataAdapter types etc I have defined to perform the above, are to be released at what point of time ? Do I need to code it myself or will they be released when the window is closed ?

Basically I wish to know what kind of these database related objects I can reuse in all modules and which objects I will need to create for each child window, so that my application memory requirement is optimum.

I understand this is a very basic query, but I am new to DotNet and my concepts are not quite clear. Will appreciate if you can help clear this query.

Thanks in advance,
yamyam
 
Old February 14th, 2006, 08:49 AM
Wrox Technical Editor
 
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

DataSet: you should clear the DataSet via the .Clear() method if the data is no longer needed.

DataAdapter: you should leave the DataAdapter for Garbage Collection to run its course.

DataReader: you should explicitly close the DataReader via the .Close() method.

.NET's Garbage Collection algorithm is optimized, calling the .Dispose() method on any object creates additional Garbage Collection overhead. Thus; calling .Dispose() should generally be avoided.


- A.Kahtava
 
Old February 14th, 2006, 11:01 AM
Registered User
 
Join Date: Feb 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So Do i call these methods i.e. DataSet.Clear(), DataReader.Close() etc in the close event of the window, or is there a more appropriate event where I can call them ?
 
Old February 14th, 2006, 02:01 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Close them when you are done with them. If they are available to the entire form, and you still need the resource, you could leave it open and close in the form_close event, sure.

Brian
 
Old February 17th, 2006, 03:25 AM
Authorized User
 
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
I think you can write Exception-Handling Code(try, catch, finally) in your code.
Also, you can clear the objects in finally block.
         Like :
                Dataset.Clear()
                obj=nothing (if any object is declared)
                Connection.CLose()

 DataReader can be closed when you are not using it.

I hope it wud help.

Regards,
Muskaan :)

 
Old February 17th, 2006, 04:06 AM
Wrox Technical Editor
 
Join Date: Dec 2005
Posts: 271
Thanks: 0
Thanked 0 Times in 0 Posts
Default

"Use Exceptions for exceptional problems." - The Pragmatic Prorgammer

Another alternative would be to use a "using" statement.

using ( SqlConnection conn = new SqlConnection() ){ ... }

See the following link for more details: http://weblogs.asp.net/jasonsalas/ar...08/368811.aspx



- A.Kahtava





Similar Threads
Thread Thread Starter Forum Replies Last Post
Correctly disposing dynamically created controls simarik C# 2 December 20th, 2005 08:49 PM
Releasing Memory in Access Paulsh Access VBA 2 August 24th, 2005 07:32 AM
Disposing a WinForm in Excel Bowmangotti VB.NET 2002/2003 Basics 0 October 5th, 2004 10:13 PM
Releasing VBA Application clueless_may Access VBA 2 June 17th, 2004 12:06 AM





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