RefCountedFreeLibrary

We found 10 examples in language CSharp for this search. You will see 44 fragments of code.

		private SOSDac GetSOSInterfaceNoAddRef() {
			if (_sos is null) {
				_sos = InternalDacPrivateInterface.GetSOSDacInterface();
				if (_sos is null)
					throw new InvalidOperationException("This runtime does not support ISOSDac.");
			}

			return _sos;
		}

		public T? GetInterface<T>(in Guid riid)
			where T : CallableCOMWrapper {
			var pUnknown = InternalDacPrivateInterface.QueryInterface(riid);
			if (pUnknown == IntPtr.Zero)
				return null;

			var t = (T)Activator.CreateInstance(typeof(T), this, pUnknown)!;
			return t;
		}

		internal static IntPtr TryGetDacPtr(object ix) {
			if (ix is not IntPtr pUnk) {
				if (Marshal.IsComObject(ix))
					pUnk = Marshal.GetIUnknownForObject(ix);
				else
					pUnk = IntPtr.Zero;
			}

			if (pUnk == IntPtr.Zero)
				throw new ArgumentException("clrDataProcess not an instance of IXCLRDataProcess");

			return pUnk;
		}

		internal void Flush() {
			DacDataTarget.Flush();
		}

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

		private void Dispose(bool _) {
			if (!_disposed) {
				InternalDacPrivateInterface?.Dispose();
				_sos?.Dispose();
				OwningLibrary?.Release();

				_disposed = true;
			}
		}


		private SOSDac GetSOSInterfaceNoAddRef() {
			if (_sos is null) {
				_sos = InternalDacPrivateInterface.GetSOSDacInterface();
				if (_sos is null)
					throw new InvalidOperationException("This runtime does not support ISOSDac.");
			}

			return _sos;
		}

		public T? GetInterface<T>(in Guid riid)
			where T : CallableCOMWrapper {
			var pUnknown = InternalDacPrivateInterface.QueryInterface(riid);
			if (pUnknown == IntPtr.Zero)
				return null;

			var t = (T)Activator.CreateInstance(typeof(T), this, pUnknown)!;
			return t;
		}

		internal static IntPtr TryGetDacPtr(object ix) {
			if (ix is not IntPtr pUnk) {
				if (Marshal.IsComObject(ix))
					pUnk = Marshal.GetIUnknownForObject(ix);
				else
					pUnk = IntPtr.Zero;
			}

			if (pUnk == IntPtr.Zero)
				throw new ArgumentException("clrDataProcess not an instance of IXCLRDataProcess");

			return pUnk;
		}

		internal void Flush() {
			DacDataTarget.Flush();
		}

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

		private void Dispose(bool _) {
			if (!_disposed) {
				InternalDacPrivateInterface?.Dispose();
				_sos?.Dispose();
				OwningLibrary?.Release();

				_disposed = true;
			}
		}


        public int AddRef()
        {
            return Interlocked.Increment(ref _refCount);
        }

        public int Release()
        {
            int count = Interlocked.Decrement(ref _refCount);
            if (count == 0 && _library != IntPtr.Zero)
                DataTarget.PlatformFunctions.FreeLibrary(_library);

            return count;
        }


        public bool AllocateMemory(ulong address, uint size, uint typeFlags, uint protectFlags, out ulong remoteAddress)
        {
            return VTable.AllocVirtual(Self, address, size, typeFlags, protectFlags, out remoteAddress) == HResult.S_OK;
        }

        public bool FreeMemory(ulong address, uint size, uint typeFlags)
        {
            return VTable.FreeVirtual(Self, address, size, typeFlags) == HResult.S_OK;
        }


        public bool AllocateMemory(ulong address, uint size, uint typeFlags, uint protectFlags, out ulong remoteAddress)
        {
            return VTable.AllocVirtual(Self, address, size, typeFlags, protectFlags, out remoteAddress) == HResult.S_OK;
        }

        public bool FreeMemory(ulong address, uint size, uint typeFlags)
        {
            return VTable.FreeVirtual(Self, address, size, typeFlags) == HResult.S_OK;
        }


        public int AddRef()
        {
            _addRef ??= Marshal.GetDelegateForFunctionPointer<AddRefDelegate>(_unknownVTable->AddRef);

            int count = _addRef(Self);
            return count;
        }

        public void SuppressRelease()
        {
            _disposed = true;
            GC.SuppressFinalize(this);
        }

        public int Release()
        {
            _release ??= Marshal.GetDelegateForFunctionPointer<ReleaseDelegate>(_unknownVTable->Release);

            int count = _release(Self);
            return count;
        }

        public IntPtr QueryInterface(in Guid riid)
        {
            QueryInterfaceDelegate queryInterface = Marshal.GetDelegateForFunctionPointer<QueryInterfaceDelegate>(_unknownVTable->QueryInterface);

            HResult hr = queryInterface(Self, riid, out IntPtr unk);
            return hr.IsOK ? unk : IntPtr.Zero;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        protected static void InitDelegate<T>([NotNull] ref T? t, IntPtr entry)
            where T : Delegate
        {
            if (t != null)
                return;

            InitDelegateWorker(ref t, entry);
        }

        private static void InitDelegateWorker<T>([NotNull] ref T? t, IntPtr entry)
            where T : Delegate
        {
            t = Marshal.GetDelegateForFunctionPointer<T>(entry);

#if DEBUG
            if (t.Method.GetParameters().First().ParameterType != typeof(IntPtr))
                throw new InvalidOperationException();
#endif
        }


        public int AddRef()
        {
            _addRef ??= Marshal.GetDelegateForFunctionPointer<AddRefDelegate>(_unknownVTable->AddRef);

            int count = _addRef(Self);
            return count;
        }

        public void SuppressRelease()
        {
            _disposed = true;
            GC.SuppressFinalize(this);
        }

        public int Release()
        {
            _release ??= Marshal.GetDelegateForFunctionPointer<ReleaseDelegate>(_unknownVTable->Release);

            int count = _release(Self);
            return count;
        }

        public IntPtr QueryInterface(in Guid riid)
        {
            QueryInterfaceDelegate queryInterface = Marshal.GetDelegateForFunctionPointer<QueryInterfaceDelegate>(_unknownVTable->QueryInterface);

            HResult hr = queryInterface(Self, riid, out IntPtr unk);
            return hr.IsOK ? unk : IntPtr.Zero;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        protected static void InitDelegate<T>([NotNull] ref T? t, IntPtr entry)
            where T : Delegate
        {
            if (t != null)
                return;

            InitDelegateWorker(ref t, entry);
        }

        private static void InitDelegateWorker<T>([NotNull] ref T? t, IntPtr entry)
            where T : Delegate
        {
            t = Marshal.GetDelegateForFunctionPointer<T>(entry);

#if DEBUG
            if (t.Method.GetParameters().First().ParameterType != typeof(IntPtr))
                throw new InvalidOperationException();
#endif
        }


        public int AddRef()
        {
            int count = _unknownVTable->AddRef(Self);
            return count;
        }

        public void SuppressRelease()
        {
            _disposed = true;
            GC.SuppressFinalize(this);
        }

        public int Release()
        {
            int count = _unknownVTable->Release(Self);
            return count;
        }

        public IntPtr QueryInterface(in Guid riid)
        {
            HResult hr = _unknownVTable->QueryInterface(Self, riid, out IntPtr unk);
            return hr.IsOK ? unk : IntPtr.Zero;
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                Release();
                _library.Release();
                _disposed = true;
            }
        }

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


        private SOSDac GetSOSInterfaceNoAddRef()
        {
            if (_sos is null)
            {
                _sos = InternalDacPrivateInterface.GetSOSDacInterface();
                if (_sos is null)
                    throw new InvalidOperationException("This runtime does not support ISOSDac.");
            }

            return _sos;
        }

        public T? GetInterface<T>(in Guid riid)
            where T : CallableCOMWrapper
        {
            IntPtr pUnknown = InternalDacPrivateInterface.QueryInterface(riid);
            if (pUnknown == IntPtr.Zero)
                return null;

            T t = (T)Activator.CreateInstance(typeof(T), this, pUnknown)!;
            return t;
        }

        internal static IntPtr TryGetDacPtr(object ix)
        {
            if (!(ix is IntPtr pUnk))
            {
                if (Marshal.IsComObject(ix))
                    pUnk = Marshal.GetIUnknownForObject(ix);
                else
                    pUnk = IntPtr.Zero;
            }

            if (pUnk == IntPtr.Zero)
                throw new ArgumentException("clrDataProcess not an instance of IXCLRDataProcess");

            return pUnk;
        }

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

        private void Dispose(bool _)
        {
            if (!_disposed)
            {
                InternalDacPrivateInterface?.Dispose();
                _sos?.Dispose();
                OwningLibrary?.Release();

                _disposed = true;
            }
        }

        [Fact]
        public void EnsureFinalReleaseOfInterfaces()
        {
            using DataTarget dt = TestTargets.Types.LoadFullDump();

            RefCountedFreeLibrary library;
            SOSDac sosDac;

            using (ClrRuntime runtime = dt.ClrVersions.Single().CreateRuntime())
            {
                library = runtime.DacLibrary.OwningLibrary;
                sosDac = runtime.DacLibrary.SOSDacInterface;

                // Keep library alive
                library.AddRef();
            }

            sosDac.Dispose();
            Assert.Equal(0, library.Release());
        }

        [LinuxFact]
        public void CreateSnapshotAndAttach_ThrowsPlatformNotSupportedException()
        {
            _ = Assert.Throws<PlatformNotSupportedException>(() => DataTarget.CreateSnapshotAndAttach(Process.GetCurrentProcess().Id));
        }

        [WindowsFact]
        public void LoadDump_ThrowsInvalidDataExceptionForEmptyFile()
        {
            string path = Path.GetTempFileName();
            _ = Assert.Throws<InvalidDataException>(() => DataTarget.LoadDump(path));
        }

Microsoft.Diagnostics.Runtime.RefCountedFreeLibrary : Object

Constructors :

public RefCountedFreeLibrary(IntPtr library = )

Methods :

public Void AddRef()
public Int32 Release()
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()