How to let the VS2013 call stack show more functions - c++

In my MFC program, I need to parse a 1.2GB file.During debugging, every time the program near the completion of parsing,it will has an exception as follows:
First-chance exception at 0x522C31CA (msvcr120d.dll) in LogDataTrans.exe: 0xC0000005: Access violation writing location 0x0003FA0C.
Here is the last function in call stack:msvcr120d.dll!memcpy(unsigned char * dst, unsigned char * src, unsigned long count) Line 188 Unknown.But other functions in the call stack are defined in mfc120d and user32.dll.I have changed the code from memcpy(&_lrs.body[_lrs.lenBody], buf, l); to
try{
memcpy(&_lrs.body[_lrs.lenBody], buf, l);
}
catch (...){
ofstream f("memcpy.error");
f << "memcpy.error";
}
But the same problem occurred.Also,this memcpy function has been used many times before the exception occurs.
I can't find elsewhere my own code is wrong.So, I want to let the call stack show more functions to let me trace back to my own functions,not only functions provided by the system.The current call stack is this:
msvcr120d.dll!memcpy(unsigned char * dst, unsigned char * src, unsigned long count) Line 188 Unknown
LogDataTrans.exe!AfxWinMain(struct HINSTANCE__ *,struct HINSTANCE__ *,char *,int) C++
mfc120d.dll!_AfxDispatchCmdMsg(CCmdTarget * pTarget, unsigned int nID, int nCode, void (void) * pfn, void * pExtra, unsigned int nSig, AFX_CMDHANDLERINFO * pHandlerInfo) Line 77 C++
mfc120d.dll!CCmdTarget::OnCmdMsg(unsigned int nID, int nCode, void * pExtra, AFX_CMDHANDLERINFO * pHandlerInfo) Line 373 C++
mfc120d.dll!CDialog::OnCmdMsg(unsigned int nID, int nCode, void * pExtra, AFX_CMDHANDLERINFO * pHandlerInfo) Line 85 C++
mfc120d.dll!CWnd::OnCommand(unsigned int wParam, long lParam) Line 2784 C++
mfc120d.dll!CDialogEx::OnCommand(unsigned int wParam, long lParam) Line 290 C++
mfc120d.dll!CWnd::OnWndMsg(unsigned int message, unsigned int wParam, long lParam, long * pResult) Line 2108 C++
mfc120d.dll!CWnd::WindowProc(unsigned int message, unsigned int wParam, long lParam) Line 2094 C++
mfc120d.dll!AfxCallWndProc(CWnd * pWnd, HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Line 282 C++
mfc120d.dll!AfxWndProc(HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Line 435 C++
mfc120d.dll!AfxWndProcBase(HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Line 299 C++
user32.dll!_InternalCallWinProc#20() Unknown
user32.dll!_UserCallWinProcCheckWow#32() Unknown
user32.dll!_SendMessageWorker#24() Unknown
user32.dll!_SendMessageW#16() Unknown
user32.dll!_xxxButtonNotifyParent#12() Unknown
user32.dll!_xxxBNReleaseCapture#12() Unknown
user32.dll!_ButtonWndProcWorker#24() Unknown
user32.dll!_ButtonWndProcA#16() Unknown
user32.dll!_InternalCallWinProc#20() Unknown
user32.dll!_UserCallWinProcCheckWow#32() Unknown
user32.dll!_DispatchMessageWorker#8() Unknown
user32.dll!_DispatchMessageW#4() Unknown
user32.dll!_IsDialogMessageW#8() Unknown
user32.dll!_IsDialogMessageA#8() Unknown
mfc120d.dll!CWnd::IsDialogMessageA(tagMSG * lpMsg) Line 193 C++
mfc120d.dll!CWnd::PreTranslateInput(tagMSG * lpMsg) Line 4590 C++
mfc120d.dll!CDialog::PreTranslateMessage(tagMSG * pMsg) Line 80 C++
mfc120d.dll!CDialogEx::PreTranslateMessage(tagMSG * pMsg) Line 275 C++
mfc120d.dll!CWnd::WalkPreTranslateTree(HWND__ * hWndStop, tagMSG * pMsg) Line 3363 C++
mfc120d.dll!AfxInternalPreTranslateMessage(tagMSG * pMsg) Line 233 C++
mfc120d.dll!CWinThread::PreTranslateMessage(tagMSG * pMsg) Line 777 C++
mfc120d.dll!AfxPreTranslateMessage(tagMSG * pMsg) Line 252 C++
mfc120d.dll!AfxInternalPumpMessage() Line 178 C++
mfc120d.dll!CWinThread::PumpMessage() Line 900 C++
mfc120d.dll!AfxPumpMessage() Line 190 C++
mfc120d.dll!CWnd::RunModalLoop(unsigned long dwFlags) Line 4644 C++
mfc120d.dll!CWnd::CreateRunDlgIndirect(const DLGTEMPLATE * lpDialogTemplate, CWnd * pParentWnd, HINSTANCE__ * hInst) Line 470 C++
mfc120d.dll!CDialog::DoModal() Line 633 C++
LogDataTrans.exe!CLogDataTransApp::InitInstance() Line 75 C++
[External Code]
LogDataTrans.exe!WinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, char * lpCmdLine, int nCmdShow) Line 26 C++
All these functions are not defined by me.

Related

MFC Ribbon quick access toolbar crash when state is changed

I have an old MFC application I am updating and I just added a ribbon (using the CMFCRibbonBar class).
However any commands in the quick access toolbar on the ribbon that cause a change in state (eg closing the file and exiting to the main menu) cause a crash when the command is used from the quick access toolbar but not the normal ribbon button.
The crash happens in the MFC file afxribbonbar.cpp in OnMouseMove()
if (pHit != m_pHighlighted)
{
PopTooltip();
if (m_pHighlighted != NULL)
{
ASSERT_VALID(m_pHighlighted);
m_pHighlighted->m_bIsHighlighted = FALSE;
m_pHighlighted->OnHighlight(FALSE);
InvalidateRect(m_pHighlighted->GetRect());
m_pHighlighted = NULL;
}
It crashes on the InvalidateRect line, because m_pHighlighted seems to exist but have no data, so either the stack is corrupted or m_pHighlighted doesn't exist anymore.
I have some lines to reload where all the frames were last time the user was in the new state and I found if I comment out the lines when I change state the crash doesn't happen:
theApp.SaveState(this, ToolbarKey(i_oldAppState));
and
theApp.LoadState( this, ToolbarKey(i_newAppState) );
But ideally I want these lines as otherwise all the windows and frames revert to their default state and users have to resize everything again.
It feels like the entries in the quick access toolbar are being destroyed and recreated when I change state, but I can't find where MFC does this, or a way to set the highlighted entry to null.
EDIT: The crash is an unhandled exception due to an access reading violation in mfc140u.dll. The crash happens deep in the MFC code, well away from anything I have written.
The call stack when this unhandled exception occurs is:
mfc140u.dll!CBasePane::get_accValue(tagVARIANT varChild, wchar_t * * pszValue) Line 1494 C++
mfc140u.dll!CMFCRibbonBar::OnMouseMove(unsigned int nFlags, CPoint point) Line 2348 C++
mfc140u.dll!CMFCRibbonBar::OnLButtonUp(unsigned int nFlags, CPoint point) Line 2276 C++
mfc140u.dll!CWnd::OnWndMsg(unsigned int message, unsigned __int64 wParam, __int64 lParam, __int64 * pResult) Line 2698 C++
mfc140u.dll!CWnd::WindowProc(unsigned int message, unsigned __int64 wParam, __int64 lParam) Line 2099 C++
mfc140u.dll!CBasePane::WindowProc(unsigned int message, unsigned __int64 wParam, __int64 lParam) Line 1020 C++
mfc140u.dll!CMFCRibbonBar::WindowProc(unsigned int message, unsigned __int64 wParam, __int64 lParam) Line 4884 C++
mfc140u.dll!AfxCallWndProc(CWnd * pWnd, HWND__ * hWnd, unsigned int nMsg, unsigned __int64 wParam, __int64 lParam) Line 265 C++
mfc140u.dll!AfxWndProc(HWND__ * hWnd, unsigned int nMsg, unsigned __int64 wParam, __int64 lParam) Line 417 C++
mfc140u.dll!AfxWndProcBase(HWND__ * hWnd, unsigned int nMsg, unsigned __int64 wParam, __int64 lParam) Line 299 C++
user32.dll!UserCallWinProcCheckWow() Unknown
user32.dll!DispatchMessageWorker() Unknown
user32.dll!IsDialogMessageW() Unknown
mfc140u.dll!CWnd::IsDialogMessageW(tagMSG * lpMsg) Line 194 C++
[Inline Frame] mfc140u.dll!CWnd::PreTranslateInput(tagMSG *) Line 4606 C++
mfc140u.dll!CBasePane::PreTranslateMessage(tagMSG * pMsg) Line 1057 C++
mfc140u.dll!CMFCRibbonBar::PreTranslateMessage(tagMSG * pMsg) Line 3734 C++
mfc140u.dll!CWnd::WalkPreTranslateTree(HWND__ * hWndStop, tagMSG * pMsg) Line 3379 C++
mfc140u.dll!AfxInternalPreTranslateMessage(tagMSG * pMsg) Line 233 C++
mfc140u.dll!AfxInternalPumpMessage() Line 178 C++
mfc140u.dll!CWinThread::Run() Line 629 C++
I had the same bug in the BCG Library. I Made my private fix there for the variables m_pHighlighted and m_pPressed.
BOOL CBCGPRibbonBar::LoadState (LPCTSTR lpszProfileName, int nIndex, UINT uiID)
{
// 2014-04-23 mri: we need to clear pointer to may be active elements that
// may get deleted, when LoadState is executed.
// We cannot set m_pActiveCategory to NULL!!!! This get us into problems when the
// program starts and the minimize/maximize button is used, when this pointer is
// NULL.
m_pHighlighted = NULL;
m_pPressed = NULL;
You should set them to nullptr before you call LoadState
As I remember the problem ist that a click in the ribbon is handled in the Mouse-Down message. So the Mouse-Up message is never executed that clears this member...

Resolving an exception with LoadToolbarEx and difference between count of normal v large images

I just tried running my CDialog app in debug and I get an exception. It relates to my creation of myu toolbar.
I call this mention in OnInitDialog:
void CMeetingScheduleAssistantDlg::CreateToolbar()
{
DWORD dwCtrlStyle = TBSTYLE_FLAT | TBSTYLE_TOOLTIPS | CBRS_SIZE_DYNAMIC;
DWORD dwStyle = AFX_DEFAULT_TOOLBAR_STYLE;
if (m_ToolBar.CreateEx(this, dwCtrlStyle,
dwStyle, CRect(1, 1, 1, 1), IDR_TOOLBAR))
{
dwStyle = CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC;
m_ToolBar.SetPaneStyle(m_ToolBar.GetPaneStyle() | dwStyle);
CMFCToolBarInfo info;
info.m_uiColdResID = IDB_PNG_MAIN_TOOLBAR;
info.m_uiHotResID = IDB_PNG_MAIN_TOOLBAR;
info.m_uiLargeColdResID = IDB_PNG_MAIN_TOOLBAR;
info.m_uiLargeHotResID = IDB_PNG_MAIN_TOOLBAR;
m_ToolBar.LoadToolBarEx(IDR_TOOLBAR, info, FALSE);
CSize sizeToolBar = m_ToolBar.CalcFixedLayout(TRUE, TRUE);
m_ToolBar.SetWindowPos(NULL, 0, 0, sizeToolBar.cx, sizeToolBar.cy,
SWP_NOACTIVATE | SWP_NOZORDER);
// Move all controls down
CPoint ptOffset(0, sizeToolBar.cy);
CRect rcChild;
CWnd* pwndChild = GetWindow(GW_CHILD);
while (pwndChild)
{
if (pwndChild->GetSafeHwnd() != m_ToolBar.GetSafeHwnd())
{
pwndChild->GetWindowRect(rcChild);
ScreenToClient(rcChild);
rcChild.OffsetRect(ptOffset);
pwndChild->MoveWindow(rcChild, FALSE);
}
pwndChild = pwndChild->GetNextWindow();
}
// Resize the window
CRect rcWindow;
GetWindowRect(rcWindow);
rcWindow.bottom += sizeToolBar.cy;
MoveWindow(rcWindow, FALSE);
}
}
The exception is on this line:
LoadToolBarEx
When I trace the code I end up at this ASSERT:
// Load large images:
if (params.m_uiLargeHotResID != 0)
{
if (!m_LargeImages.Load(params.m_uiLargeHotResID, NULL, TRUE))
{
return FALSE;
}
ASSERT(m_Images.GetCount() == m_LargeImages.GetCount());
}
When I view these variables:
m_Images has 10 images.
m_LargeImages has 5 images.
This is the call stack:
> Meeting Schedule Assistant.exe!CMFCToolBar::LoadBitmapEx(CMFCToolBarInfo & params, int bLocked) Line 781 C++
Meeting Schedule Assistant.exe!CMFCToolBar::LoadToolBarEx(unsigned int uiToolbarResID, CMFCToolBarInfo & params, int bLocked) Line 872 C++
Meeting Schedule Assistant.exe!CMeetingScheduleAssistantDlg::CreateToolbar() Line 2288 C++
Meeting Schedule Assistant.exe!CMeetingScheduleAssistantDlg::OnInitDialog() Line 246 C++
Meeting Schedule Assistant.exe!AfxDlgProc(HWND__ * hWnd, unsigned int message, unsigned __int64 __formal, __int64 __formal) Line 28 C++
[External Code]
Meeting Schedule Assistant.exe!CWnd::DefWindowProcW(unsigned int nMsg, unsigned __int64 wParam, __int64 lParam) Line 1100 C++
Meeting Schedule Assistant.exe!CWnd::Default() Line 299 C++
Meeting Schedule Assistant.exe!CDialog::HandleInitDialog(unsigned __int64 __formal, __int64 __formal) Line 721 C++
Meeting Schedule Assistant.exe!CWnd::OnWndMsg(unsigned int message, unsigned __int64 wParam, __int64 lParam, __int64 * pResult) Line 2441 C++
Meeting Schedule Assistant.exe!CWnd::WindowProc(unsigned int message, unsigned __int64 wParam, __int64 lParam) Line 2099 C++
Meeting Schedule Assistant.exe!AfxCallWndProc(CWnd * pWnd, HWND__ * hWnd, unsigned int nMsg, unsigned __int64 wParam, __int64 lParam) Line 265 C++
Meeting Schedule Assistant.exe!AfxWndProc(HWND__ * hWnd, unsigned int nMsg, unsigned __int64 wParam, __int64 lParam) Line 418 C++
[External Code]
Meeting Schedule Assistant.exe!IsolationAwareCreateDialogIndirectParamW(HINSTANCE__ * hInstance, const DLGTEMPLATE * lpTemplate, HWND__ * hWndParent, __int64(*)(HWND__ *, unsigned int, unsigned __int64, __int64) lpDialogFunc, __int64 dwInitParam) Line 569 C++
Meeting Schedule Assistant.exe!CWnd::CreateDlgIndirect(const DLGTEMPLATE * lpDialogTemplate, CWnd * pParentWnd, HINSTANCE__ * hInst) Line 358 C++
Meeting Schedule Assistant.exe!CWnd::CreateRunDlgIndirect(const DLGTEMPLATE * lpDialogTemplate, CWnd * pParentWnd, HINSTANCE__ * hInst) Line 460 C++
Meeting Schedule Assistant.exe!CDialog::DoModal() Line 633 C++
Meeting Schedule Assistant.exe!CMeetingScheduleAssistantApp::InitInstance() Line 251 C++
Meeting Schedule Assistant.exe!AfxWinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, wchar_t * lpCmdLine, int nCmdShow) Line 37 C++
Meeting Schedule Assistant.exe!wWinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, wchar_t * lpCmdLine, int nCmdShow) Line 26 C++
[External Code]
My main PNG toolbar resource has 10 images. Yet I specified this code:
info.m_uiColdResID = IDB_PNG_MAIN_TOOLBAR;
info.m_uiHotResID = IDB_PNG_MAIN_TOOLBAR;
info.m_uiLargeColdResID = IDB_PNG_MAIN_TOOLBAR;
info.m_uiLargeHotResID = IDB_PNG_MAIN_TOOLBAR;
My toolbar image is 320 x 32.
How do I deal with this exception?
I think I have come across the answer here.
CMFCToolBar::m_dblLargeImageRatio
Specifies the ratio between the dimension (height or width) of large
images and the dimension of regular images.
Remarks The default ratio is 2. You can change this value to make
large toolbar images larger or smaller.
The framework uses this data member when you do not specify a set of
large images. For example, if you provide only the set of small images
with size 16x16 and want the large images to have the size 24x24, set
this data member to 1.5.
CMFCToolBar::m_dblLargeImageRatio = 1.0;

Why mfc deadlock when calling a afxmessagebox from propertypage into a extension dll

I have a mfc Application an a extension dll with a modal Dialog. In this Dialog is a PropertySheet. One of the Property-Pages is loaded from a extnsion dll. Into the Extension DLL is an other property sheet. If I want to open a AfxMessageBox into the poperty sheet of the dll the application hang.
I think there is an deadlock.
The OS is Windows 7.
IDE Visual-Studio 2012
Here are some lines of the CallStack.
> mfc110d.dll!_AfxTraceMsg(const char * lpszPrefix, const tagMSG * pMsg) Zeile 302 C++
mfc110d.dll!AfxCallWndProc(CWnd * pWnd, HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Zeile 275 C++
mfc110d.dll!AfxWndProc(HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Zeile 453 C++
mfc110d.dll!AfxWndProcBase(HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Zeile 304 C++
user32.dll!76a362fa() Unnamed
[The frames below are possible corrupted or not here. No symbols loaded foruser32.dll]
user32.dll!76a36d3a() Unknown
user32.dll!76a36ce9() Unknown
user32.dll!76a3966e() Unknown
user32.dll!76a396d5() Unknown
user32.dll!76a6104b() Unknown
user32.dll!76a60d62() Unknown
user32.dll!76a61089() Unknown
ntdll.dll!76f91171() Unknown
user32.dll!76a362fa() Unknown
ntdll.dll!76f91171() Unknown
ntdll.dll!76f91171() Unknown
ntdll.dll!76f91171() Unknown
ntdll.dll!76f91171() Unknown
user32.dll!76a36ce9() Unknown
user32.dll!76a40d37() Unknown
ntdll.dll!76f91171() Unknown
ntdll.dll!76f91171() Unknown
ntdll.dll!76f91171() Unknown
mfc110d.dll!CWnd::DefWindowProcA(unsigned int nMsg, unsigned int wParam, long lParam) Line 1141 C++
mfc110d.dll!CWnd::Default() Line 323 C++
mfc110d.dll!CWnd::OnActivate(unsigned int __formal, CWnd * __formal, int __formal) Line 368 C++
mfc110d.dll!CMFCPropertyPage::OnActivate(unsigned int nState, CWnd * pWndOther, int bMinimized) Line 73 C++
mfc110d.dll!CWnd::OnWndMsg(unsigned int message, unsigned int wParam, long lParam, long * pResult) Line 2540 C++
mfc110d.dll!CWnd::WindowProc(unsigned int message, unsigned int wParam, long lParam) Line 2137 C++
mfc110d.dll!AfxCallWndProc(CWnd * pWnd, HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Line 290 C++
mfc110d.dll!AfxWndProc(HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Line 453 C++
mfc110d.dll!AfxWndProcBase(HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Line 304 C++
user32.dll!76a362fa() Unknown
user32.dll!76a36d3a() Unknown
user32.dll!76a36ce9() Unknown
comctl32.dll!71f771cb() Unknown
comctl32.dll!71f33770() Unknown
mfc110d.dll!CMapPtrToPtr::HashKey(void * key) Zeile 42 C++
mfc110d.dll!CMapPtrToPtr::GetValueAt(void * key) Zeile 180 C++
0058ca3c() Unbekannt
user32.dll!76a362fa() Unknown
user32.dll!76a5f963() Unknown
user32.dll!76a5f91b() Unknown
user32.dll!76a5f7a4() Unknown
ntdll.dll!76f91171() Unknown
user32.dll!76a362fa() Unknown
ntdll.dll!76f91171() Unknown
ntdll.dll!76f91171() Unknown
ntdll.dll!76f91171() Unknown
ntdll.dll!76f91171() Unknown
user32.dll!76a36ce9() Unknown
user32.dll!76a40d37() Unknown
ntdll.dll!76f91171() Unknown
ntdll.dll!76f91171() Unknown
ntdll.dll!76f91171() Unknown
mfc110d.dll!CWnd::DefWindowProcA(unsigned int nMsg, unsigned int wParam, long lParam) Zeile 1141 C++
mfc110d.dll!CWnd::WindowProc(unsigned int message, unsigned int wParam, long lParam) Zeile 2138 C++
mfc110d.dll!AfxCallWndProc(CWnd * pWnd, HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Zeile 290 C++
mfc110d.dll!AfxWndProc(HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Zeile 453 C++
mfc110d.dll!AfxWndProcBase(HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Zeile 304 C++
user32.dll!76a362fa() Unknown
user32.dll!76a36d3a() Unknown
user32.dll!76a36ce9() Unknown
user32.dll!76a36ded() Unknown
user32.dll!76a36e4c() Unknown
ntdll.dll!76f3011a() Unknown
user32.dll!76a4219a() Unknown
user32.dll!76a8ef0a() Unknown
user32.dll!76a362fa() Unknown
user32.dll!76a5f9ff() Unknown
user32.dll!76a5f91b() Unknown
user32.dll!76a5f7a4() Unknown
user32.dll!76a4afbc() Unknown
user32.dll!76a362fa() Unknown
user32.dll!76a36d3a() Unknown
user32.dll!76a36ce9() Unknown
user32.dll!76a40d37() Unknown
user32.dll!76a4795a() Unknown
mfc110d.dll!_AfxActivationWndProc(HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Zeile 489 C++
user32.dll!76a362fa() Unknown
user32.dll!76a36d3a() Unknown
user32.dll!76a36ce9() Unknown
user32.dll!76a3966e() Unknown
user32.dll!76a6208f() Unknown
user32.dll!76a5cf5b() Unknown
user32.dll!76a8f808() Unknown
user32.dll!76a8fae4() Unknown
user32.dll!76a8fbe7() Unknown
user32.dll!76a8fc66() Unknown
user32.dll!76a8fdb9() Unknown
user32.dll!76a8fdfe() Unknown
mfc110d.dll!CWinApp::ShowAppMessageBox(CWinApp * pApp, const char * lpszPrompt, unsigned int nType, unsigned int nIDPrompt) Zeile 128 C++
mfc110d.dll!CWinApp::DoMessageBox(const char * lpszPrompt, unsigned int nType, unsigned int nIDPrompt) Zeile 45 C++
mfc110d.dll!AfxMessageBox(const char * lpszText, unsigned int nType, unsigned int nIDHelp) Zeile 147 C++
Here is some Code from the DLL. CPropStkList is derived from CPropertyPage.
void CPropStkList::OnBnClicked()
{
//BOOL test = TryEnterCriticalSection(AfxGetThread());
AfxMessageBox(_T("Hallo Test!!"));
//CDialog dlg(IDD_DIALOG2);
//dlg.DoModal();
}
And here is the Code of the Parent-Application where calls the DLL-Dialog.
typedef UINT ( * LPDLLFUNC)(CGlobal *, CMsgSocket *); //Initialize DLL
typedef CProSeSDialog * ( * GETDLG)(CWnd*, UINT, CString); //Open Dialog from DLL
LPDLLFUNC lpfnDllFunc = NULL;
GETDLG getDlg = NULL;
HINSTANCE hDLL = NULL;
hDLL = AfxLoadLibrary("Stammdaten_DLL.dll");
if(hDLL)
{
lpfnDllFunc = (LPDLLFUNC)::GetProcAddress(hDLL,"Init");
getDlg = (GETDLG)::GetProcAddress(hDLL, "ShowDlg");
if (!lpfnDllFunc)
{
AfxMessageBox("Function not found in DLL");
FreeLibrary(hDLL);
//return;
}
if(!getDlg)
{
AfxMessageBox("Function getDlg not found in DLL");
FreeLibrary(hDLL);
}
lpfnDllFunc(AfxGetGlobal(), AfxGetGlobalSocket()); //Initialize DLL
dlg[i=GetNewDlgNr()] = getDlg(this, 1, _T(""));
m_strText[i] = INTLTXT ("Werkzeugstammdaten") + " DLL";
}
else
{
AfxMessageBox("Dll not found!");
}
Creation of the Dialog into the DLL
extern "C" AFX_EXT_API CProSeSDialog* ShowDlg(CWnd *parent, UINT dlgType = 0, CString strKey = _T(""))
{
new CDynLinkLibrary(Stammdaten_DLLDLL);
//Lege Einstiegsdaten fest
CGlobalKeyBuffer &buffer = CGlobalKeyBuffer::Instance();
buffer.m_strKey = strKey;
CViewDlg *test = new CViewDlg(WKZSTAMM);
test->Create(IDD_DIALOG1, parent);
return test;
}
UPDATE
After hours of debugging the Application i founnd that the Applicaion Sends some Messages. It never ends with sending messages.
Here are the sended Messages.
<025111> 001F1316 S WM_GETDLGCODE
<025112> 001F1316 R WM_GETDLGCODE fuDlgCode:DLGC_WANTARROWS | DLGC_HASSETSEL | DLGC_WANTCHARS
<025113> 001F1314 S WM_GETDLGCODE
<025114> 001F1314 R WM_GETDLGCODE fuDlgCode:DLGC_WANTARROWS | DLGC_HASSETSEL | DLGC_WANTCHARS
<025115> 00201312 S WM_GETDLGCODE
<025116> 00201312 R WM_GETDLGCODE fuDlgCode:DLGC_WANTARROWS | DLGC_WANTCHARS
<025117> 001E130C S WM_GETDLGCODE
<025118> 001E130C R WM_GETDLGCODE fuDlgCode:DLGC_WANTARROWS | DLGC_WANTCHARS
<025119> 001F1308 S WM_GETDLGCODE
<025120> 001F1308 R WM_GETDLGCODE fuDlgCode:DLGC_STATIC
<025121> 001F1306 S WM_GETDLGCODE
<025122> 001F1306 R WM_GETDLGCODE fuDlgCode:DLGC_STATIC
<025123> 001E1304 S WM_GETDLGCODE
<025124> 001E1304 R WM_GETDLGCODE fuDlgCode:DLGC_STATIC
<025125> 001F1302 S WM_GETDLGCODE
<025126> 001F1302 R WM_GETDLGCODE fuDlgCode:DLGC_STATIC
<025127> 00211300 S WM_GETDLGCODE
<025128> 00211300 R WM_GETDLGCODE fuDlgCode:DLGC_STATIC
<025129> 001E12FE S WM_GETDLGCODE
<025130> 001E12FE R WM_GETDLGCODE fuDlgCode:DLGC_STATIC
<025131> 001E12FC S WM_GETDLGCODE
I found a solution fo my problem.
You have to add to every element (control) that has any children the extern style
WS_EX_CONTROLPARENT.
ModifyStyleEx(0, WS_EX_CONTROLPARENT);
If that doesn't work you have to many subchildren. More than three is very difficult in MFC.

CString Format() unable to read memory VS2015

I'm keep receiving the message Error reading characters of string Unable to read memory. I'm not sure how to correct it as it seems like everything is in order when the CString.Format() function is called.
Here is the spot in strnlen.cpp where the error occurs:
And here are the Locals at the time of the break:
This is the function that is calling strnlen.cpp (the last line is where the break occurs):
char comp[50];
gethostname(comp,50);
CString textmsg;
textmsg.Format("%s %s: %s",TEXT,comp, m_edit_message);
m_edit_message is a CString variable.
Lastly here is the call stack at the time of the error:
NetChess.exe!common_strnlen_c<unsigned char>(const unsigned char * const string, const unsigned int maximum_count) Line 36 C++
NetChess.exe!common_strnlen_simd<0,unsigned char>(const unsigned char * const string, const unsigned int maximum_count) Line 94 C++
NetChess.exe!common_strnlen<unsigned char>(const unsigned char * const string, const unsigned int maximum_count) Line 153 C++
NetChess.exe!strnlen(const char * string, unsigned int maximum_count) Line 165 C++
NetChess.exe!__crt_stdio_output::output_processor<char,__crt_stdio_output::string_output_adapter<char>,__crt_stdio_output::standard_base<char,__crt_stdio_output::string_output_adapter<char> > >::type_case_s_compute_narrow_string_length(const int maximum_length, char __formal) Line 2268 C++
NetChess.exe!__crt_stdio_output::output_processor<char,__crt_stdio_output::string_output_adapter<char>,__crt_stdio_output::standard_base<char,__crt_stdio_output::string_output_adapter<char> > >::type_case_s() Line 2255 C++
NetChess.exe!__crt_stdio_output::output_processor<char,__crt_stdio_output::string_output_adapter<char>,__crt_stdio_output::standard_base<char,__crt_stdio_output::string_output_adapter<char> > >::state_case_type() Line 1999 C++
NetChess.exe!__crt_stdio_output::output_processor<char,__crt_stdio_output::string_output_adapter<char>,__crt_stdio_output::standard_base<char,__crt_stdio_output::string_output_adapter<char> > >::process() Line 1644 C++
NetChess.exe!common_vsprintf<__crt_stdio_output::standard_base,char>(const unsigned __int64 options, char * const buffer, const unsigned int buffer_count, const char * const format, __crt_locale_pointers * const locale, char * const arglist) Line 163 C++
NetChess.exe!__stdio_common_vsprintf(unsigned __int64 options, char * buffer, unsigned int buffer_count, const char * format, __crt_locale_pointers * locale, char * arglist) Line 235 C++
NetChess.exe!_vscprintf_l(const char * const _Format, __crt_locale_pointers * const _Locale, char * _ArgList) Line 1655 C++
NetChess.exe!_vscprintf(const char * const _Format, char * _ArgList) Line 1672 C++
[External Code]
NetChess.exe!CMessageSend::OnOK() Line 60 C++
NetChess.exe!_AfxDispatchCmdMsg(CCmdTarget * pTarget, unsigned int nID, int nCode, void(CCmdTarget::*)() pfn, void * pExtra, unsigned int nSig, AFX_CMDHANDLERINFO * pHandlerInfo) Line 77 C++
NetChess.exe!CCmdTarget::OnCmdMsg(unsigned int nID, int nCode, void * pExtra, AFX_CMDHANDLERINFO * pHandlerInfo) Line 372 C++
NetChess.exe!CDialog::OnCmdMsg(unsigned int nID, int nCode, void * pExtra, AFX_CMDHANDLERINFO * pHandlerInfo) Line 85 C++
NetChess.exe!CWnd::OnCommand(unsigned int wParam, long lParam) Line 2779 C++
NetChess.exe!CWnd::OnWndMsg(unsigned int message, unsigned int wParam, long lParam, long * pResult) Line 2092 C++
NetChess.exe!CWnd::WindowProc(unsigned int message, unsigned int wParam, long lParam) Line 2078 C++
NetChess.exe!AfxCallWndProc(CWnd * pWnd, HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Line 265 C++
NetChess.exe!AfxWndProc(HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Line 418 C++
[External Code]
NetChess.exe!CWnd::IsDialogMessageA(tagMSG * lpMsg) Line 193 C++
NetChess.exe!CWnd::PreTranslateInput(tagMSG * lpMsg) Line 4586 C++
NetChess.exe!CDialog::PreTranslateMessage(tagMSG * pMsg) Line 80 C++
NetChess.exe!CWnd::WalkPreTranslateTree(HWND__ * hWndStop, tagMSG * pMsg) Line 3358 C++
NetChess.exe!AfxInternalPreTranslateMessage(tagMSG * pMsg) Line 233 C++
NetChess.exe!CWinThread::PreTranslateMessage(tagMSG * pMsg) Line 777 C++
NetChess.exe!AfxPreTranslateMessage(tagMSG * pMsg) Line 252 C++
NetChess.exe!AfxInternalPumpMessage() Line 178 C++
NetChess.exe!CWinThread::PumpMessage() Line 900 C++
NetChess.exe!CWinThread::Run() Line 629 C++
NetChess.exe!CWinApp::Run() Line 787 C++
NetChess.exe!AfxWinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, char * lpCmdLine, int nCmdShow) Line 47 C++
NetChess.exe!WinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, char * lpCmdLine, int nCmdShow) Line 26 C++
[External Code]
MessageSend.cpp
// MessageSend.cpp : implementation file
//
#include "stdafx.h"
#include "NetChess.h"
#include "MessageSend.h"
#include "NetChessDoc.h"
#include "NetChessView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////////////////////////////////////////////// CMessageSend dialog
bool BoundsCheck2(char *myTestArray, unsigned int expectedSize);
CMessageSend::CMessageSend(CWnd* pParent)
: CDialog(CMessageSend::IDD, pParent)
{
//{{AFX_DATA_INIT(CMessageSend)
m_edit_message = _T("");
m_edit_receive_message = _T("");
//}}AFX_DATA_INIT
}
void CMessageSend::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMessageSend)
DDX_Text(pDX, IDC_EDIT_MESSAGE, m_edit_message);
DDV_MaxChars(pDX, m_edit_message, 50000);
DDX_Text(pDX, IDC_EDIT_RECEIVE_MESSAGE, m_edit_receive_message);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMessageSend, CDialog)
//{{AFX_MSG_MAP(CMessageSend)
ON_WM_VSCROLL()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//////////////////////////////////////CMessageSend message handlers
void CMessageSend::OnOK()
{
//msgDlg.m_edit_send_message
UpdateData(TRUE);
char comp[50];
if (BoundsCheck2(comp, 50))
gethostname(comp,50);
CString textmsg;
textmsg.Format("%s %s: %s",TEXT,comp, (CString) m_edit_message);
m_edit_receive_message += (CString)comp + ": " + m_edit_message + (CString)"\r\n";;
((CNetChessView*)((CFrameWnd*)(AfxGetApp()->m_pMainWnd))->GetActiveView())->SendSockData((unsigned char*)textmsg.GetBuffer(0),textmsg.GetLength());
//textmsg.ReleaseBuffer(0);
m_edit_message = "";
UpdateData(FALSE);
CWnd* wnd= GetDlgItem(IDC_EDIT_RECEIVE_MESSAGE);
wnd->PostMessage(WM_VSCROLL,SB_BOTTOM,0);
//CDialog::OnOK();
}
void CMessageSend::SetReceiveData(char* data)
{
m_edit_receive_message += (data + (CString)"\r\n");
UpdateData(FALSE);
CWnd* wnd= GetDlgItem(IDC_EDIT_RECEIVE_MESSAGE);
wnd->PostMessage(WM_VSCROLL,SB_BOTTOM,0);
}
void CMessageSend::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}
bool BoundsCheck2(char *myTestArray, unsigned int expectedSize)
{
//Reference: Reference: http://lelanthran.com/deranged/?p=182
bool status = true;
unsigned int count = 0;
//perform bounds checkes on data1; just to be safe
for (size_t i = 0; i < sizeof(myTestArray) / sizeof(myTestArray[0]); i++) {
count++;
}
if (count <= expectedSize)
status = true;
else
status = false;
return status;
}
MessageSend.h
#if !defined(AFX_MESSAGESEND_H__08C3FB4D_9E1E_4AF9_951F_7ED1033E3B16__INCLUDED_)
#define AFX_MESSAGESEND_H__08C3FB4D_9E1E_4AF9_951F_7ED1033E3B16__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MessageSend.h : header file
//
///////////////////////////////////////////CMessageSend dialog
class CMessageSend : public CDialog
{
// Construction
public:
CMessageSend(CWnd* pParent = NULL);
void SetReceiveData(char* data);
// standard constructor
// Dialog Data
//{{AFX_DATA(CMessageSend)
enum { IDD = IDD_DIALOG_MESSAGE };
CString m_edit_message;
CString m_edit_receive_message;
//}}AFX_DATA
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
virtual void OnOK();
afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
DECLARE_MESSAGE_MAP()
};
#endif // !defined(AFX_MESSAGESEND_H__08C3FB4D_9E1E_4AF9_951F_7ED1033E3B16__INCLUDED_)
Any help on this is much appreciated.
One problem you have in your code, this code:
bool BoundsCheck2(char *myTestArray, unsigned int expectedSize)
// ...
for (size_t i = 0; i < sizeof(myTestArray) / sizeof(myTestArray[0]); i++) {
count++;
}
is not doing what you think, sizeof(myTestArray) / sizeof(myTestArray[0] is equal to 8 - always (well on 32bit is would be 4 - size of pointer), this is because when you call a function with array argument it decays to pointer to its first element and the size of array is lost.
Its also hard to tell what this function is supposed to do, is it checking whether compiler created 50 element array? You better zero initialize it before use.
Another thing: check if you are compiling with UNICODE enabled, if so then CString is of wchar_t character type.
This code:
CString textmsg;
textmsg.Format("%s %s: %s",TEXT,comp, (CString) m_edit_message);
I would rewrite as:
CString textmsg;
textmsg.Format("%s %s: %s",TEXT.GetString(), comp, m_edit_message.GetString());
assuming TEXT is of type CString - I suppose it is bound to some widget?
also:
char comp[50];
as:
char comp[256] = {0};
why 256, read here: https://msdn.microsoft.com/pl-pl/library/windows/desktop/ms738527(v=vs.85).aspx
last thing, always check result codes for Win API functions, otherwise you might use returned data in undetermined state.

MFC program crashes when window resized with error "A required resource was not found."

I'm developing an SDI MFC application where there are some custom buttons which are drawn in a custom way or have images loaded on top of them. When the running application is re-sized continuously for about 5 to 10 minutes, it crashes with the error "A required resource was not found."
I have checked the code thoroughly and all the GetDC() calls are followed by ReleaseDC() calls. Furthermore, I always save the old GDI object (for example, an oldBrush), whenever I do a DC.SelectObject(&newBrush) call and then restore the old pen with DC.SelectObject(&oldBrush).
Any hints for what else might be causing this error?
Edit: I used the program Deleaker to find the GDI leaks in the program and deleted those GDI objects which were causing the leak.
Edit: Here's the call stack for AfxThrowResourceException:
mfc110ud.dll!AfxThrowResourceException() Line 1353 C++
mfc110ud.dll!CWindowDC::CWindowDC(CWnd * pWnd) Line 1022 C++
mfc110ud.dll!CMFCToolBarImages::PrepareDrawImage(tagAFXDrawState & ds, CSize sizeImageDest, int bFadeInactive) Line 1219 C++
mfc110ud.dll!CMFCToolBarImages::DrawEx(CDC * pDC, CRect rect, int iImageIndex, CMFCToolBarImages::ImageAlignHorz horzAlign, CMFCToolBarImages::ImageAlignVert vertAlign, CRect rectSrc, unsigned char alphaSrc) Line 1729 C++
mfc110ud.dll!CMFCControlRenderer::FillInterior(CDC * pDC, CRect rect, CMFCToolBarImages::ImageAlignHorz horz, CMFCToolBarImages::ImageAlignVert vert, unsigned int index, unsigned char alphaSrc) Line 470 C++
mfc110ud.dll!CMFCControlRenderer::FillInterior(CDC * pDC, CRect rect, unsigned int index, unsigned char alphaSrc) Line 474 C++
mfc110ud.dll!CMFCControlRenderer::Draw(CDC * pDC, CRect rect, unsigned int index, unsigned char alphaSrc) Line 253 C++
mfc110ud.dll!CMFCVisualManagerOffice2007::DrawNcCaption(CDC * pDC, CRect rectCaption, unsigned long dwStyle, unsigned long dwStyleEx, const ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > & strTitle, const ATL::CStringT<wchar_t,StrTraitMFC_DLL<wchar_t,ATL::ChTraitsCRT<wchar_t> > > & strDocument, HICON__ * hIcon, int bPrefix, int bActive, int bTextCenter, const CObList & lstSysButtons) Line 2097 C++
mfc110ud.dll!CMFCVisualManagerOffice2007::OnNcPaint(CWnd * pWnd, const CObList & lstSysButtons, CRect rectRedraw) Line 2343 C++
mfc110ud.dll!CFrameImpl::OnNcPaint() Line 1564 C++
mfc110ud.dll!CFrameWndEx::OnNcPaint() Line 1030 C++
mfc110ud.dll!CWnd::OnWndMsg(unsigned int message, unsigned int wParam, long lParam, long * pResult) Line 2459 C++
mfc110ud.dll!CWnd::WindowProc(unsigned int message, unsigned int wParam, long lParam) Line 2137 C++
mfc110ud.dll!AfxCallWndProc(CWnd * pWnd, HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Line 290 C++
mfc110ud.dll!AfxWndProc(HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Line 453 C++
mfc110ud.dll!AfxWndProcBase(HWND__ * hWnd, unsigned int nMsg, unsigned int wParam, long lParam) Line 304 C++
I am sure you have a GDI or other Windows resource leak. The message text Comes from an internal exception in the MFC.
What you see is the result of a call to AfxThrowResourceException. Set a break Point on this function in the Debugger and you can see what Operation Fails.
As a result of this acion you know if you have a Memory or GDI leak, or may be other handle leak...