Print Page | Close Window

Closing skinned app crashes in AfxGetAmbientActCtx

Printed From: Codejock Forums
Category: Codejock Products
Forum Name: Toolkit Pro
Forum Description: Topics Related to Codejock Toolkit Pro
URL: http://forum.codejock.com/forum_posts.asp?TID=13554
Printed Date: 23 December 2024 at 10:38am
Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com


Topic: Closing skinned app crashes in AfxGetAmbientActCtx
Posted By: cjsnewbie
Subject: Closing skinned app crashes in AfxGetAmbientActCtx
Date Posted: 02 March 2009 at 3:48pm
Hi,

I'm using Toolkit v12.1.1 with skinning like this:

In constructor - XTPSkinManager()->LoadSkin(m_csStylesPath + _T("Vista.cjstyles") , _T("NORMALBLACK_INI"));   

Here is what I'm doing. I press one button on my main app dialog which opens new dialog window. This new dialog window class is derived from CDHtmlDialog MFC class. I close new dialog window by pressing 'X' (close button). Then, I close my main app. That is when I get this error:




Call Stack:

mfc80ud.dll!AfxGetAmbientActCtx()  Line 37 + 0x8 bytes    C++
mfc80ud.dll!AFX_MAINTAIN_STATE2::AFX_MAINTAIN_STATE2(AFX_MODULE_STATE * pNewState=0x027b4868)  Line 74 + 0x5 bytes    C++
mfc80ud.dll!CCmdTarget::InternalRelease()  Line 173 + 0x14 bytes    C++
ToolkitPro1211vc80UD.dll!CXTPSkinObjectApplicationFrame::~CXTPSkinObjectApplicationFrame()  Line 94    C++
ToolkitPro1211vc80UD.dll!CXTPSkinObjectApplicationFrame::`vector deleting destructor'()  + 0x6c bytes    C++
ToolkitPro1211vc80UD.dll!CXTPSkinObject::OnFinalRelease()  Line 145 + 0x35 bytes    C++
mfc80ud.dll!CCmdTarget::InternalRelease()  Line 175    C++
ToolkitPro1211vc80UD.dll!CXTPSkinManager::RemoveAll(int bUnattach=1)  Line 640    C++
ToolkitPro1211vc80UD.dll!CXTPSkinManager::~CXTPSkinManager()  Line 387    C++
ToolkitPro1211vc80UD.dll!CXTPSkinManager::`vector deleting destructor'()  + 0x6c bytes    C++
ToolkitPro1211vc80UD.dll!CXTPSkinManager::CDestructor::~CDestructor()  Line 610 + 0x36 bytes    C++
ToolkitPro1211vc80UD.dll!`dynamic atexit destructor for '__xtpSkinManagerDestructor''()  + 0x28 bytes    C++
ToolkitPro1211vc80UD.dll!_CRT_INIT(void * hDllHandle=0x00450000, unsigned long dwReason=0, void * lpreserved=0x00000001)  Line 420    C
ToolkitPro1211vc80UD.dll!__DllMainCRTStartup(void * hDllHandle=0x00450000, unsigned long dwReason=0, void * lpreserved=0x00000001)  Line 512 + 0x11 bytes    C
ToolkitPro1211vc80UD.dll!_DllMainCRTStartup(void * hDllHandle=0x00450000, unsigned long dwReason=0, void * lpreserved=0x00000001)  Line 462 + 0x11 bytes    C
ntdll.dll!7c90118a()    
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]   
ntdll.dll!7c923aba()    
ntdll.dll!7c90eb79()    
ntdll.dll!7c910415()    
ntdll.dll!7c91041e()    
ntdll.dll!7c923c68()    
kernel32.dll!7c81ca96()    
ToolkitPro1211vc80UD.dll!CXTPSkinManagerApiHook::OnHookGetModuleHandleA(const char * lpModuleName=0x00000002)  Line 1492 + 0x10 bytes    C++
kernel32.dll!7c81cb0e()    
msvcr80d.dll!__crtExitProcess(int status=2)  Line 684    C
msvcr80d.dll!doexit(int code=2, int quick=0, int retcaller=0)  Line 596 + 0x9 bytes    C
msvcr80d.dll!exit(int code=2)  Line 398 + 0xd bytes    C
MainApp.exe!__tmainCRTStartup()  Line 610    C
MainApp.exe!wWinMainCRTStartup()  Line 414    C
kernel32.dll!7c817067()    


I tried,

XTPSkinManager()->LoadSkin(NULL, NULL);
XTPSkinManager()->RemoveAll();   

in main dialog's destructor. Didn't work.

Any suggestion?

Thanks.



Replies:
Posted By: Oleg
Date Posted: 03 March 2009 at 12:42am
Hi,
Send it to mailto:support@codejock.com - support@codejock.com with sample that can help reproduce it.


-------------
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS


Posted By: cjsnewbie
Date Posted: 06 March 2009 at 8:42pm
Originally posted by oleg oleg wrote:

Hi,
Send it to mailto:support@codejock.com - support@codejock.com with sample that can help reproduce it.


Done.


Posted By: znakeeye
Date Posted: 07 March 2009 at 4:54am
Please post the answer here when you've found the problem!
I've had similar crashes (in 12.1.1) at the very same line (AfxGetAmbientActCtx).


-------------
PokerMemento - http://www.pokermemento.com/


Posted By: cjsnewbie
Date Posted: 11 March 2009 at 12:58pm
Any update on this from support staff?

Originally posted by znakeeye znakeeye wrote:

Please post the answer here when you've found the problem!
I've had similar crashes (in 12.1.1) at the very same line (AfxGetAmbientActCtx).


Will do.


Posted By: znakeeye
Date Posted: 11 March 2009 at 5:03pm
Btw, check out this thread: http://forum.codejock.com/forum_posts.asp?TID=12581 - http://forum.codejock.com/forum_posts.asp?TID=12581

-------------
PokerMemento - http://www.pokermemento.com/


Posted By: Oleg
Date Posted: 12 March 2009 at 1:56am
Hi,
 
I can't find issue number. What number was assigned ?


-------------
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS


Posted By: cjsnewbie
Date Posted: 12 March 2009 at 12:50pm
Originally posted by oleg oleg wrote:

Hi,
 
I can't find issue number. What number was assigned ?


Issue number? Is it something I should get in return for sending my sample project? I haven't received anything at all.

Just for information, I added link to this thread in subject line of email.


Posted By: cjsnewbie
Date Posted: 23 March 2009 at 5:59pm
Originally posted by oleg oleg wrote:

Hi,
 
I can't find issue number. What number was assigned ?


Any update? It's been quite a while and I haven't heard anything back.


Posted By: Allover
Date Posted: 08 July 2009 at 11:15am
Hi, we are using CodeJock Toolkit 13.1 and VS2008. Starting from after 12.0.1 we constantly receive an unhandled exception at AfxGetAmbientActCtx (called from CXTPSkinManager::RemoveAll) upon the application exit. Note that Toolkit 12.0.1 worked fine for us. The problem arises if we create a Wizard property sheet which resides in an external DLL (plug-in system for our application). Digging the Toolkit source we found that any dialog/property sheet and all their child windows will be hooked by CXTPSkinManager through CXTPSkinManager::HookWndProc. Each window will be associated with CXTPSkinObject. However, if a property sheet is created from another module (DLL) the hook function doesn't really work. (NOTE: we did not test this statement for dialogs/property sheets resided and created within the main EXE.) When the property sheet is closed all allocated CXTPSkinObject are not erased from the CXTPSkinManager map.
There are 2 solutions for this:
1. Call XTPSkinManager()->SetAutoApplyNewWindows(FALSE);
2. Each property sheet is derived from a class which keeps track over all child windows (one needs enumerating all windows within each property page including the property sheet itself). Upon exit from the property sheet (close, cancel etc.) one must remove ALL tracked windows manually from the SkinManager calling XTPSkinManager()->Remove(hWnd). This seems to solve the problem correctly for all our external property sheets.

Is there any better solution for this matter?

Best regards,
Peter



-------------
Peter


Posted By: Insad
Date Posted: 05 August 2009 at 11:34am
Hi,
 
We too still have this problem.
 
In our situation we have an EXE with the skinning enabled, has some windows etc. It calls a DLL which creates a dialog (which indeed shows the correct skin).
 
The code inside the DLL does not creates the skinmanager, but the dialog is derived from CXTResizeDialog.
 
When the dialog processing is done it is closed and the DLL ends. -- No problems until here
 
Then the EXE is closed (it has a dialog, derived from CXTResizeDialog, still showing, pressing the OK or Cancel on it ends the application)
-- Now the unhandled exception at AfxGetAmbientActCtx  occours.
 
The options mentioned by Allover are not working / are not desirable.
 
Same question from me too: Is there a solution or a quick fix?
 
Best regards,
 
Albert van Peppen


Posted By: znakeeye
Date Posted: 07 August 2009 at 3:47am

Try cleaning up the skin manager in ExitInstance:

CYourApp::ExitInstance()
{
    XTPSkinManager()->LoadSkin(NULL, NULL);
    XTPSkinManager()->RemoveAll();


-------------
PokerMemento - http://www.pokermemento.com/


Posted By: Allover
Date Posted: 07 August 2009 at 4:55am
The crash happens in CXTPSkinManager::RemoveAll(). It doesn't matter if you call XTPSkinManager()->RemoveAll() in ExitInstance or later. The problem can be described as follows.
1. You have a skinned application.
2. You have a dialog/wizard/property sheet created in a DLL (plug-in etc.).
3. For every dialog item a CXTPSkinObject will be allocated and added to the CXTPSkinManager map.
4. When the dialog is closed all allocated CXTPSkinObject are not erased from the CXTPSkinManager map and stay there alive with not valid CWnd pointers.
5. At any point after p.4 you call CXTPSkinManager::RemoveAll() the AfxGetAmbientActCtx will be thrown because  the CXTPSkinManager tries to delete all allocated CXTPSkinObjects. Since the dialog is closed all controls do not exist anymore. However the corresponding CXTPSkinObjects are still alive. It seems that they must be destroyed just before the dialog and all its controls will be destroyed (HWNDs or CWnds should be still valid). It looks like this is a Windows hook callback problem or something like that which works fine with dialogs created within main EXE but the hook function pointer is lost in the calling chain (which seems to handle operations such as creation and destruction of windows) if a dialog created in a DLL file and is not properly called when you close such a dialog.
 
NOTE: our DLL files are plug-ins and loaded and unloaded by LoadLibrary, FreeLibrary. We don't know what happens with dialogs created in DLLs which are explicitly linked to the application.


-------------
Peter


Posted By: Insad
Date Posted: 07 August 2009 at 5:34am
Hi,
 
I assume you mean in the ExitInstance of the EXE.
 
The LoadSkin(NULL,NULL); is already in place and is the function that causes the crash.
Added the RemoveAll() will not add any value since the application is already crashed before reaching it
 
I was wondering:
Is there possibly something that needs to be done in the unload of the DLL or in the destroy of the dialog created in the DLL?
 
The crash does not occour when I don't call the dialog in the DLL: I do all initialisation in my DLL code but never create the Dialog and, as you would understand, don't call it :)
 
 


Posted By: znakeeye
Date Posted: 08 August 2009 at 8:21am

Wild guessing again... but please see this thread: http://forum.codejock.com/forum_posts.asp?TID=14933 - http://forum.codejock.com/forum_posts.asp?TID=14933

What if your application receives WM_QUIT somehow and crash in a similar way as mine? Use Spy++ and look for it! (Not when debugging as this might hang your computer thoroughly!)

-------------
PokerMemento - http://www.pokermemento.com/


Posted By: Insad
Date Posted: 12 August 2009 at 4:31am
Nope, no WM_QUIT message.
 
A simple testcase which fails:
 
Create a simple dialog based app, with skinning.
In the InitInstance skinning is enabled, a skin is loaded, a dialog is created, the DoModal() is done and the InitInstance returns FALSE.
In the ExitInstance i call the LoadSkin(NULL,NULL); --> This crashes!
Note that i check to see if skinning is Enabled before calling the LoadSkin(NULL,NULL);
 
The EXE-dialog has a button, and when pressed in loads a DLL (LoadLibraryEx/GetProcAddress/etc.) and calls a function in the DLL which creates a new dialog which just let you press OK or Cancel.
Then the Dialog in the DLL is destructed and the DLL terminates and is unloaded by the calling EXE.
When back in the EXE-dialog, you can press Cancel to exit the application.
 
I've tried several different skin-files; all fail. When providing NO skinfile, the InitInstance calls LoadSkin(NULL,NULL), and the application does not crash (hence: I am not calling the LoadSkin(NULL,NULL) when skinning is not enabled ; Using the function IsEnabled() in the skinmanager)
 


-------------
Products in use: XTP version 13.4.2/15.3.1/16.4.0/17.3.0
Platforms: Win2003(R2)/Win2008(R2)/Win7/Win2011/Win8(.1)/Win2012(R2)/Win10 (x86/x64)
Langs: VC++ (MFC) 6/2005/2008/2013/2015/2017


Posted By: Insad
Date Posted: 01 February 2010 at 5:13am
Hi,
 
Sorry for bumping this post but it is still a major issue for us.
Currently we deliver our products with skinning disabled, but one of our key points where suppose to be the skinning possibilities.
 
The issue still happens in TKP 13.2.1.
The mentioned testcase is also still showing the issue.
Can/do I need to provide you with more information?
 
Thanks in advance.
 


-------------
Products in use: XTP version 13.4.2/15.3.1/16.4.0/17.3.0
Platforms: Win2003(R2)/Win2008(R2)/Win7/Win2011/Win8(.1)/Win2012(R2)/Win10 (x86/x64)
Langs: VC++ (MFC) 6/2005/2008/2013/2015/2017


Posted By: Smucker
Date Posted: 01 February 2010 at 11:08am
I have seen several activation context problems and crashes caused by DLLs injected into the process (Logitech's SetPoint for example). I believe they no longer occur in my application because I statically link to Codejock and MFC.

See https://forum.codejock.com/forum_posts.asp?TID=13394&KW=activation+context - this thread for another example.

Another possible workardound: if you're not running on Windows 9x/ME (where GDI and USER stuff leak systemwide), and have closed or flushed all your streams and database connections, using TerminateProcess() will result in a clean and quick application shutdown.



-------------
Product: Xtreme Toolkit Pro version 13.2 (Unicode, static build)

Platform: Windows 200x/XP/Vista/Win7 (32/64 bit)

Language: Visual C++ 9.0 (Studio 2008)



Posted By: mgampi
Date Posted: 09 February 2016 at 1:03am
Hello;

Has anyone every solved this problem?
I'm using toolkit 15.3.1 and now I'm facing the same problem.
In my case, static linking is no valid option.

TIA

Martin


-------------
Martin

Product: Xtreme Toolkit v 22.1.0, new Projects v 24.0.0
Platform: Windows 10 v 22H2 (64bit)
Language: VC++ 2022


Posted By: mgampi
Date Posted: 17 March 2017 at 7:44am
Still an issue!!Cry
Eventually fixed in 17.3?


-------------
Martin

Product: Xtreme Toolkit v 22.1.0, new Projects v 24.0.0
Platform: Windows 10 v 22H2 (64bit)
Language: VC++ 2022


Posted By: astoyan
Date Posted: 19 March 2017 at 10:34pm
The initial issue should be fixed in 17.3 though you still need to make sure you call LoadSkin(NULL, NULL) prior to application exit. In 18.0 you won't need to do it.

The issue with WM_QUIT hasn't been reported for a while, I assume it's been fixed after 15.3.1 but cannot be sure.

If you still can reproduce anything of that in the latest version please provide details or a sample, we'll try to fix it in 18.0.

Regards,
  Alexander


Posted By: mgampi
Date Posted: 29 March 2017 at 12:11pm
Hi;

Just tested 17.3.0 and I can still get the error! Cry

It's all time reproducable!

Take a skinned application
Within the app call a exported C function into a regular DLL that opens a CXTPResizeDialog derived non modal dialog box.
The XTPResizeDialog derived class contains a CXTPToolbar:
... InitCommandBars();

  CXTPCommandBars* CommandBars = GetCommandBars();
  Toolbar_.SetCommandBars(CommandBars);
  VERIFY(Toolbar_.CreateToolBar(WS_TABSTOP|WS_VISIBLE|WS_CHILD|CBRS_TOOLTIPS, this));
  VERIFY(Toolbar_.LoadToolBar(IDR_TOOLBAR));
  Toolbar_.ShowExpandButton(FALSE);
  Toolbar_.SetFlags(xtpFlagAlignTop|xtpFlagStretched);

  CommandBars->EnableCustomization(FALSE);
    CommandBars->GetCommandBarsOptions()->ShowKeyboardCues(xtpKeyboardCuesShowWindowsDefault);
  CommandBars->GetToolTipContext()->SetStyle(xtpToolTipResource); ...


Whenever the app is closed after the dialog is shown I get the crash with this call stack:

>    mfc90ud.dll!AfxGetAmbientActCtx()  Zeile 37 + 0xa Bytes    C++
     mfc90ud.dll!AFX_MAINTAIN_STATE2::AFX_MAINTAIN_STATE2(AFX_MODULE_STATE * pNewState=0x10c7a028)  Zeile 86 + 0x5 Bytes    C++
     mfc90ud.dll!CCmdTarget::InternalRelease()  Zeile 173 + 0x14 Bytes    C++
     ToolkitPro1730vc90UD.dll!CXTPResourceImages::RemoveAll()  Zeile 688 + 0xe Bytes    C++
     ToolkitPro1730vc90UD.dll!CXTPResourceTheme::~CXTPResourceTheme()  Zeile 154    C++
     ToolkitPro1730vc90UD.dll!CXTPRibbonTheme::~CXTPRibbonTheme()  Zeile 155 + 0x8 Bytes    C++
     ToolkitPro1730vc90UD.dll!CXTPRibbonTheme::`vector deleting destructor'()  + 0x57 Bytes    C++
     mfc90ud.dll!CCmdTarget::OnFinalRelease()  Zeile 579 + 0x21 Bytes    C++
     mfc90ud.dll!CCmdTarget::InternalRelease()  Zeile 175    C++
     ToolkitPro1730vc90UD.dll!CXTPCommandBars::~CXTPCommandBars()  Zeile 266 + 0x14 Bytes    C++
     ToolkitPro1730vc90UD.dll!CXTPCommandBars::`vector deleting destructor'()  + 0x57 Bytes    C++
     mfc90ud.dll!CCmdTarget::OnFinalRelease()  Zeile 579 + 0x21 Bytes    C++
     mfc90ud.dll!CCmdTarget::InternalRelease()  Zeile 175    C++
     ToolkitPro1730vc90UD.dll!CXTPCommandBarsSiteBase<CFrameWnd>::~CXTPCommandBarsSiteBase<CFrameWnd>()  Zeile 54 + 0x1a Bytes    C++
     ToolkitPro1730vc90UD.dll!CXTPFrameWndBase<CFrameWnd>::~CXTPFrameWndBase<CFrameWnd>()  Zeile 235 + 0x8 Bytes    C++
     ToolkitPro1730vc90UD.dll!CXTPFrameWnd::~CXTPFrameWnd()  + 0x16 Bytes    C++
     XREngine.exe!CMainFrame::~CMainFrame()  Zeile 218 + 0x122 Bytes    C++
     XREngine.exe!CMainFrame::`scalar deleting destructor'()  + 0x2b Bytes    C++
     mfc90ud.dll!CView::PostNcDestroy()  Zeile 121 + 0x21 Bytes    C++
     mfc90ud.dll!CWnd::OnNcDestroy()  Zeile 864    C++
     mfc90ud.dll!CWnd::OnWndMsg(unsigned int message=130, unsigned int wParam=0, long lParam=0, long * pResult=0x0018edb8)  Zeile 2042    C++
     ToolkitPro1730vc90UD.dll!CXTPCommandBarsSiteBase<CFrameWnd>::OnWndMsg(unsigned int message=130, unsigned int wParam=0, long lParam=0, long * pResult=0x0018edb8)  Zeile 199 + 0x18 Bytes    C++
     mfc90ud.dll!CWnd::WindowProc(unsigned int message=130, unsigned int wParam=0, long lParam=0)  Zeile 1755 + 0x20 Bytes    C++
     mfc90ud.dll!AfxCallWndProc(CWnd * pWnd=0x05feb1f8, HWND__ * hWnd=0x00131748, unsigned int nMsg=130, unsigned int wParam=0, long lParam=0)  Zeile 240 + 0x1c Bytes    C++
     mfc90ud.dll!AfxWndProc(HWND__ * hWnd=0x00131748, unsigned int nMsg=130, unsigned int wParam=0, long lParam=0)  Zeile 403    C++
     mfc90ud.dll!AfxWndProcBase(HWND__ * hWnd=0x00131748, unsigned int nMsg=130, unsigned int wParam=0, long lParam=0)  Zeile 441 + 0x15 Bytes    C++
     user32.dll!75958e71()    
     [Unten angegebene Rahmen sind möglicherweise nicht korrekt und/oder fehlen, keine Symbole geladen für user32.dll]   
     user32.dll!759590d1()    
     user32.dll!75958fce()    
     user32.dll!75964d95()    
     ToolkitPro1730vc90UD.dll!CXTPSkinManagerApiHook::OnHookCallWindowProcW(long (HWND__ *, unsigned int, unsigned int, long)* lpPrevWndFunc=0x785f1ab0, HWND__ * hWnd=0x00131748, unsigned int Msg=130, unsigned int wParam=0, long lParam=0)  Zeile 1399 + 0x1e Bytes    C++
     ToolkitPro1730vc90UD.dll!CXTPHookManager::HookWndProc(HWND__ * hWnd=0x00131748, unsigned int message=130, unsigned int wParam=0, long lParam=0)  Zeile 431 + 0x1c Bytes    C++
     user32.dll!75958e71()    
     user32.dll!7595ae9d()    
     user32.dll!75958fce()    
     user32.dll!7595932c()    
     user32.dll!7595d821()    
     ntdll.dll!776d0596()    
     mfc90ud.dll!CWnd::DestroyWindow()  Zeile 1007 + 0xd Bytes    C++
     DWrite.dll!6c006100()    

When I do not create the toolbar (remove the lines of code above) the app closes without any crash.

Any ideas or a workaround? Now it is really urgent, because release date is near and I have no other possibility to make huge design changes!


-------------
Martin

Product: Xtreme Toolkit v 22.1.0, new Projects v 24.0.0
Platform: Windows 10 v 22H2 (64bit)
Language: VC++ 2022


Posted By: Insad
Date Posted: 30 March 2017 at 5:03am
Hi,

We still have the problem when we enable the possibility for the user to select his own theme/style.

Our work around is that we have selected a fixes theme/style and have disabled the functionality that the user can switch his theme/style.

Not as good as we hoped for, but in small, simple sample applications we can't get it to reproduce.

Possibly it has something to do with the fact that we use multi threading and somewhere a handle is used from the wrong thread or something?
We did have that problem with MAPI as well but here we managed to solve this ;)

We test to see the problem is resolved in each release from TKP but so far no luck.

But we have high hopes for v18.0, if it ever gets released..



-------------
Products in use: XTP version 13.4.2/15.3.1/16.4.0/17.3.0
Platforms: Win2003(R2)/Win2008(R2)/Win7/Win2011/Win8(.1)/Win2012(R2)/Win10 (x86/x64)
Langs: VC++ (MFC) 6/2005/2008/2013/2015/2017



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.04 - http://www.webwizforums.com
Copyright ©2001-2021 Web Wiz Ltd. - https://www.webwiz.net