Should Dispose () ever create new instances of objects?

Using C # .NET 4.0

My company app uses a resource lock to edit records at the same time. We use the database to store the start time of the lock, as well as the user who acquired the lock. This led to the following (strange?) Implementation of utilization in the resource store, which, as it turns out, is called from the destructor:

protected virtual void Dispose(bool disposing)
        {
            lock (this)
            {
                if (lockid.HasValue)
                {
                    this.RefreshDataButtonAction = null;
                    this.ReadOnlyButtonAction = null;

                try
                {
                    **Dictionary<string, object> parameters = new Dictionary<string, object>();
                    parameters.Add("@lockID", lockid.Value);
                    parameters.Add("@readsToDelete", null);
                    Object returnObject = dbio2.ExecuteScalar("usp_DeleteResourceLockReads", parameters);**

                    lockid = null;
                }
                catch (Exception ex)
                {
                    Logger.WriteError("ResourceLockingController", "DeleteResourceLocks", ex);
                }
                finally
                {
                    ((IDisposable)_staleResourcesForm).Dispose();
                    _staleResourcesForm = null;
                }
            }
        }
    }

, "Handle is not initialized" . , Finalize(), dispose()? - , Dispose()?

+4
3

. , * dbio2 . .NET. , , , dbase, , "Handle ". , dbio2 .

, . , 2- - , dbase .

, . dbio2.ExecuteScalar(), . , , .

+1

Dispose - , . , / , Dispose.

, ; , Dispose, /. .

+1

, , . IDisposable - , ( ), , . Dispose (.. , , GC ), , . , #, . , IDisposable.

In your case, you are using the dbio2 object, which I assume represents your database connection. However, since this is called from the destructor, how do you know if your connection is still valid? You destructor may lose an hour after your connection. You should try to make sure that this Dispose is called while you know that the dbio2 object is still in scope.

+1
source

All Articles