Click here to Skip to main content
15,905,874 members
Home / Discussions / C#
   

C#

 
GeneralRe: Destructor peculiarity Pin
DaveyM6927-Oct-09 10:45
professionalDaveyM6927-Oct-09 10:45 
GeneralRe: Destructor peculiarity Pin
Luc Pattyn27-Oct-09 11:17
sitebuilderLuc Pattyn27-Oct-09 11:17 
GeneralRe: Destructor progress report 1 Pin
Luc Pattyn27-Oct-09 17:20
sitebuilderLuc Pattyn27-Oct-09 17:20 
GeneralRe: Destructor progress report 1 Pin
DaveyM6928-Oct-09 8:58
professionalDaveyM6928-Oct-09 8:58 
GeneralRe: Destructor progress report 1 Pin
Luc Pattyn28-Oct-09 9:54
sitebuilderLuc Pattyn28-Oct-09 9:54 
GeneralRe: Destructor progress report 1 Pin
DaveyM6928-Oct-09 10:52
professionalDaveyM6928-Oct-09 10:52 
GeneralRe: Destructor progress report 1 Pin
DaveyM6928-Oct-09 22:19
professionalDaveyM6928-Oct-09 22:19 
GeneralRe: Destructor progress report 1 Pin
DaveyM6928-Oct-09 9:35
professionalDaveyM6928-Oct-09 9:35 
Further to my earlier reply, if I were to implement IDisposable the Port class would need to look something like this. I think this is messier but I do understand the reasoning for the recommendation for using Dispose.
C#
using System;

namespace MIDI.NET.Ports
{
    /// <summary>
    /// Represents a MIDI port.
    /// </summary>
    public abstract class Port : IDisposable
    {
        bool disposed;

        ~Port()
        {
            Dispose(false);
        }

        public virtual void Close()
        {
            if (disposed)
                throw new ObjectDisposedException(
                    ToString(),
                    "This port has been disposed. If you wish to reuse it please use the ReCreate method.");
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    // Managed clean up only
                }
                Close();
                disposed = true;
            }
        }

        public void Recreate()
        {
            if (disposed)
            {
                GC.ReRegisterForFinalize(this);
                disposed = false;
            }
        }
    }
}


I haven't tested this yet, but I'm 99% certain this gave me problems when I tried a similar implementation some months ago. Sometimes ports never closed so the application hung (often invisibly) or it created system instability, sometimes I got Access Violation (or similar) errors. I never got to the bottom of the problem but I figured it was one of the following:

1. Close being called on the instance could not be guaranteed to be successful as fields of the class may no longer be accessible (a loose theory and quite possibly/likely wrong!)
2. When calling Open on the port a callback is created which lasts beyond the call to Close as the Close itself generates a callback. Once this callback is registered it cannot be unregistered (that's the way the API works). Calling Close during Dispose may be too late as the callback delegate (and it's target) may now be collected making the unmanaged callback invalid (quite probable although badly explained!)

Dave

"My code works, but I don't understand why!" - DaveyM69 (Me)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus)

QuestionTheading problem Pin
Rick van Woudenberg23-Oct-09 0:52
Rick van Woudenberg23-Oct-09 0:52 
AnswerRe: Theading problem Pin
Not Active23-Oct-09 2:49
mentorNot Active23-Oct-09 2:49 
Questionneed some quick help thx : how can i access and open form2 from form1 menu item Pin
KIM K23-Oct-09 0:48
KIM K23-Oct-09 0:48 
AnswerRe: need some quick help thx : how can i access and open form2 from form1 menu item Pin
nagendrathecoder23-Oct-09 0:55
nagendrathecoder23-Oct-09 0:55 
AnswerRe: need some quick help thx : how can i access and open form2 from form1 menu item Pin
Rick van Woudenberg23-Oct-09 0:57
Rick van Woudenberg23-Oct-09 0:57 
Questionhow to update progressbar in Child Form from Parent Form Pin
sandy_55723-Oct-09 0:39
sandy_55723-Oct-09 0:39 
AnswerRe: how to update progressbar in Child Form from Parent Form Pin
Rick van Woudenberg23-Oct-09 1:03
Rick van Woudenberg23-Oct-09 1:03 
GeneralRe: how to update progressbar in Child Form from Parent Form Pin
benjymous23-Oct-09 1:08
benjymous23-Oct-09 1:08 
GeneralRe: how to update progressbar in Child Form from Parent Form Pin
sandy_55723-Oct-09 2:16
sandy_55723-Oct-09 2:16 
AnswerRe: how to update progressbar in Child Form from Parent Form Pin
benjymous23-Oct-09 1:07
benjymous23-Oct-09 1:07 
QuestionDisplaying Chinese characters Crystal reports Pin
dasha_pl23-Oct-09 0:31
dasha_pl23-Oct-09 0:31 
QuestionWhy cant i do this? Pin
ONeil Tomlinson22-Oct-09 23:48
ONeil Tomlinson22-Oct-09 23:48 
AnswerRe: Why cant i do this? Pin
Calla22-Oct-09 23:50
Calla22-Oct-09 23:50 
AnswerRe: Why cant i do this? Pin
Christian Graus23-Oct-09 0:08
protectorChristian Graus23-Oct-09 0:08 
AnswerRe: Why cant i do this? Pin
dxlee23-Oct-09 5:00
dxlee23-Oct-09 5:00 
QuestionLast grandchild of a node in a tree Pin
devboycpp22-Oct-09 23:17
devboycpp22-Oct-09 23:17 
AnswerRe: Last grandchild of a node in a tree [modified] Pin
dan!sh 23-Oct-09 0:27
professional dan!sh 23-Oct-09 0:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.