You have quite a few problems/oddities:
1) Why does GetMonthList take the pwstrMonthList as a parameter? If it mallocs and frees it all in the function, taking it as a parameter is pointless. It might as well be local to the function.
2) Why are you using malloc/free anyway? new/delete should be preferred in C++.
3) You have some type issues. Remember that both * and [] operators dereference the pointer. Therefore *pwstrMonthList and pwstrMonthList[0] are equivilent and interchangable.
4) You are attempting to free pwstrMonthList, but pwstrMonthList was not allocated with malloc. This is why your program is crashing.
5) You are also (possibly) stepping outside the allocated bounds in your while loop. WCHAR* is likely bigger than WCHAR.
If you want to create a 2D array to hold these strings, you need to:
- allocate an array of
pointers.
- for each of those pointers, allocate an array of WCHARs
- To avoid memory leaks, you'll also need to destroy everything you've allocated
it'll look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
WCHAR** p; // p is a pointer to a pointer
p = new WCHAR*[12]; // allocate 12 pointers
for(int i = 0; i < 12; ++i)
{
p[i] = new WCHAR[80]; // for each pointer, allocate 80 WCHARs
}
// .. then, to clean up
for(int i = 0; i < 12; ++i)
{
delete[] p[i]; // delete each set of 80 allocated WCHARs
}
delete p; // delete the allocated pointers
|
Of course... all of this is much, much simpler and safer if you just use C++ container classes:
1 2 3
|
vector< wstring > myarray(12); // make a resizable array of strings with an initial size of 12
// all done. Don't have to worry about clean up or destroying anything
|