Code Monkey home page Code Monkey logo

Comments (12)

DanielGilbert avatar DanielGilbert commented on July 17, 2024

Using the _disposed syntax is considered best practice. It might be better to add the null-check right before the expression that causes this crash. Feels better, imho, and should cause less sideeffects we can't even think of at the moment.

from cscore.

ziriax avatar ziriax commented on July 17, 2024

Ok, then the fix requires a tiny bit more code, but doable.

Personally I do not agree with many best practices; after 25 years of fulltime software development, I see many "best practices" swing like a pendulum ;-)

from cscore.

DanielGilbert avatar DanielGilbert commented on July 17, 2024

So do I ;) But I was wondering why your pull request is in the queue for like two months now, and that was my guess. I encountered the same exception today, and came looking if some else already encountered this issue.

Looking at Dispose, I wonder if it would need more hardening. Alas, as far as I can see, there is no IsDisposing or IsDisposed in the WaveWriter. So when calling Dispose(), the caller needs to ensure that no thread calls Writer from the WaveWriter. And two threads should never access Dispose() at the same time, so the caller needs some more synching here.

But that are only whishes and just my two cents. All in all, it's an awesome library.

from cscore.

ziriax avatar ziriax commented on July 17, 2024

Oh, I even did not know that your classes were thread safe.

from cscore.

DanielGilbert avatar DanielGilbert commented on July 17, 2024

My classes? I'm not the author, just a random stranger. :)

from cscore.

ziriax avatar ziriax commented on July 17, 2024

Lol, sorry, I guess I was a bit too impulsive here ;-)

from cscore.

filoe avatar filoe commented on July 17, 2024

If the constructor crashes, there won't be any instance to dispose. Am I wrong?

from cscore.

DanielGilbert avatar DanielGilbert commented on July 17, 2024

Thanks for jumping in filoe.

Unfortunately you are - the finalizer will be called when you've got an exception in the constructor. Because there is no distinction between managed and unmanaged resources in Dispose() (that's the idea of the disposing var, as the comment says ;) ), an exception will be thrown.

As a bonus: For the user, it would be good to know if the object is in an "disposing" state, because then the user can block concurrent access to the write method - and it might take a while to write the header to the wav file on large files.* So a simple IsDisposing property would be awesome. And, for the sake of completeness, what will happen if I access the object after it being disposed? An IsDisposedwould make sense, at least from my point of view.

* Edit: Just realized you are already reserving the space for the header. Shouldn't depend on the filesize, just on plain i/o speed.

from cscore.

ziriax avatar ziriax commented on July 17, 2024

True, but the finalizer is called, so Dispose is called indirectly. I made a small test program to confirm this.

.NET always allocates the object (zeroed) before calling the constructor... So it will be finalized, since memory was allocated for it.

If you want to avoid these problems, then make a static method WaveWriter.FromFilePath instead. And just keep dummy constructors that cannot fail, or wrap the constructor bodies in try/catch, calling Dispose in the catch and rethrowing. Ugly, and could be avoided if the CLR would evaluate all arguments passed to a constructor before allocating an object, but helas...

from cscore.

ziriax avatar ziriax commented on July 17, 2024

Sometimes I even ended up with an enumeration and property LifeCycle { Allocated, Constructing, Constructed, Disposing, Disposed } ;-)

from cscore.

DanielGilbert avatar DanielGilbert commented on July 17, 2024

FYI, I've made a suggestion for a change to this class. Let me know your constructive feedback. 😃

from cscore.

filoe avatar filoe commented on July 17, 2024

Thanks for your contribution. Got merged :).

from cscore.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.