Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C++
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.
Posted
Updated 3-Nov-23 4:19am
v3
Comments
Richard MacCutchan 3-Nov-23 8:05am    
As far as I can see your cde should work. What is the value of iSelected on return?

1 solution

The problem you have is with this line:
C++
int iSelected = (int)SendMessage(GetDlgItem(hDlg, IDC_LIST2), LB_GETSELITEMS, 0, (LPARAM)pStatus.get());

The WPARAM item is required to contain the maximum size of the input buffer: see LB_GETSELITEMS message (Winuser.h) - Win32 apps | Microsoft Learn[^]. So it should be:
C++
int iSelected = (int)SendMessage(GetDlgItem(hDlg, IDC_LIST2), LB_GETSELITEMS, iSelCount, (LPARAM)pStatus.get());
 
Share this answer
 
Comments
CPallini 3-Nov-23 8:58am    
:thumbsup:
Good catch!
Richard MacCutchan 3-Nov-23 9:14am    
Thanks; but I had to run the code to discover the problem.
CPallini 3-Nov-23 9:34am    
A genuine experimental approach.
I was completely out of the way.
Richard MacCutchan 3-Nov-23 9:42am    
I also needed to learn how to use unique_ptr, as I still think in C and struggle with all these new-fangled classes. :)
Rick York 3-Nov-23 16:48pm    
unique_ptr is very, very handy. I try to use it whenever possible. One primitive way to use it is just to hold the pointer and eliminate the need for explicit deletion. I sometimes do this because it greatly simplifies code. You can have multiple return points and you don't have to remember to delete anything before each of them. I've been wrapping lots of resources in container classes just to have this automatic deletion capability.

What I meant earlier is you can construct an object in the usual way and then call the reset method of unique_ptr to save the pointer and that makes deleting the object automatic.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900