EDIT#3:
It seems that after using wstring the problem was in the buffer overflow.
After changing the code like below ( putting 48 instead of 50 ), everything seems to work fine:
wchar_t temp[50], destination[50];
GetDlgItem( ... , temp, 48 );
swprintf_s( destination, 50, L"%s", temp );
This is directed to those members who observed that my code is more C than C++:
You are right, it is so because I use pure Win32 API, and all examples are in C.
To use it in C++ I need to know MFC, which I don't at this moment.
For all those who tried to help, you get +5 from me.
I have tried your answers and they all worked, but the best advice is just to use wstring.
I was afraid to do that since I am a beginner, but thanks to the help of the community I think I have managed to learn something new.
Thank you all, again.
The last thing I ask for is if somebody can explain me briefly why the above code, when number 48 is substituted with 50, creates buffer overflow ?
Thank you all again.
Regards.
EDIT#2:
Replaced vectors of wchar_t pointers with vectors of wstring, but crash still occurs.
Still, performance is better. After I isolate the problem I will update my question again.
Thanks to all members who posted their answers and comments, they are highly appreciated.
Regards.
EDIT#1:
EDITED QUESTION TO CORRECT TYPOS THAT APPEARED IN CODE SNIPPETS ( thank you Mr. pwasser for pointing them out ).
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;
static vector< vector< wchar_t* > > Pee; ...
case WM_INITDIALOG:
{
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:
{
wchar_t *temp = new wchar_t[50];
GetDlgItemInt( ... , temp, ... );
UINT i =
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];
GetDlgItemInt( ... , temp, ... );
ee[12] = temp;
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:
{
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 )
{
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();
ee.assign( 13, L"-1" );
for( int i = 2012; i < currentYear; i++ )
Pee.push_back(ee);
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 ?
I use MS Visual Studio C++, and pure WIN32. Ask for additional information, I will gladly provide them.