ObjectDisposedException thrown afyter using iDB2Connection object.

  • Thread starter Thread starter Jason H
  • Start date Start date
J

Jason H

Hi All,

This is my first post here and concerns using IBM's DB2 drivers. I don't
normally use DB2 so I don't have a lot of experience with it. Okay, enough of
the preamble.... :)

My question is.....

If I use the iDB2Connection then close the form it's held in I get an
"ObectDisposedException" thrown. Example code below....

private void button1_Click(object sender, EventArgs e)
{
string cs = Properties.Settings.Default.CS.ToString();
iDB2Connection cn = new iDB2Connection(cs);
//using(iDB2Connection cn = new iDB2Connection(cs))
//{
//}
cn.Dispose();
}


I've tried various things to get round this problem, removing more and more
lines until I get to this. The object can be used with the database. You'll
not that the above example does not even try to open a DB connection. It
appears enough that the object be created and disposed. Note also that the
exception doesn't get thrown until the form is closed. It doesn't matter how
long you wait.

I'd like to know how I stop the exception from being thrown. Failing that
what is the best way to trap the exception on a closed form?

Thanks

Jason.
 
Hello Peter,

Thank you for you reply. Sorry I missed out information.....

The object that is the problem is iDB2Connection and is part of the IBM
supplied drivers, it's in the namespace "IBM.Data.DB2.iSeries".

As you correctly guessed, the sample code was in a windows Form.

Not sure where the exception is raised from but the information I can get to
is....

System.ObjectDisposedException was unhandled
Message="Safe handle has been closed"
Source="mscorlib"
ObjectName=""
StackTrace:
at System.Runtime.InteropServices.SafeHandle.DangerousRelease()
at System.Threading.RegisteredWaitHandleSafe.Finalize()
InnerException:


I've added a little error trapping as shown below. However the exception is
thrown after it completes this code. The debug.print lines don't get run.

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
catch (ObjectDisposedException ode)
{
System.Diagnostics.Debug.Print("ObjectDisposedException");
}
catch (Exception ex)
{
System.Diagnostics.Debug.Print("Exception");
}
}
}

Not sure how to trap this?

Any ideas?

Jason.



Peter Duniho said:
[...]
I've tried various things to get round this problem, removing more and
more
lines until I get to this. The object can be used with the database.
You'll
not that the above example does not even try to open a DB connection. It
appears enough that the object be created and disposed. Note also that
the
exception doesn't get thrown until the form is closed. It doesn't matter
how
long you wait.

I'd like to know how I stop the exception from being thrown. Failing that
what is the best way to trap the exception on a closed form?

You haven't posted enough information for anyone to know what the problem
is. You haven't even stated what object is the one being accessed, nor
what the call stack is when the error occurs.

That said, there are two possibilities, given the information you've
provided so far (obviously, since you've provided incomplete information,
there may be other possibilities not knowable given the information
provided so far):

-- You've got some code (probably in the Form instance) that, when the
form closes, attempts to use the iDB2Connection instance that's already
been disposed

-- You've got some code (probably in the iDB2Connection instance)
that, when the form closes, attempts to use the Form instance that's
already been disposed.

Note that even the location of the problematic code cannot be reliably
determined from the information given.

Without a concise-but-complete code example, it's not possible to know
which is actually the case. From the example you provided, there's no
obvious reason _either_ could be happening, but obviously one or the other
must be.

Pete
 
Hello Peter,

Thank you for you reply. Sorry I missed out information.....

The object that is the problem is iDB2Connection and is part of the IBM
supplied drivers, it's in the namespace "IBM.Data.DB2.iSeries".

As you correctly guessed, the sample code was in a windows Form.

Not sure where the exception is raised from but the information I can get to
is....

System.ObjectDisposedException was unhandled
Message="Safe handle has been closed"
Source="mscorlib"
ObjectName=""
StackTrace:
at System.Runtime.InteropServices.SafeHandle.DangerousRelease()
at System.Threading.RegisteredWaitHandleSafe.Finalize()
InnerException:


I've added a little error trapping as shown below. However the exception is
thrown after it completes this code. The debug.print lines don't get run.

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
catch (ObjectDisposedException ode)
{
System.Diagnostics.Debug.Print("ObjectDisposedException");
}
catch (Exception ex)
{
System.Diagnostics.Debug.Print("Exception");
}
}
}

Not sure how to trap this?

Any ideas?

Jason.



Peter Duniho said:
[...]
I've tried various things to get round this problem, removing more and
more
lines until I get to this. The object can be used with the database.
You'll
not that the above example does not even try to open a DB connection. It
appears enough that the object be created and disposed. Note also that
the
exception doesn't get thrown until the form is closed. It doesn't matter
how
long you wait.

I'd like to know how I stop the exception from being thrown. Failing that
what is the best way to trap the exception on a closed form?

You haven't posted enough information for anyone to know what the problem
is. You haven't even stated what object is the one being accessed, nor
what the call stack is when the error occurs.

That said, there are two possibilities, given the information you've
provided so far (obviously, since you've provided incomplete information,
there may be other possibilities not knowable given the information
provided so far):

-- You've got some code (probably in the Form instance) that, when the
form closes, attempts to use the iDB2Connection instance that's already
been disposed

-- You've got some code (probably in the iDB2Connection instance)
that, when the form closes, attempts to use the Form instance that's
already been disposed.

Note that even the location of the problematic code cannot be reliably
determined from the information given.

Without a concise-but-complete code example, it's not possible to know
which is actually the case. From the example you provided, there's no
obvious reason _either_ could be happening, but obviously one or the other
must be.

Pete
 
Back
Top