To the first question, yes, LPARAM is intended to be used as an integer or pointer. This is clear from the definition:
typedef LONG_PTR LPARAM;
This is an integer long enough to hold the pointer.
About doing shared_ptr, you are right, if you pass the raw pointer and wrap it in another shared_ptr, you will free it twice:
shared_ptr<Thing> a; PostThreadMessage(x, 0, (LPARAM)a.get()); ... LRESULT OnMessage(int msg, WPARAM wp, LPARAM lp) { shared_ptr<Thing> p((Thing*)lp);
But you can try a workaround instead:
shared_ptr<Thing> a; PostThreadMessage(x, 0, new shared_ptr<Thing>(a)); //pointer to smart-pointer ... LRESULT OnMessage(int msg, WPARAM wp, LPARAM lp) { shared_ptr<Thing> *pp = (shared_ptr<Thing>*)lp; shared_ptr<Thing> p(*pp); delete pp; //no leak }
AFTER REPAIR . Note that PostThreadMessage may fail ... and you do not want shared_ptr to leak.
In my experience, it is generally best to use std :: deque to store data and use PostThreadMessage to notify if data is there. This way you will never lose an object! Ymmv
source share