I would like to update my MFC production code to use the std::shared_ptr smart pointer when calling other windows or threads. Such calls are SendMessage , PostMessage and PostThreadMessage , which pass wparam and lparam and which are unsigned int and long respectively. I am currently creating a class object, a new object, making a call passing a pointer to the object, using the object on the receiving side, and then deleting it.
Since shared_ptr works so well in the rest of my code, I would like to at least study the reasons why I cannot do the same for Windows calls.
Current call:
auto myParams = new MyParams(value1, value2, value3); PostThreadMessage(MSG_ID, 0, reinterpret_cast< LPARAM >( myParams ); ReceivingMethod::OnMsgId( WPARAM wParam, LPARAM lParam) { auto myParams = reinterpret_cast< MyParams * >( lParam ); ...
for C ++ an 11-like smart pointer call:
std::shared_ptr< MyParams > myParams( new MyParams( value1, value2, value3 ) ); PostThreadMessage( MSG_ID, 0, ???myParams??? ); ReceivingMethod::OnMsgId( WPARAM wParam, LPARAM lParam ) { auto myParams = ???lParam???; ...
Change 1:
@Remy Lebeau: Here is an example of code that has been modified to use the unique_ptr transfer method, however there are leaks in the transmission of an object in my code.
struct Logger { Logger() { errorLogger = ( ErrorLogger * )AfxBeginThread( RUNTIME_CLASS( ErrorLogger ), THREAD_PRIORITY_BELOW_NORMAL ); } ~Logger() {
Note that when I comment on the passage of unique_ptr, the leaks disappear. How is my code different from your code that uses this approach and works?
Edit2:
Regarding @Remy Lebeau βs answer, showing how std::unique_ptr can be used instead of std::shared_ptr , I said in a comment below that β... there are no additional objects to implement. There are no obvious minusesβ. Well, thatβs not entirely true. A MyParams object must be created for each type of message. Some applications may have only a few types, but some may have 100 or more. Each time I want to execute a function on the other hand, I have to create a new structure that has a constructor that takes all the arguments of the target call. Very tiring to implement and difficult to maintain if there are many.
I think that this phase of building the structure could be eliminated by simply passing arguments.
There are apparently new C ++ 1x constructs that can help with this. One of them is probably std::forward_as_tuple , which "Creates a tuple of references to arguments in arguments suitable for transfer as a function argument".
For my application, I solved the problem by templatizing MyParams , but for those who want to avoid adding a lot of structures, they might want to take a look at using tuples, etc.
Edit 3: The answers to the answer number @RemyLebeau 1 and @ rtischer8277s are correct. Unfortunately, StackOverflow does not recognize several correct answers. This StackOverflow limitation reflects the erroneous psycholinguistic assumption that the linguistic context is universal for the same language group, and this is not so. (see "Introduction to Integral Linguistics" by Roger Harris for the myth of the language of fixed code, p. 34). In response to my original post, @RemyLebeau answered a question based on the context described by the published code that shows new ed MyParams (see "Editing 2: for a more detailed explanation"). Much later, in answer 5 (rtischer8277), I myself answered the question, based on the original wording of the question, which asked if std::shared_ptr can be used on threads using PostThreadMessage . As a reasonable consequence, I returned the correct answer to @RemyLebeau, this is the first correct answer.
EDIT4: I added a third legitimate response to this post. See the 6th answer starting with @Remy Lebeau and @ rtischer8277 so far have provided two answers to my original post .... The effect of this solution is to turn the end-to-end stream into a conceptually simple RPC (remote procedure call) . Although this answer shows how to use the future to control ownership and synchronization, it does not show how to create a message object with an arbitrary number of parameters that can be safely used on both sides of the PostThreadMessage call. This functionality is addressed in StackOverflows Response to the release of passing a parameter package over an obsolete function signature using forward_as_tuple .