1

While studying the following snippet of code from the project I work on

var storedObject = _objectStorage.OpenObject(objectId);
PerformActions(storeObject);

I discovered that storedObject object is disposable and need to be disposed. My first try was to enclose PerformActions in using statement:

using (var storedObject = _objectStorage.OpenObject(objectId))
{
    PerformActions(storeObject);
}

but as it turns out at least one action inside PerformActions is asynchronous and therefore I get ObjectDisposedException.

What refactoring strategy would you suggest for this case to eliminate issue with undisposed objects?

user10101
  • 1,704
  • 2
  • 20
  • 49
  • 1
    This is probably a poorly designed class. You have no hope if the class itself doesn't report itself dormant. You'll need to get in touch with the developer and ask him how you are supposed to dispose the object safely. If you don't get a satisfactory answer then just don't dispose it. Disposing is optional. – Hans Passant Mar 17 '13 at 13:27

0 Answers0