0

I'm writing a C# wrapper around a native dll that uses opaque pointers to identify resources. A typical example would be something like

typedef struct session session;
typedef struct track track;

void* sessionCreate(int arg, session** sess);
void  sessionRelease(session** sess);
track* sessionGetTrack(session* sess, int index);
void trackAddRef(track* trk);
void trackRelease(track* trk);
int  trackGetLength(track* trk);

In my wrapper, I've created C# classes corresponding to the different opaque types, with member functions corresponding to the various functions using the different opaque types.

This is fine. There are also callbacks from the dll, for example

void(* track_changed )(track *trk, bool changedExternally);

In order to map from the static delegate that handles the callback to the object that corresponds to the handle supplied, I'm using a static dictionary of WeakReferences (IntPtr/SafeHandle as key, object reference as aata) in each of my classes.

So what is the right way to approach removing entries from the static dictionary? I'm writing library code and cannot rely on my clients to Dispose my objects. Should the I put the code in the finalizer?

Or is there a better way to manage the correspondence between the static callbacks and my object instances?

4

1 に答える 1

1

他の BCL クラスの場合と同様に、オブジェクトは であるため、クライアント実際にオブジェクトを破棄する必要があります(実際、私はmustと言いたいのですが)。IDisposable

いずれにせよ、破棄/ファイナライズ パターンのボグ標準実装は、すべてのベースをカバーする必要があります。さらに関連する議論がここにあります

于 2013-01-22T15:16:28.590 に答える