Unhandled exception occurs when I try to do cleanup for STL vectors
NECESSARY INFORMATION:
NOTE:
This is a simplified description, in order to keep this question as short
as possible.
For further information ask additional questions, and I will provide more
information.
I have a dialog box with 2 edit controls, 2 buttons and combo box.
On dialog creation, in WM_INIDIALOG, a connection is established with a
database, which holds data about monthly salary of employees on a year
basis.
Then primary keys of the employees are loaded into combo box.
Tables look like this:
Table_Employee < #primary_key, ...>
Table_Salary < #pk_salary, $primary_key, January, February, ..., Year>
Relationship is one to many, since one employee has data about monthly
salary for every year, like this:
| January | ... | Year | #pk_salary| $primary_key| | 1000.51 | ... | 2012
| 100025558 | 48089989891 | | 2000.51 | ... | 2013 | 552025558 |
48089989891 | ...
Once a user selects a primary key from combo box , he is able to change
the data about monthly salary by typing it in first edit control, and he
must type in the year in the second edit control.
Entered data is held in a vectors declared like this:
INT_PTR CALLBACK dlgProcedure(HWND hwnd, UINT Message,
WPARAM wParam, LPARAM lParam )
{
static vector<wchar_t*> ee; // vector for monthly salaries and a year
// this vector holds data for all thes
tatic vector< vector<wchar_t*> > Pee; years
...
case WM_INITDIALOG:
{
// 12 months + 1 year = vector of 13 to store data
ee.assign( 13, LoadedValue );
...
After user pushes the first button , data for the month is saved in the
above vector like this:
case IDC_BUTTON_MONTH:
{
// needed, since we have vector of wchar_t*
wchar_t *temp = new wchar_t[50];
GetDlgItemInt( ... , temp, ... );
UINT i = // ordinal of the month taken from checkbox
ee[ i ] = temp;
Then user must enter year, and after pushing the second button it is
stored like this:
case IDC_BUTTON_YEAR:
{
wchar_t *temp = new wchar_t[50]; // needed, since we have vector
of wchar_t*
GetDlgItemInt( ... , temp, ... );
ee[12] = temp;
// This means that all the data is collected
// so we can store this year's data in the vector for years
Pee.push_back(ee);
This way, vector Pee holds data for all the years ( 2012, 2013, ... ), and
vector ee holds the specifics ( monthly salary for a certain year ).
THE PROBEM:
After selection in the combo box changes, I must clear all vectors, in
order for new data to be stored.
When I do that, I get the error, and my program snaps. Crash occurs also
when I try to close the window.
If I comment out the section of the code that clears vectors, my program
works, but then I can not use it to store new data, since vectors are not
cleared properly.
IMPORTANT INFORMATIONS:
Once I start program and change selection in combo box,a dialog box pops
up, with 2 debuggers offered, and this message:
An unhandled exception occurred in SomeProgramName.exe[3300].
In Debug, in MS Visual Studio 2008, I have clicked on Exceptions, and
checked everything. After I start the program in Debug mode, I get the
dialog box with following message:
This may be due to a corruption of the heap, which indicates a bug in
MyProgramName.exe or any of the DLLs it has loaded.
This may also be due to the user pressing F12 while MyProgramName.exe has
focus.
The output window may have more diagnostic information.
As I have said above, after I comment out the cleanup code, error no
longer occurs.
That is why I am very sure that there lies my problem.
PROBLEMATIC CODE SNIPPETS:
Handler for WM_CLOSE:
case WM_CLOSE:
{
// cleanup
for( vector<wchar_t*>::size_type i = 0; i < ee.size(); i++)
delete[] ee[i];
ee.clear();
for( vector< vector<wchar_t*> >::size_type i = 0; i <
pee.size(); i++)
for( vector<wchar_t*>::size_type j = 0; j < pee[i].size();
j++)
delete[] pee[i][j];
pee.clear();
DestroyWindow( hDlg );
}
return TRUE;
Handler for combo box:
case IDC_COMBO12:
{
if(HIWORD(wParam) == CBN_SELCHANGE )
{
// cleanup
for( vector<wchar_t*>::size_type i = 0; i < ee.size(); i++)
delete[] ee[i];
ee.clear();
for( vector< vector<wchar_t*> >::size_type i = 0; i <
pee.size(); i++)
for( vector<wchar_t*>::size_type j = 0; j <
pee[i].size(); j++)
delete[] pee[i][j];
pee.clear();
// assign neutral values to vectors
ee.assign( 13, L"-1" );
for( int i = 2012; i < currentYear; i++ )
Pee.push_back(ee);
// other commands, like loading data from database and so
on...
THE QUESTION:
Since I have a vector of pointers ( vector < wchar_t* > ) , I believe that
I can't just use clear() method to empty the vector since it would cause
memory leaks.
That is why, in my opinion, I must delete pointer first, and only then use
clear() method. This is my first time using vector of wchar_t*, so I ask
the community what am I doing wrong here?
How should I correctly reset those vectors in my handlers ?
Ask for additional information, I will gladly provide them.
No comments:
Post a Comment