Beispiel #1
0
        public LockBytesWrapper(BinaryData data)
        {
            try
            {
                m_data = data;
                data.LockInMemory();
                if (data is OptimizedBinaryData)
                {
                    OptimizedBinaryData optData = data as OptimizedBinaryData;
                    m_lockbytesinterface = optData.AsLockBytes() as StructuredStorageInterface.ILockBytes;
                }
                else
                {
                    int len = data.Length;
                    m_hglobalHandle = Marshal.AllocHGlobal(len);
                    data.CopyTo(m_hglobalHandle);

                    StructuredStorageInterface.CreateILockBytesOnHGlobal(m_hglobalHandle, false, out m_lockbytesinterface);
                }
            }
            catch (Exception)
            {
                // if we throw in here, we most likely wont get disposed properly (the new this won't be assigned to anything)
                // this becomes an issue because we have locked the binary data in memory and wont be releasing it
                // so to keep things tidy and make sure we don't get spurious errors we will release the binary data object
                // and tidy up ourselves in case of an exception
                Dispose(true);
                throw;
            }

        }
Beispiel #2
0
        protected virtual void Dispose(bool disposing)
        {
            lock (this)
            {
                if (m_bdisposed)
                    return;

                m_bdisposed = true;
            }

            if (disposing)
                GC.SuppressFinalize(this);

            if (null != m_hglobalHandle)
                Marshal.FreeHGlobal(m_hglobalHandle);

            m_hglobalHandle = new IntPtr(0) ;

            if (null != m_lockbytesinterface)
                Marshal.ReleaseComObject(m_lockbytesinterface);

            m_lockbytesinterface = null;

            if (m_data != null)
                m_data.ReleaseFromMemory();

            m_data = null;
        }