How to verify the bar state info

Verify the bar state info in the application profile before calling LoadBarState()

<!-- Article Starts -->

The Problem

If you use CFrameWnd's LoadBarState()/SaveBarState() to remember the positions of the control bars, you may run into this while developing your app - if you change the bar IDs, or simply remove a bar (it is no longer created in CMainFrame::OnCreate()), your app will crash.

Why? Because in the LoadBarState() internals, there is a portion of code which retrieves the bar ID, calls CMainFrame::GetControlBar() and if the pointer returned is NULL (there is no controlbar created with this ID) it will assert. In Release builds, the ASSERT line is not compiled, so it goes further and tries to update the m_nMRUWidth member of the bar, causing a protection error. Nasty.

Well, since you are a MFC veteran, you already know how to fix this:

Open the registry, and delete the whole key storing your app's profile.

Or, open your favorite text editor, and remove the profile section from the .ini file.

Or, simpler, comment the LoadBarState() call in CMainFrame::OnCreate(), run the app (allowing the SaveBarState() to update the profile), then uncomment it back.

What about this? - You already shipped to billions of customers the CoolApp 1.0 with 2 toolbars. Now it's time for an upgrade - you will deliver CoolApp 2.0, which has only one big toolbar, or you changed the toolbar IDs. The new version will crash on a billion computers because of old profile settings, and you get a billion calls for tech support. Uh-oh!

But I have this super install package which verifies the existing profile or builds a brand new one, you'll say. Congratulations, and keep up the good work! This article is not for you, but you may read it anyway.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Comments and Discussions

Thank you very much for your article, I've just had to remove a toolbar from my companies product and ran into this problem straight away - this article help me to get it sorted

Dylan

"In meetings, the person who is least competent usually does the most talking. Talking is a direct substitute for competence, at least in the minds of other people. Five minutes after you leave a meeting, you won't remember what anyone said but you will remember who did most of the talking. Withing a day your mind will translate that into a notion that the talker was unusually knowledgeable" - Scott Adams, Dilbert and the way of the weasel

I am writting an application with a large number of Toolbars, most of which remain hidden until the user asks for them.

Problem 1.If I Dock them all, there is a Bug in MFC which prevents me from Docking the visible toolbars next to each other.

Problem 2.What I have done to overcome problem 1 means that the verify code doesn't work, so if I add or remove another toolbar the application dumps, (unless you start messing around with the registry settings).

You will have no problems if all Bars are using either AddControlBar (for non docking bars) or DockControlBar (for docking bars).Using this enshures that you can add and remove Bars. Only changing the ID's will make problems.

The only problem is that you don't have a direct access to the frame pointer, so either pass it somehow to your class, or use (CFrameWnd*) AfxGetMainWnd() - not too elegant.You need the frame for GetControlBar().Anyway, the code is basically the same :