You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Coming along with the introduction of writer API for .net wrapper is the Image class. This class implements IDisposable as it maintains an unmanaged resource (_d member variable). I think other wrapper classes that do the same thing should implement IDisposable too.
A guideline already exists regarding how to implement the "disposable" pattern. There are 2 key behaviors I would like to point out:
An disposable object should be able to be disposed at anytime (i.e. without relying on GC to do the job through finalizers)
A Dispose method should be idempotent
Another thing to consider is how a disposable class should behave when we access its properties/methods when it is disposed. I have seen some code check for disposed state (usually indicated by a private variable) and throw an exception (preferably ObjectDisposedException). Others code would omit such checks. For zxing-cpp .net wrapper I think either way is fine. Accessing a disposed object's members without guards should lead to dereferencing a null pointer on the native side and that's a kind of (implicitly) throwing an exception.
The text was updated successfully, but these errors were encountered:
The reason I chose to implement IDisposable for the Image wrapper is not that it maintains an unmanaged resource but because of two conditions simultaneously true: a) the unmanaged memory behind _d can be large and b) there is a use case in which a lot of those Image objects can be produced in rapid succession. To some extend those two conditions might be true for the Barcode class as well, but not for any of the other. And having to deal with disposed objects is exactly an argument against introducing it in the first place. Not having to worry about it is exactly the value of a garbage collector.
Hi,
Coming along with the introduction of writer API for .net wrapper is the
Image
class. This class implementsIDisposable
as it maintains an unmanaged resource (_d
member variable). I think other wrapper classes that do the same thing should implementIDisposable
too.A guideline already exists regarding how to implement the "disposable" pattern. There are 2 key behaviors I would like to point out:
Dispose
method should be idempotentAnother thing to consider is how a disposable class should behave when we access its properties/methods when it is disposed. I have seen some code check for disposed state (usually indicated by a private variable) and throw an exception (preferably ObjectDisposedException). Others code would omit such checks. For zxing-cpp .net wrapper I think either way is fine. Accessing a disposed object's members without guards should lead to dereferencing a null pointer on the native side and that's a kind of (implicitly) throwing an exception.
The text was updated successfully, but these errors were encountered: