4

我们突然在某些 Windows 安装中遇到了智能卡 API 的问题。调用 SCardEstablishContext 函数时似乎存在内存泄漏。可以在控制台应用程序中使用 http://www.pinvoke.net/default.aspx/winscard.scardestablishcontext提供的代码示例重现该问题

class Program
{
    #region Win32
    // WinSCard APIs to be imported.
    [DllImport("WinScard.dll")]
    static extern int SCardEstablishContext(uint dwScope,
        IntPtr notUsed1,
        IntPtr notUsed2,
        out IntPtr phContext);

    [DllImport("WinScard.dll")]
    static extern int SCardReleaseContext(IntPtr phContext);

    [DllImport("WinScard.dll")]
    static extern int SCardConnect(IntPtr hContext,
        string cReaderName,
        uint dwShareMode,
        uint dwPrefProtocol,
        ref IntPtr phCard,
        ref IntPtr ActiveProtocol);

    [DllImport("WinScard.dll")]
    static extern int SCardDisconnect(IntPtr hCard, int Disposition);

    [DllImport("WinScard.dll", EntryPoint = "SCardListReadersA", CharSet = CharSet.Ansi)]
    static extern int SCardListReaders(
      IntPtr hContext,
      byte[] mszGroups,
      byte[] mszReaders,
      ref UInt32 pcchReaders);
    #endregion

    static void Main(string[] args)
    {
        while (true)
        {
            SmartCardInserted();
            System.Threading.Thread.Sleep(10);
        }
    }

    internal static bool SmartCardInserted()
    {
        bool cardInserted = false;
        IntPtr hContext = IntPtr.Zero;

        try
        {
            List<string> readersList = new List<string>();

            int ret = 0;
            uint pcchReaders = 0;
            int nullindex = -1;
            char nullchar = (char)0;

            // Establish context.
            ret = SCardEstablishContext(2, IntPtr.Zero, IntPtr.Zero, out hContext);

            // First call with 3rd parameter set to null gets readers buffer length.
            ret = SCardListReaders(hContext, null, null, ref pcchReaders);

            byte[] mszReaders = new byte[pcchReaders];

            // Fill readers buffer with second call.
            ret = SCardListReaders(hContext, null, mszReaders, ref pcchReaders);

            // Populate List with readers.
            ASCIIEncoding ascii = new ASCIIEncoding();

            string currbuff = ascii.GetString(mszReaders);

            int len = (int)pcchReaders;

            if (len > 0)
            {
                while (currbuff[0] != nullchar)
                {
                    nullindex = currbuff.IndexOf(nullchar);   // Get null end character.
                    string reader = currbuff.Substring(0, nullindex);
                    readersList.Add(reader);
                    len = len - (reader.Length + 1);
                    currbuff = currbuff.Substring(nullindex + 1, len);
                }
            }

            // We have list of readers, check for cards.
            IntPtr phCard = IntPtr.Zero;
            IntPtr ActiveProtocol = IntPtr.Zero;
            int result = 0;

            foreach (string readerName in readersList)
            {
                try
                {
                    result = SCardConnect(hContext, readerName, 2, 3, ref phCard, ref ActiveProtocol);
                    if (result == 0)
                    {
                        cardInserted = true;
                        break;
                    }
                }
                finally
                {
                    SCardDisconnect(phCard, 0);
                }
            }
        }
        finally
        {
            SCardReleaseContext(hContext);
        }

        return cardInserted;

    }
}

为了测试,我们在一个无限循环中调用 SmartCardInserted() 方法,延迟很小 => 内存不断增长并分配了新的 hadles。

我们在运行 Windows 10 或 Windows Server 2012 的系统上看到了这个问题,但在 Windows Server 2008 上没有。

任何想法都非常感谢!

4

1 回答 1

8

该问题似乎已随 Windows 10 的 v1709 发布。重现该错误的最短代码量是

while(true) {
    ret = SCardEstablishContext(2, IntPtr.Zero, IntPtr.Zero, out hContext);
    SCardReleaseContext(hContext);
}

每次建立和释放上下文时,它都会泄漏约 264 字节的内存。

如果您在循环之外维护 hContext 并且仅在 IntPtr.Zero 时创建上下文,您应该能够避免泄漏。然后当你调用 SCardListReaders 时,检查你是否得到 SCARD_E_INVALID_HANDLE 并使你的 hContext 无效。

class Program
{
    #region Win32
    // WinSCard APIs to be imported.
    [DllImport("WinScard.dll")]
    static extern int SCardEstablishContext(uint dwScope,
        IntPtr notUsed1,
        IntPtr notUsed2,
        out IntPtr phContext);

    [DllImport("WinScard.dll")]
    static extern int SCardReleaseContext(IntPtr phContext);

    [DllImport("WinScard.dll")]
    static extern int SCardConnect(IntPtr hContext,
        string cReaderName,
        uint dwShareMode,
        uint dwPrefProtocol,
        ref IntPtr phCard,
        ref IntPtr ActiveProtocol);

    [DllImport("WinScard.dll")]
    static extern int SCardDisconnect(IntPtr hCard, int Disposition);

    [DllImport("WinScard.dll", EntryPoint = "SCardListReadersA", CharSet = CharSet.Ansi)]
    static extern int SCardListReaders(
      IntPtr hContext,
      byte[] mszGroups,
      byte[] mszReaders,
      ref UInt32 pcchReaders);
    #endregion

    static void Main(string[] args)
    {
        IntPtr hContext = IntPtr.Zero;
        while (true)
        {
            SmartCardInserted(hContext);
            System.Threading.Thread.Sleep(10);
        }
        SCardReleaseContext(hContext);
    }

    internal static bool SmartCardInserted(IntPtr hContext)
    {
        bool cardInserted = false;

        try
        {
            List<string> readersList = new List<string>();

            int ret = 0;
            uint pcchReaders = 0;
            int nullindex = -1;
            char nullchar = (char)0;

            // Establish context.
            if(hContext == IntPtr.Zero)
                ret = SCardEstablishContext(2, IntPtr.Zero, IntPtr.Zero, out hContext);

            // First call with 3rd parameter set to null gets readers buffer length.
            ret = SCardListReaders(hContext, null, null, ref pcchReaders);

            if(ret == 0x80100003) // SCARD_E_INVALID_HANDLE = 0x80100003, // The supplied handle was invalid
            {
                try
                {
                    SCardReleaseContext(hContext);
                }
                catch {}
                finally
                {
                    hContext = IntPtr.Zero;
                }
                return false;
            }

            byte[] mszReaders = new byte[pcchReaders];

            // Fill readers buffer with second call.
            ret = SCardListReaders(hContext, null, mszReaders, ref pcchReaders);

            // Populate List with readers.
            ASCIIEncoding ascii = new ASCIIEncoding();

            string currbuff = ascii.GetString(mszReaders);

            int len = (int)pcchReaders;

            if (len > 0)
            {
                while (currbuff[0] != nullchar)
                {
                    nullindex = currbuff.IndexOf(nullchar);   // Get null end character.
                    string reader = currbuff.Substring(0, nullindex);
                    readersList.Add(reader);
                    len = len - (reader.Length + 1);
                    currbuff = currbuff.Substring(nullindex + 1, len);
                }
            }

            // We have list of readers, check for cards.
            IntPtr phCard = IntPtr.Zero;
            IntPtr ActiveProtocol = IntPtr.Zero;
            int result = 0;

            foreach (string readerName in readersList)
            {
                try
                {
                    result = SCardConnect(hContext, readerName, 2, 3, ref phCard, ref ActiveProtocol);
                    if (result == 0)
                    {
                        cardInserted = true;
                        break;
                    }
                }
                finally
                {
                    SCardDisconnect(phCard, 0);
                }
            }
        }

        return cardInserted;
    }
}

在修复 Winscard.dll API 之前,这是一种解决方法。

于 2018-05-01T05:36:15.267 回答