General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 5th, 2004, 07:28 AM
BungoMan85 BungoMan85 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 1 BungoMan85 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Problem with CFileDialog (access violation).

I'm having some nasty problems with the MFC class CFileDialog. I'm simply trying to use it to get the name of a selected file and put it in a text edit box. I have done this many times before and have never had this sort of problem. Here is the code that is producing the error:

Code:
void CFileSplitterDlg::OnButtonOpenSplitFile() 
{
	 CFileDialog FileOpen(TRUE,0,0,OFN_HIDEREADONLY|OFN_OVERWRITEPR  OMPT,0,this);

	 FileOpen.m_ofn.lpstrTitle="Select File to Split";

	 int Result=FileOpen.DoModal();

	 if (Result==IDOK)
	 {
		  SetDlgItemText(IDC_EDIT_SPLIT,FileOpen.GetPathName  ());
		  m_ProgressSplit.SetPos(0);
	 }
	 else
	 {
		  if (Result==IDCANCEL)
		  {
			   SetDlgItemText(IDC_EDIT_SPLIT,"");
		  }
	 }
}


IDC_EDIT_SPLIT is defined, and m_ProgressSplit is a valid member variable. I also looked up on the MSDN anything I could find on CFileDialog and access violations and found something regarding custom CFileDialog classes and access violations, the issue is apparently fixed in windows 2000 (I'm running XP Pro), and is also fixed in VS6SP6 (I was running VS6 with no service pack when I encountered this error, I have since upgraded to SP6, this of course did not help), so I'm pretty sure it's not that I'm missing anything. Originally I was using MFC dlls, I tried switching to static MFC libraries to see if that had any effect, it didn't. I also tried created a brand new dialog based MFC application and made it so the only thing it did was create a CFileDialog when the default 'OK' button was pressed. I got the same error (which leads me to believe it is not something isolated to my project).

I've narrowed it down to the destructor of CFileDialog. When I press the 'Open Split File' (which calls CFileSplitterDlg::OnButtonOpenSplitFile()) button the CFileDialog is created and DoModal() is called (causing the dialog to appear). It then lets me select a file. If I press 'Open' it returns IDOK and the file name gets put into the text edit box. If I select 'Cancel' IDCANCEL is returned and anything inside the text edit box is cleared (as it should be). The error doesn't occur untill the destructor is called. Which of course happens when the CFileDialog goes out of scope as soon as the function. Everything I have before then works correctly (I used message boxes to stop determine the point just before it crashes). I even commented out everything but the constructor and still got an error when the destructor is called.

The error message it says when the program crashes when I run it through the debugger says, "Unhandled exception in File Splitter.exe (KERNEL32.DLL): 0xC0000005: Access Violation."

Here is the call stack the debugger gives me.

Code:
KERNEL32! 77e778ce()
CFileDialog::~CFileDialog() + 71 bytes
CFileSplitterDlg::OnButtonOpenSplitFile() line 198 + 18 bytes
_AfxDispatchCmdMsg(CCmdTarget * 0x0012fd74 {CFileSplitterDlg}, unsigned int 1007, int 0, void (void)* 0x00401078 CFileSplitterDlg::OnButtonOpenSplitFile(void), void * 0x00000000, unsigned int 12, AFX_CMDHANDLERINFO * 0x00000000) line 88
CCmdTarget::OnCmdMsg(unsigned int 1007, int 0, void * 0x00000000, AFX_CMDHANDLERINFO * 0x00000000) line 302 + 39 bytes
CDialog::OnCmdMsg(unsigned int 1007, int 0, void * 0x00000000, AFX_CMDHANDLERINFO * 0x00000000) line 97 + 24 bytes
CWnd::OnCommand(unsigned int 1007, long 2818546) line 2099
CWnd::OnWndMsg(unsigned int 273, unsigned int 1007, long 2818546, long * 0x0012f82c) line 1608 + 28 bytes
CWnd::WindowProc(unsigned int 273, unsigned int 1007, long 2818546) line 1596 + 30 bytes
AfxCallWndProc(CWnd * 0x0012fd74 {CFileSplitterDlg hWnd=???}, HWND__ * 0x00a3027a, unsigned int 273, unsigned int 1007, long 2818546) line 215 + 26 bytes
AfxWndProc(HWND__ * 0x00a3027a, unsigned int 273, unsigned int 1007, long 2818546) line 379
USER32! 77d67b17()
USER32! 77d6cdce()
USER32! 77d45696()
USER32! 77d461f6()
USER32! 77d5ec02()
USER32! 77d5c3f6()
USER32! 77d67b17()
USER32! 77d6cdce()
USER32! 77d44435()
USER32! 77d49611()
USER32! 77d5d704()
CWnd::IsDialogMessageA(tagMSG * 0x005f58dc {msg=0x00000202 wp=0x00000000 lp=0x00060017}) line 182
CWnd::PreTranslateInput(tagMSG * 0x005f58dc {msg=0x00000202 wp=0x00000000 lp=0x00060017}) line 3435
CDialog::PreTranslateMessage(tagMSG * 0x005f58dc {msg=0x00000202 wp=0x00000000 lp=0x00060017}) line 92
CWnd::WalkPreTranslateTree(HWND__ * 0x00a3027a, tagMSG * 0x005f58dc {msg=0x00000202 wp=0x00000000 lp=0x00060017}) line 2678 + 18 bytes
CWinThread::PreTranslateMessage(tagMSG * 0x005f58dc {msg=0x00000202 wp=0x00000000 lp=0x00060017}) line 672 + 18 bytes
CWinThread::PumpMessage() line 848 + 30 bytes
CWnd::RunModalLoop(unsigned long 4) line 3489 + 19 bytes
CDialog::DoModal() line 539 + 12 bytes
CFileSplitterApp::InitInstance() line 52 + 11 bytes
AfxWinMain(HINSTANCE__ * 0x00400000, HINSTANCE__ * 0x00000000, char * 0x00141f3c, int 1) line 39 + 11 bytes
WinMain(HINSTANCE__ * 0x00400000, HINSTANCE__ * 0x00000000, char * 0x00141f3c, int 1) line 30
WinMainCRTStartup() line 198 + 54 bytes
KERNEL32! 77e7eb69()


And here is a disassembly immediately surrounding the code that execution stops on.

Code:
77E778BE   xor		 eax,eax
77E778C0   jmp		 77E77554
77E778C5   mov		 ecx,dword ptr [esp+4]
77E778C9   mov		 eax,0FFFFFFFFh
77E778CE   lock xadd   dword ptr [ecx],eax
77E778D2   dec		 eax
77E778D3   ret		 4


It apparently stops on 77E778CE. It would appear that the pointer in ecx is a bad pointer (NULL or outside the program's memory space). I'm not 100% sure what lock xadd does, I googlged for it and it appears to have to do with threads and making it so another thread can't mess upo a counter of somekind. I'm wondering why it doesnt crash on 77E778C5... or is all that happens there is the value pointed to on the stack pointer+4 being put into ecx and that is used as a pointer? I'm not good with pointers in assembly (I don't know the syntax well enough) so I don't know if I really know what I'm talking about.

Either way it seems like it ends up with an invalid pointer. My biggest question is why? I have not done anything out of the ordinary. I followed all the examples I've found that use CFileDialog (even the one on MSDN) exactly as they were presently (sans a few minor changes that shouldn't cause an error like this). I don't understand why it used to work and now it doesn't (in any new or old project).

Any help on this would be grealty appreciated. I'm at a total loss here. This far exceeds my ability...

Reply With Quote
  #2  
Old July 30th, 2004, 04:20 PM
slim slim is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 1 slim User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
This is very likely to be caused by a bug in MFC related to CFileDialog. Please read the following article, and hope that will fix it. (you need to download service pack 6 for VC)
FIX: Opening a CFileDialog from Handler of a Custom CFileDialog Causes Access Violation

http://support.microsoft.com/default.aspx?scid=kb;EN-US;256329



S.

[QUOTE=BungoMan85]I'm having some nasty problems with the MFC class CFileDialog. I'm simply trying to use it to get the name of a selected file and put it in a text edit box. I have done this many times before and have never had this sort of problem. Here is the code that is producing the error:

[code]
....

Reply With Quote
  #3  
Old June 24th, 2009, 12:02 PM
cyberstealth1k cyberstealth1k is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 1 cyberstealth1k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 10 sec
Reputation Power: 0
Lightbulb FIX: Problem with CFileDialog (access violation)

I've searched for this problem and had problems finding a solution, but I think I've stumbled across a fix.

From what I've gathered:
  1. This happens using MFC's CFileDialog or Win32 API GetOpenFileName/GetSaveFileName
  2. occurs on Windows XP SP2 (unknown on Windows 2000, Vista, Windows 7?)
  3. Occurs with Visual Studio 6, 2003, 2005, and possibly newer
  4. happens the 2nd (or greater) time that you open the Open/Save dialog; ONLY(?) on the DESKTOP when you hover your mouse over a file and get a tooltip
  5. Crashes in Microsoft's apps too. Try it in notepad! (File>Open>(file on desktop)>OK...File>Open>(hover over file on desktop) *crash*
  6. Therefore, this is a WINDOWS bug, not a bug with your code.

Whenever your application is initialized OnInitDialog() or similar...

Make a call to CoInitializeEx(NULL, COINIT_MULTITHREADED);

and at the end of your program (OnClose() or similar), [FONT]CoUninitialize()[/FONT]

Example:
Code:
BOOL MyDialog::OnInitDialog()
{
     CDialog::OnInitDialog();
     CoInitializeEx(NULL, COINIT_MULTITHREADED);

     //insert all of your other code here...
}


void MyDialog::OnClose()
{
     CoUninitialize();
     CDialog::OnClose();
}

void MyDialog::OnBnClickedButton1()
{
     CFileDialog dlg(true);
     if(dlg.DoModal == IDOK)
       {
          AfxMessageBox(dlg.GetFileName());
       }
}


Found in various spots... (sorry, it won't let me submit clickable URLs [yes, I've tried the URL tag...])

http://www.codeproject.com/KB/dialog/XFolderDialog.aspx?msg=2582508#xx2582508xx

http://support.microsoft.com/kb/287087

Keywords: MFC CFileDialog GetOpenFileName GetSaveFileName Access Violation 0xC00000005 Crash Desktop Hover Mouse ToolTip

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Problem with CFileDialog (access violation).


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
Stay green...Green IT