Click here to Skip to main content
16,016,570 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the below code please suggest the references which we are using and especially where ever we are using the 'new' keyword for the SqlCommand etc., should we explicitly dispose of the references or enclose them with 'using ()' or will it get garbage collected when it loses scope.

C#
private static Boolean clsinit()
{
    Boolean mret = true;
    String mconnstr = @"Data Source=PL\clsSQL,7000;Initial Catalog=clsms;User
    ID=1111;password=2222;";
    clsmain.clsconn = new SqlConnection(mconnstr);
    try
    {
        clsmain.clsconn.Open();
        SqlCommand msqlcmd = new SqlCommand("select licdate from customer",
        clsmain.clsconn);
        SqlDataReader msqldat = msqlcmd.ExecuteReader();
        msqldat.Read();
        if (DateTime.Now.Date > Convert.ToDateTime(msqldat["licdate"]).Date)
        {
            MessageBox.Show("Authentication Restricted.");
            mret = false;
        }
        else
        {
            clsmain.clsconn.Close();
            if (!pychk())
            {
                MessageBox.Show("Authentication Failed");
                mret = false;
            }
        }
    }
    catch
    {
        clsmain.oclsmsgmgr.showmsg("Error connecting Server...!|.",
        mret = false;
    }
    finally
    {
        clsmain.clsconn.Close();
    }
    loadcls mfrm = new loadcls();
    mfrm.ShowDialog();
    return mret;
}


Please Note : I have been given a task to figure out some memory leaks in a project. Please throw some light in a 'dummies' language on garbage collection. Im quite new to .NET GC operations.

Thanks in advance.

What I have tried:

I have searched the web for a clear picture but those have gone over my head. So probably hoping with my own example if someone clarifies I could understand better.
Posted
Updated 1-Aug-16 20:46pm

1 solution

That's really dirty code with all that static and global crap.
My impression from that snippet is that you are running your application in it, with the line mfrm.ShowDialog();, am I correct? Since all those disposable types you talked about are in the same function, it depends on the capabilities compiler to find out that they went out of scope. With different functions, that's easy: when the function is left, local variables are out of scope. I won't exclude that they get garbage collected even here, but I won't assume it either.
On the other hand, memory leaks of a function called once only are rather irrelevant. Look for places which are called repeatedly - small memory leaks there can sum up to relevant amounts.
 
Share this answer
 
Comments
Priya-Kiko 2-Aug-16 4:00am    
Quite offensive though, Thank you for the reply.

Sorry, We are not professionals like you all and are just doing some R&D with stuff in the self learning process.

This code snippet is part of a class library the references of which i want to access from other forms and other dlls just like --- class.reference = something. ---- This is what I came across doing some reading.

Moreover, the sqlcommand is used in several places in several functions. Basically i need to have some light on how to implement the dispose pattern.

I need some light on this please :

1. Seems that a variable loses scope when it leaves out the block. Now I have figured this is okay with an Int or a String but not with SqlCommand or SqlDataReader etc.,
2. Now these unmanaged resources as they are called must be handled for disposing.
3. It is impractical to have a line ref.Dispose where ever such references are used considering my lack of knowledge on which are unmanaged resources and which are not.
4. At this background, i have figured this.. Dispose pattern and GC etc., which I dont know how to implement in my project.
5. Should I implement each and every class of my project from IDisposable and add a Dispose() method. If so I have to keep track of all the references used in the module to dispose them here isn't it.

Or just Form.Close or calling the destructor of the class can't do the job ?


Am i understanding this right. Please suggest.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900