I have come to realise that one of the issues causing failure in my application recently is the improper use of smart pointer. I thought I had complete grasp of its usage until now!
Consider the code snippet from an application that consumed substantial amount of my time.
int iSelCount = (int)SendMessage(GetDlgItem(hDlg, IDC_LIST2),
LB_GETSELCOUNT, 0, 0);
unique_ptr<int[]> pStatus = make_unique<int[]>(iSelCount);
int iSelected = (int)SendMessage(GetDlgItem(hDlg, IDC_LIST2),
LB_GETSELITEMS, 0, (LPARAM)pStatus.get());
if (iSelCount > iSelected)
{
AppErrMessage msg{
AppErrMessage::ERM_TOO_MANY_STATUS_SELECTIONS };
pair<wstring, wstring> err = GetAppErrMessage(msg);
MessageBox(hDlg, err.first.c_str(), err.second.c_str(), MB_OK);
return (INT_PTR)TRUE;
}
Debugging shows that the smart pointer
pStatus
does not receive the various indices of items selected in the listbox.
Examples on the internet all show smart pointers being initialized with value at creation. None show their use for various other purpuses much later after their creation. How do I use a
std::unique_ptrbin
in the above situation. I wanted to use a vector, but it seemed ill-advised to so do. Can a vector be used in the same situation? Please help with sample code.
What I have tried:
The internet has no solution to this as far as my searching effort is concerned.