Link to home
Start Free TrialLog in
Avatar of MEGADETH
MEGADETH

asked on

CFileDialog InterlockedDecrement

All,

I use CFileDialog to get the file path name for a user.

void CTransactionListFilterDlg::OnBtnFile()
{
//////////////////////////////////////////////////////////////////////////////////////
        // File path name
     TCHAR szFilePathName[_MAX_PATH+1];
     memset(szFilePathName, 0, _MAX_PATH+1);

     CFileDialog dlg(TRUE);
     dlg.m_ofn.lpstrFile = &szFilePathName[0];
     dlg.m_ofn.nMaxFile = _MAX_PATH;
     dlg.m_ofn.lpstrTitle = _T("Add from file");
     dlg.m_ofn.Flags =  dlg.m_ofn.Flags | OFN_ENABLESIZING |OFN_LONGNAMES | OFN_NODEREFERENCELINKS | OFN_PATHMUSTEXIST | OFN_SHAREAWARE;



     int dm = dlg.DoModal();
     if(dm != IDOK)
          return;

        CStringArray arsTransID;
     if(!ReadFile(szFilePathName, arsTransID))
          return;

//////////////////////////////////////////////////////////////////////////////////////

}


This code works until before it exits the OnBtnFile() method.

I use Boundchecker and I got a error message inside the ~CString for "InterlockedDecrement".
Something is wrong with the reference count that cause this problem.

I wonder if the problem lies because this OnBtnFile() is called after two dialog boxes and somehow there is a dangling pointer.

Any help is highly appreaciated.
I am new to this forum, so, I apologize if the points that I put does not reflect to the problem.
 

Avatar of aphillips
aphillips
Flag of Australia image

I can't see anything wrong.  Can you post the code for your ReadFile function? Is there anything else in OnBtnFile after the call to ReadFile?  (If not what is the CStringArray for?)
Avatar of MEGADETH
MEGADETH

ASKER

Below is the complete function.

If I hardcode the file path using the commented lines, it works.

It is worth mention, how this function is called. From the main application, user open an X dialog. Then on X dialog, I call Y dialog. On this Y dialog, the OnBtnFile (browse button located).



void CTransactionListFilterDlg::OnBtnFile()
{
     TCHAR szFilePathName[_MAX_PATH+1];
     memset(szFilePathName, 0, _MAX_PATH+1);

     
     CFileDialog dlg(TRUE);
     dlg.m_ofn.lpstrFile = &szFilePathName[0];
     dlg.m_ofn.nMaxFile = _MAX_PATH;
     dlg.m_ofn.lpstrTitle = _T("Add from file");
     dlg.m_ofn.Flags =  dlg.m_ofn.Flags | OFN_ENABLESIZING |OFN_LONGNAMES | OFN_NODEREFERENCELINKS | OFN_PATHMUSTEXIST | OFN_SHAREAWARE;



     int dm = dlg.DoModal();
     if(dm != IDOK)
          return;


     ASSERT(::IsWindow(m_hWnd));
 

//     CString szFilePathName = _T("");
//     szFilePathName += "C:\\Documents and Settings\\megadeth\\Desktop\\test.txt";

     CStringArray arsTransID;
     if(!ReadFile(szFilePathName, arsTransID))
          return;


     for(int i = 0; i < arsTransID.GetSize(); i++)
     {
          CString strTransID;
          strTransID = (LPCTSTR) arsTransID.GetAt(i);
          strTransID.TrimLeft();
          strTransID.TrimRight();
          if(strTransID.GetLength() > 0)
          {
               if(m_lstTrans.FindStringExact(0, (LPCTSTR) strTransID) == LB_ERR)
                    m_lstTrans.AddString((LPCTSTR) strTransID);
          }
         
          strTransID.Empty();
     }    

     
}
I found the problem. Something is not right with the window handle. I use Win API "GetOpenFileName", and specify the handle. It works.

Thanks aphilips for responding.
ASKER CERTIFIED SOLUTION
Avatar of SpideyMod
SpideyMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> Thanks aphilips for responding.

No worries.  Glad you worked out the problem.