I am writing some tests for someone else's code. I saw a problem with part of the code so I wanted to write a test that would demonstrate the problem. But my testing isn't giving the results that I expected.
This code has a modeless dialog that is created with a 'new' + 'Create', and then when the dialog is dismissed, it calls 'delete this' on itself. But the code author calls the 'new' on a member variable of the mainframe, and doesn't make sure that the modeless dialog is not already showing before calling 'new' on it again. So if the function that shows the dialog is called while the dialog is already showing, then 'new' is performed on a pointer that already points to valid CDialog memory.
It would essentially be the same as doing this: CMyDialog* pMyDlg = new CMyDialog; pMyDlg->Create(IDD_MY_DIALOG, this); pMyDlg->ShowWindow(SW_SHOWNORMAL); pMyDlg = new CMyDialog; pMyDlg->Create(IDD_MY_DIALOG, this); pMyDlg->ShowWindow(SW_SHOWNORMAL); pMyDlg = new CMyDialog; pMyDlg->Create(IDD_MY_DIALOG, this); pMyDlg->ShowWindow(SW_SHOWNORMAL);
In my testing I tried to create the dialog several times in a row, without dismissing any of the dialogs, then dismiss them and create them in an intermixed order, waiting for it to break.
But it never broke. I only ever see one dialog window on the screen, even though the 'new', 'Create' and 'ShowWindow' are called multiple times in a row.
Is this what you would expect? And how would you test this to prove that it's written poorly?
Kind of weird. The repeated new calls will cause memory leaks, i.e memory which has been allocated will become inaccessible, and hence impossible to be freed. This doesn't need any testing.
Repeated Create( ) calls... well I've never done it, and don't know what the behavior will be by default, but come to think of it, I've never seen any app which shows multiple versions of the same modal dialog as well, so I don't know what we should expect here.
If you want to test before calling new whether the object is already allocated or not, the simplest way I can think of is nulling the pointer in the ctor, and checking if it's null or not right before any allocation attempts.
Good luck, Ehsan
_______________________________________________ msvc mailing list [email protected] See http://beginthread.com/mailman/listinfo/msvc_beginthread.com for subscription changes, and list archive.
