c# - Dispose works fine, but not on terminating -


I have a class that implements IDisposable , because this image resources ( bitmap class) I use it to wrap all the deceptive lockbits / unlockbits And it works fine when I use the Dispose () or statement statement.

However, if I leave the program to end without exposing, I get a system.exvival exception . Simplicity, I think that GC calls me the dissection in the same way, and the object releases resources from envy, but it is not what is happening. Why?


Here is the IDisposable code:

  private bool _disposing = false; ~ QuickBitmap () {remove (wrong); } Public Zero Extraction (Settle (True); GC.SuppressFinalize (this);} Private Wide Dispos (Bull Rifed Decis) {If (_discising) Return; SaveBits (); // UnlockBit Bytes = Private Cover for Null; // byte [] image bmpData = null; // bitmapdata object if (safe Depression & BM! = Tap) {BM.Desc (); // bitmap object BM = null;} _disposing = true;}   

This works fine when:

 using  (var qbm = new QuickBitmap ("myfile.jpg")) {// use qbm.GetPixel Will be on /qbm.SetPixel}  < / Pre> 

This is when it does not work:

  public static zero main (string [] args) {// This is just an example, just create the object and By doing anything, the exception will throw var qbm = new QuickBitmap (args [0]); Qbm.SetPixel (0, 0, Color. Black); qbm.Save ();}   

Full excetion (no internal exception):

  An unrestricted exception of type 'System.AccessViolationException' in mscorlib.Dll Additional Information: Try reading or writing secure memory Ia it was often corrupt an indication that other memory.   

Reproduction is 100%, even on different machines. I understand that we should use the use or () , it is bad to not use it and I just want to know all these things: Why does this happen? Why is memory "protected"? What type of use am I doing? The problem occurs because you included an unnecessary final adder in your implementation. Lastly the code executed Typically can not see managed objects safely, it is likely that calling objects to the SaveBits are used in violation of this rule, though You did not include the code for that method.

The best solution will be QuickBitmap class

QuickBitmap class directly does not own unmanaged resources.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -