WorstNoob
WorstNoob

Reputation: 1

SCardConnect C# Memory Leak

I am having trouble fixing a memory leak causes by SCardConnect function in winscard.dll library wrapper. Are there a way to fix the memory by winscard.dll

Summary: When the SCardConnect successfully returns 0, which means that there is a card presents on the reader, the memory goes off the roof.

Details:

I found out that the SCardEstablishContext also create memory leak so I tried to minimize the use of that function as much as possible. I also tried to use SCardStatusChange function but it also creates memory leak and the logic is complicated.

  1. Wrapper Card.cs:
[DllImport("winscard.dll")]
        public static extern int SCardConnect(int hContext, string szReaderName, int dwShareMode, int dwPrefProtocol, ref int phCard, ref int ActiveProtocol);
  1. Main Code:(omitted some global variables)
public Form1()
        {
            InitializeComponent();

            do
            {

                if (this.Connect())
                {

                }
                else
                {

                }

            }
            while (true);
        }

public bool Connect()
    {
        if(string.IsNullOrEmpty(readername))
            try
            {
                readername = this.GetReadersList()[0];
            }
            catch(Exception e) { }


        retCode = Card.SCardConnect(hContext, readername, Card.SCARD_SHARE_SHARED,
                             Card.SCARD_PROTOCOL_T0 | Card.SCARD_PROTOCOL_T1, ref hCard, ref Protocol);
        
        if (retCode != Card.SCARD_S_SUCCESS)
        {
            return false;
        }
        else
        {
            return true;
        }       
        
    }

public List<string> GetReadersList()
    {
        string ReaderList = "" + Convert.ToChar(0);
        int indx;
        int pcchReaders = 0;
        string rName = "";
        List<string> lstReaders = new List<string>();
        //Establish Context
        retCode = Card.SCardEstablishContext(Card.SCARD_SCOPE_USER, 0, 0, ref hContext);


        // 2. List PC/SC card readers installed in the system

        retCode = Card.SCardListReaders(this.hContext, null, null, ref pcchReaders);

        if (retCode != Card.SCARD_S_SUCCESS)
        {
            throw new Exception("Error SCardListReaders");
        }

        byte[] ReadersList = new byte[pcchReaders];

        // Fill reader list
        retCode = Card.SCardListReaders(this.hContext, null, ReadersList, ref pcchReaders);

        if (retCode != Card.SCARD_S_SUCCESS)
        {
            throw new Exception("Error SCardListReaders");
        }

        rName = "";
        indx = 0;


        while (ReadersList[indx] != 0)
        {

            while (ReadersList[indx] != 0)
            {
                rName += (char)ReadersList[indx];
                indx++;
            }


            lstReaders.Add(rName);
            rName = "";
            indx++;

        }
        return lstReaders;
    }

  1. Memory diagnostic: enter image description here.

All the memory up-ramps are at the time of card holding on the reader.

PS: I have tried this in Window Form, Console, and Window Service platforms.

Upvotes: 0

Views: 301

Answers (0)

Related Questions