释放临时 COM 对象

发布于 2024-08-19 18:34:56 字数 1029 浏览 6 评论 0原文

请考虑以下使用 COM 对象的 C# 代码。


MyComObject o = new MyComObject;
try
{
 var baz = o.Foo.Bar.Baz;
 try
 { 
  // do something with baz
 }
 finally
 {
  Marshal.ReleaseComObject(baz);
 }
}
finally
{
 Marshal.ReleaseComObject(o);
}

这将释放 COM 对象 obaz,但不会释放 o.Fooo.Foo.Bar 返回的临时对象。 当这些对象持有大量非托管内存或其他资源时,这可能会导致问题。

一个明显但丑陋的解决方案是,使用 try-finallyMarshal.ReleaseComObject 使代码更加混乱。看 用法

作为解决方法,我创建了一个帮助程序类


class TemporaryComObjects: IDisposable
{
 public C T<C>(C comObject)
 {
  m_objects.Add(comObject);
  return comObject;
 }
 public void Dispose()
 {
  foreach (object o in m_objects)
   Marshal.ReleaseComObject(o);
 }
}


using (TemporaryComObjects t = new TemporaryComObjects())
{
 MyComObject o = t.T(new MyComObject);
 var baz = t.T(t.T(t.T(o.Foo).Bar).Baz);
 // do something with baz
}

我的问题: 这段代码有潜在的问题吗? 有人有更优雅的解决方案吗?

Consider the following C# code using a COM object.


MyComObject o = new MyComObject;
try
{
 var baz = o.Foo.Bar.Baz;
 try
 { 
  // do something with baz
 }
 finally
 {
  Marshal.ReleaseComObject(baz);
 }
}
finally
{
 Marshal.ReleaseComObject(o);
}

This will release the COM objects o and baz, but not the temporary objects returnd by o.Foo and o.Foo.Bar.
This can cause problems, when those objects hold a large amount of unmanaged memory or other resources.

An obvious but ugly solution would be, to clutter the code even more with try-finally and Marshal.ReleaseComObject. See
C# + COM Interop, deterministic release

As a workaround, I created a helper class


class TemporaryComObjects: IDisposable
{
 public C T<C>(C comObject)
 {
  m_objects.Add(comObject);
  return comObject;
 }
 public void Dispose()
 {
  foreach (object o in m_objects)
   Marshal.ReleaseComObject(o);
 }
}

Usage:


using (TemporaryComObjects t = new TemporaryComObjects())
{
 MyComObject o = t.T(new MyComObject);
 var baz = t.T(t.T(t.T(o.Foo).Bar).Baz);
 // do something with baz
}

My questions:
Are there potential problems with this code?
Has anybody a more elegant solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你げ笑在眉眼 2024-08-26 18:34:56

我最大的抱怨是名字,TAdd 可能更能说明用法。我还将 where T : class 添加到通用方法中,但“流畅的 API”似乎可用。我也倾向于稍微简化代码。我还可以看到一些使用 Expression API 遍历整个树并捕获所有中间步骤的方法,但这不会微不足道 - 但想象一下:

using(var com = new SomeWrapper()) {
    var baz = com.Add(() => new MyComObject().Foo.Bar.Baz);
}

在哪里是一个表达式树,我们自动获取中介。

(此外,您可以在 Dispose()Clear()null 列表)


如下所示:

static class ComExample {
    static void Main()
    {
        using (var wrapper = new ReleaseWrapper())
        {
            var baz = wrapper.Add(
                () => new Foo().Bar.Baz);
            Console.WriteLine(baz.Name);
        }
    }
}

class ReleaseWrapper : IDisposable
{
    List<object> objects = new List<object>();
    public T Add<T>(Expression<Func<T>> func)
    {
        return (T)Walk(func.Body);
    }
    object Walk(Expression expr)
    {
        object obj = WalkImpl(expr);
        if (obj != null && Marshal.IsComObject(obj) && !objects.Contains(obj)) 
        {
            objects.Add(obj);
        }
        return obj;
    }
    object[] Walk(IEnumerable<Expression> args)
    {
        if (args == null) return null;
        return args.Select(arg => Walk(arg)).ToArray();
    }
    object WalkImpl(Expression expr)
    {
        switch (expr.NodeType)
        {
            case ExpressionType.Constant:
                return ((ConstantExpression)expr).Value;
            case ExpressionType.New:
                NewExpression ne = (NewExpression)expr;
                return ne.Constructor.Invoke(Walk(ne.Arguments));
            case ExpressionType.MemberAccess:
                MemberExpression me = (MemberExpression)expr;
                object target = Walk(me.Expression);
                switch (me.Member.MemberType)
                {
                    case MemberTypes.Field:
                        return ((FieldInfo)me.Member).GetValue(target);
                    case MemberTypes.Property:
                        return ((PropertyInfo)me.Member).GetValue(target, null);
                    default:
                        throw new NotSupportedException();

                }
            case ExpressionType.Call:
                MethodCallExpression mce = (MethodCallExpression)expr;
                return mce.Method.Invoke(Walk(mce.Object), Walk(mce.Arguments));
            default:
                throw new NotSupportedException();
        }
    }
    public void Dispose()
    {
        foreach(object obj in objects) {
            Marshal.ReleaseComObject(obj);
            Debug.WriteLine("Released: " + obj);
        }
        objects.Clear();
    }
}

My biggest gripe would be the name, T; Add might be more illusrative of the usage. I'd also add where T : class to the generic method, but the "fluent API" seems usable. I'd also be inclined to flatten the code a bit. I can also see some ways of using the Expression API to walk an entire tree and capture all the intermediate steps, but it wouldn't be trivial - but imagine:

using(var com = new SomeWrapper()) {
    var baz = com.Add(() => new MyComObject().Foo.Bar.Baz);
}

where that is an expression tree and we get the intermediaries automatically.

(also, you could Clear() or null the list in Dispose())


Like so:

static class ComExample {
    static void Main()
    {
        using (var wrapper = new ReleaseWrapper())
        {
            var baz = wrapper.Add(
                () => new Foo().Bar.Baz);
            Console.WriteLine(baz.Name);
        }
    }
}

class ReleaseWrapper : IDisposable
{
    List<object> objects = new List<object>();
    public T Add<T>(Expression<Func<T>> func)
    {
        return (T)Walk(func.Body);
    }
    object Walk(Expression expr)
    {
        object obj = WalkImpl(expr);
        if (obj != null && Marshal.IsComObject(obj) && !objects.Contains(obj)) 
        {
            objects.Add(obj);
        }
        return obj;
    }
    object[] Walk(IEnumerable<Expression> args)
    {
        if (args == null) return null;
        return args.Select(arg => Walk(arg)).ToArray();
    }
    object WalkImpl(Expression expr)
    {
        switch (expr.NodeType)
        {
            case ExpressionType.Constant:
                return ((ConstantExpression)expr).Value;
            case ExpressionType.New:
                NewExpression ne = (NewExpression)expr;
                return ne.Constructor.Invoke(Walk(ne.Arguments));
            case ExpressionType.MemberAccess:
                MemberExpression me = (MemberExpression)expr;
                object target = Walk(me.Expression);
                switch (me.Member.MemberType)
                {
                    case MemberTypes.Field:
                        return ((FieldInfo)me.Member).GetValue(target);
                    case MemberTypes.Property:
                        return ((PropertyInfo)me.Member).GetValue(target, null);
                    default:
                        throw new NotSupportedException();

                }
            case ExpressionType.Call:
                MethodCallExpression mce = (MethodCallExpression)expr;
                return mce.Method.Invoke(Walk(mce.Object), Walk(mce.Arguments));
            default:
                throw new NotSupportedException();
        }
    }
    public void Dispose()
    {
        foreach(object obj in objects) {
            Marshal.ReleaseComObject(obj);
            Debug.WriteLine("Released: " + obj);
        }
        objects.Clear();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文