Stack around the variable 'pages' was corrupted.

The program compiles fine, however after I open file it crashes.
1
2
FileOpen(hSheet, true, -1); // this is OK
SwitchPages(hApp, hSheet, &scen); // here it crashes 


It crashes on return from function SwitchPages.
The error:
Run-Time Check Failure #2 - Stack around the variable 'pages' was corrupted.

makewindows.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef AOKTS_DIALOGWIN_H
#define AOKTS_DIALOGWIN_H

#include "../model/scen.h"
#include "mapview.h"
#include "../resource.h" // must be included after Windows stuff
#include "strings.h"


#include <commdlg.h>
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <CommCtrl.h>
#include "propsheetproc.h"
//Number of property pages (tabs)
#define NUM_PAGES 9

#define MAP_OFFSET 10

// data shared by the property pages:

extern class Scenario scen;
extern HINSTANCE hApp;

extern struct PropSheetData
{
	int pindex;			//current player number
	class Player *p;			//current player struct
	int sel0, sel1;	//page dependant, should be reset on SETACTIVE
	HWND statusbar;
	HWND mapview;
	HMENU menu;
	UINT tformat, ecformat, mcformat;	//clipboard formats
} propdata;

HWND MakeMapView(HWND sheet, int cmdshow);
HWND MakeSheet(HINSTANCE app);
void SwitchPages(HINSTANCE app, HWND hPropSheetDlg, Scenario * scenario );

#endif	// AOKTS_DIALOGWIN_H 


makewindows.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "makewindows.h"

#include "editorsDlgProc.h"
DLGPROC procs[NUM_PAGES] =
{
	&IMsgsDlgProc,
	&PlyDlgProc,
	&VictDlgProc,
	&DisDlgProc,
	&MapDlgProc,
	&UnitDlgProc,
	&TrigDlgProc,
	&Units2DlgProc,
	&Triggers2DlgProc
};

// PAGES_TEMPLATES_BACKUP pagesBackup;

HWND MakeMapView(HWND sheet, int cmdshow)
{
	HWND ret;
	RECT rect;

	GetWindowRect(sheet, &rect);
	ret = CreateMapView(sheet, rect.right + MAP_OFFSET, rect.top, &scen);
	ShowWindow(ret, cmdshow);

	return ret;
}

/*
	MakeSheet: Creates the main property sheet window.

	Parameters:
	HINSTANCE app: Handle to the application loading the sheet.

	Note: Called once and only once by WinMain().
*/
HPROPSHEETPAGE pages[NUM_PAGES];

HWND MakeSheet(HINSTANCE app)
{
	PROPSHEETHEADER header;
	// HPROPSHEETPAGE pages[NUM_PAGES];
	PROPSHEETPAGE pg;	//used to create each page
	HWND sheet;

	//create pages

	pg.dwSize = sizeof(PROPSHEETPAGE);
	pg.dwFlags = PSP_DEFAULT;
	pg.hInstance = app;

	for (int i = 0; i < NUM_PAGES; i++)
			{
			pg.pszTemplate = MAKEINTRESOURCE(IDD_MSGS + i);	//template IDs are in display order
			pg.pfnDlgProc = procs[i];
			pg.lParam = 0;
			pages[i] = CreatePropertySheetPage(&pg);
			} // for

	//create sheet
	header.dwSize = sizeof(header);
	header.dwFlags = PSH_MODELESS | PSH_USECALLBACK |
		PSH_NOAPPLYNOW | PSH_NOCONTEXTHELP | PSH_USEICONID;
	header.hwndParent = NULL;
	header.hInstance = app;
	header.pszIcon = MAKEINTRESOURCE(IDI_LOGO);
	header.pszCaption = szTitle;
	header.nPages = NUM_PAGES;
	header.nStartPage = 0;
	header.phpage = pages;

	header.pfnCallback = &PropSheetProc;

	sheet = (HWND)PropertySheet(&header);
	
	PropSheet_RemovePage( sheet, 5, pages[5] );
	PropSheet_RemovePage( sheet, 6, pages[6] );

	//add status bar here (can't be done in PropertySheetProc)
	propdata.statusbar = CreateWindow(STATUSCLASSNAME, welcome,
		WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
		sheet, (HMENU)IDS_MAIN, hApp, NULL);
	return sheet;
}

// HINSTANCE app: Handle to the application using the sheet
void SwitchPages(HINSTANCE app, HWND hPropSheetDlg, Scenario * scenario )
{
    PropSheet_RemovePage( hPropSheetDlg, 5, pages[5] );
    PropSheet_RemovePage( hPropSheetDlg, 6, pages[6] );

	// Add two pages
	// PROPSHEETHEADER header;
	HPROPSHEETPAGE pages[2];
	PROPSHEETPAGE pg;	//used to create each page


	//create pages
	pg.dwSize = sizeof(PROPSHEETPAGE);
	pg.dwFlags = PSP_DEFAULT;
	pg.hInstance = app;	
	
	for (int i = 0; i < 4; i++)
			{
			pg.pszTemplate = MAKEINTRESOURCE(IDD_MSGS + (i+5) );	//template IDs are in display order
			pg.pfnDlgProc = procs[5+i];
			pg.lParam = 0;
			pages[i] = CreatePropertySheetPage(&pg);
			} // for

	if ( scenario->isOpen() )
	 {
	PropSheet_InsertPage(  hPropSheetDlg, 5, pages[0]);
	PropSheet_InsertPage( hPropSheetDlg, 6, pages[1]);
	 }
	else
	{
	PropSheet_InsertPage( hPropSheetDlg, 5, pages[2]);
	PropSheet_InsertPage( hPropSheetDlg, 6, pages[3]);
	}

}


How it should work:
- I start application,
- the pages: [0 - Info], [1 - Players], [2- Victory], [3 - Disables], [4 - Map], [5 - Units], [6 - Triggers], [7 - Units], [8 - Triggers], are created
- the tabs [5 - Units], [6 - Triggers], are removed
- I click on Units tab, and then I open file (see first code)
- file is opened, then SwitchPages is processed

Why it crashes? My idea is:
1
2
PropSheet_RemovePage( hPropSheetDlg, 5, pages[5] );
PropSheet_RemovePage( hPropSheetDlg, 6, pages[6] );

Tries to remove what was already removed? I have no idea how sheets works, what is on index 5 and 6, I guess that the page 7 and 8 had to be moved to index 5 and 6. Am I wrong?

But I tried different thing. The begin of the function I change to:
1
2
3
4
5
6
7
8
9
10
if ( scenario->isOpen() )
	 {
    PropSheet_RemovePage( hPropSheetDlg, 5, pages[7] );
    PropSheet_RemovePage( hPropSheetDlg, 6, pages[8] );
	}
	else
	{
    PropSheet_RemovePage( hPropSheetDlg, 5, pages[5] );
    PropSheet_RemovePage( hPropSheetDlg, 6, pages[6] );
	}

and it crashed too.

I tried to change those two lines to>
1
2
PropSheet_InsertPage(  hPropSheetDlg, 5, pages[0]);
PropSheet_InsertPage( hPropSheetDlg, 6, pages[1]);

and it crashed too.
well I don't understand your program, but I see this:
HPROPSHEETPAGE pages[2];

and then I see this:
1
2
3
4
5
6
for (int i = 0; i < 4; i++)
{
	// your other code....
	pages[i] = CreatePropertySheetPage(&pg);
} // for


see the issue?
Yes, thanks. Now it works!
Topic archived. No new replies allowed.