2013-09-24 2 views
0

코드를 단계별로 실행하고 작업 관리자에서 프로세스에서 사용하는 GDI 및 사용자 개체 수를 확인합니다. 주석에 쓴 코드의 객체 수를 추적했습니다. 다음 코드를 수행 한 후에는 출시되지 않은 사용자 개체와 출시되지 않은 GDI 개체가 하나씩 남아있는 것으로 나타났습니다. 내가 그들을 놓아주는 것을 잊었던 곳?ICONINFO를 파괴하는 방법?

using System; 
using System.Runtime.InteropServices; 

namespace UnmanagedResourcesTest 
{ 
    class Program 
    { 
     static void Main(string[] args)//20 user objects; 27 GDI objects 
     { 
      CURSORINFO ci = new CURSORINFO(); 
      ci.cbSize = Marshal.SizeOf(ci); 
      if (GetCursorInfo(out ci)) 
      { 
       ////21(+1) user objects; 27 GDI objects 
       if (ci.flags == CURSOR_SHOWING) 
       { 
        bool result; 
        IntPtr hicon = CopyIcon(ci.hCursor);//uo1/go1 
        ////22(+1) user objects; 28(+1) GDI objects 
        ICONINFO icInfo; 
        if (GetIconInfo(hicon, out icInfo))//go1 
        { 
         ////22 user objects; 29(+1) GDI objects 
         Console.WriteLine("ICONINFO gotten"); 
        } 
        result = DestroyIcon(hicon); 
        ////21(-1) user objects; 28(-1) GDI objects 
        Console.WriteLine("Is hicon destroyed? - " + result); 
       } 
      } 
      //leaves 21-20=1 not released user object and 28-27=1 not released GDI object 
      Console.ReadKey(); 
     } 

     private const Int32 CURSOR_SHOWING = 0x00000001; 

     [DllImport("user32.dll", EntryPoint = "GetCursorInfo")] 
     private static extern bool GetCursorInfo(out CURSORINFO pci); 

     [DllImport("user32.dll", EntryPoint = "CopyIcon")] 
     private static extern IntPtr CopyIcon(IntPtr hIcon); 

     [DllImport("user32.dll", EntryPoint = "GetIconInfo")] 
     private static extern bool GetIconInfo(IntPtr hIcon, out ICONINFO piconinfo); 

     [DllImport("user32.dll", SetLastError = true)] 
     static extern bool DestroyIcon(IntPtr hIcon); 

     [StructLayout(LayoutKind.Sequential)] 
     private struct ICONINFO 
     { 
      public bool fIcon;   // Specifies whether this structure defines an icon or a cursor. A value of TRUE specifies 
      public Int32 xHotspot;  // Specifies the x-coordinate of a cursor's hot spot. If this structure defines an icon, the hot 
      public Int32 yHotspot;  // Specifies the y-coordinate of the cursor's hot spot. If this structure defines an icon, the hot 
      public IntPtr hbmMask;  // (HBITMAP) Specifies the icon bitmask bitmap. If this structure defines a black and white icon, 
      public IntPtr hbmColor; // (HBITMAP) Handle to the icon color bitmap. This member can be optional if this 
     } 

     [StructLayout(LayoutKind.Sequential)] 
     private struct POINT 
     { 
      public Int32 x; 
      public Int32 y; 
     } 

     [StructLayout(LayoutKind.Sequential)] 
     private struct CURSORINFO 
     { 
      public Int32 cbSize;  // Specifies the size, in bytes, of the structure. 
      public Int32 flags;   // Specifies the cursor state. This parameter can be one of the following values: 
      public IntPtr hCursor;   // Handle to the cursor. 
      public POINT ptScreenPos;  // A POINT structure that receives the screen coordinates of the cursor. 
     } 
    } 
} 

답변

1

메모리 누수의 원인을 발견했습니다. 다음 행에 전화해야 함 :

result = DeleteObject(icInfo.hbmMask);