aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Core/Msi/MsiHandle.cs
blob: 6d2dc98493f059177344fd4050c4bdd9aabc8c37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolset.Msi
{
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Threading;
    using WixToolset.Core.Native;

    /// <summary>
    /// Wrapper class for MSI handle.
    /// </summary>
    public class MsiHandle : IDisposable
    {
        private bool disposed;
        private uint handle;
        private int owningThread;
#if DEBUG
        private string creationStack;
#endif

        /// <summary>
        /// MSI handle destructor.
        /// </summary>
        ~MsiHandle()
        {
            this.Dispose(false);
        }

        /// <summary>
        /// Gets or sets the MSI handle.
        /// </summary>
        /// <value>The MSI handle.</value>
        internal uint Handle
        {
            get
            {
                if (this.disposed)
                {
                    throw new ObjectDisposedException("MsiHandle");
                }

                return this.handle;
            }

            set
            {
                if (this.disposed)
                {
                    throw new ObjectDisposedException("MsiHandle");
                }

                this.handle = value;
                this.owningThread = Thread.CurrentThread.ManagedThreadId;
#if DEBUG
                this.creationStack = Environment.StackTrace;
#endif
            }
        }

        /// <summary>
        /// Close the MSI handle.
        /// </summary>
        public void Close()
        {
            this.Dispose();
        }

        /// <summary>
        /// Disposes the managed and unmanaged objects in this object.
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Disposes the managed and unmanaged objects in this object.
        /// </summary>
        /// <param name="disposing">true to dispose the managed objects.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (0 != this.handle)
                {
                    if (Thread.CurrentThread.ManagedThreadId == this.owningThread)
                    {
                        int error = MsiInterop.MsiCloseHandle(this.handle);
                        if (0 != error)
                        {
                            throw new Win32Exception(error);
                        }
                        this.handle = 0;
                    }
                    else
                    {
                        // Don't try to close the handle on a different thread than it was opened.
                        // This will occasionally cause MSI to AV.
                        string message = String.Format("Leaked msi handle {0} created on thread {1} by type {2}.  This handle cannot be closed on thread {3}",
                            this.handle, this.owningThread, this.GetType(), Thread.CurrentThread.ManagedThreadId);
#if DEBUG
                        throw new InvalidOperationException(String.Format("{0}.  Created {1}", message, this.creationStack));
#else
                        Debug.WriteLine(message);
#endif
                    }
                }

                this.disposed = true;
            }
        }
    }
}