Click here to Skip to main content
15,920,383 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everybody,


I am having a very strange problem with one of my modalless dialog and it crashes.
Actually what i have done is created a modelless dialog as follows:

C++
CDlgVbRecAvail* pDlg = CDlgVbRecAvail::GetDlgInstance();

	if( pDlg->m_hWnd != NULL )
	{
		pDlg->ShowWindow(SW_RESTORE);
		pDlg->SetFocus();
	}
	else
	{
		pDlg->CDialog::Create(CDlgVbRecAvail::IDD, this);
	}
	pDlg->ShowWindow(SW_SHOW);


where CDlgVbRecAvail::GetDlgInstance() is defined as follows:

C++
CDlgVbRecAvail* CDlgVbRecAvail::GetDlgInstance()
{
	if( m_pDlgInst == NULL )
		m_pDlgInst = new CDlgVbRecAvail();
	else
		return m_pDlgInst;
	return m_pDlgInst;
}


and m_pDlgInst is declared in the header file as follows:

C++
static CDlgVbRecAvail* m_pDlgInst;


Now i have one CDialog method OnCancel and is as follows:

C++
void CDlgVbRecAvail::OnCancel()
{
	DestroyWindow();
}


and one Method overriding PostNcDestroy and is as follows

C++
void CDlgVbRecAvail::PostNcDestroy()
{
	delete this;
}


Now the problem arises when I close the dialog it crashes after the call to DestroyWindow in OnCancel method. PostNcDestroy is not being called.

it show the following dialog

http://www.imghack.se/126142[^]

and then breaks in to this class dbgheap.c in the code at line no. 2103

C++
extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer(
        const void * pUserData
        )
{
#ifndef WINHEAP
        int i;
        void * base;
#else  /* WINHEAP */
#endif  /* WINHEAP */

        if (!pUserData)
            return FALSE;

        if (!_CrtIsValidPointer(pHdr(pUserData), sizeof(_CrtMemBlockHeader), FALSE))
            return FALSE;


#ifdef WINHEAP

#ifndef _WIN64
        if ( __active_heap == __V6_HEAP )
        {
            PHEADER     pHeader;
            if (pHeader = __sbh_find_block(pHdr(pUserData)))
            {
                return __sbh_verify_block(pHeader, pHdr(pUserData));
            }
            else
                return HeapValidate( _crtheap, 0, pHdr(pUserData) );
        }
#ifdef CRTDLL
        else if ( __active_heap == __V5_HEAP )
        {
            __old_sbh_region_t * preg;
            __old_sbh_page_t *   ppage;
            __old_page_map_t *   pmap;
            if ( (pmap = __old_sbh_find_block( pHdr(pUserData), &preg, &ppage )) !=
                 NULL )
            {
                if ( *pmap )
                    return TRUE;
                else
                    return FALSE;
            }
            else
                return HeapValidate( _crtheap, 0, pHdr(pUserData) );
        }
#endif  /* CRTDLL */
        else    //  __active_heap == _SYSTEM_HEAP
#endif  /* _WIN64 */
        {
            return HeapValidate( _crtheap, 0, pHdr(pUserData) );
        }

#else  /* WINHEAP */

        /*
         * Go through the heap regions and see if the pointer lies within one
         * of the regions of the local heap.
         *
         * Pointers from non-local heaps cannot be handled. For example, a
         * non-local pointer may come from a DLL that has the CRT linked-in.
         *
         */

        for (i = 0; (base = _heap_regions[i]._regbase) != NULL &&
                i < _HEAP_REGIONMAX; i++)
        {
            if (pUserData >= base && pUserData <
                    (void *)(((char *)base)+_heap_regions[i]._currsize))
                return TRUE;
        }

        return FALSE;

#endif  /* WINHEAP */
}


Please help me in getting rid of this crash...
Thank you all.
Posted
Updated 26-Oct-13 5:49am
v3

1 solution

Actually my window was derived from CScrollView and its object was created on stack instead of heap.
so the modalless dialog which was having this scroll View, once closed with DestroyWindow was also trying to destroy this window.

So to Avoid this i simply Override the method OnPostNcDestroy and leave it blank.

C++
void OnPostNcDestroy
{

}


this prevent the dialog to destroy the scroll view.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900