Dimi, if you think this is ugly, or if you are working on a better
solution,
please do protest.
Well, in part it's a matter of style, and IMO this will clutter the code. Moreover, the patch as it is now works by accident. For example:
/* send NM_RELEASEDCAPTURE notification */ - notify(infoPtr, NM_RELEASEDCAPTURE); + notify(infoPtr->hwndSelf, NM_RELEASEDCAPTURE);
if (!infoPtr->bFocus) SetFocus(infoPtr->hwndSelf);
See, after the notify() the infoPtr may be invalid. This means that if we assume the control can be deleted at any point we send out a notification, we have to reget and recheck infoPtr after every function call. There's got to be a better way.
I personally think an exception-based solution would be better. This is what exceptions where invented. I know they look alien in C, but I use them every day in Java for good effect. It's true, we have to be careful to release resources along the way, but we have only 6 such allocations that need protecting, and they are localized. And there we already have a try/catch simulated with goto's.
Another approach may be to ref count the structure pointed to by infoPtr, and release it at exit from WinProc when the count is 0. Sort of a poor man's garbage collection. I'm cool with this too, and in fact it may be cleaner then the exception-based solution.
On Tue, Sep 20, 2005 at 11:51:00AM -0400, Dimi Paun wrote:
I personally think an exception-based solution would be better. This is what exceptions where invented.
(...)
Correct me if I am wrong (I know absolutely nothing about how the listview code works) but a flaw of the 'exception based' solution would be if the application allocated memory that would re-use the just freed memory pointed by 'infoPtr'.
This would mean that no exception would be triggered while the code would still be wrong as it would write / read something that is clearly not what it expects.
Now one could say that knowing the heap allocation algorithms this case should never happen, but well, better safe than sorry.
Lionel
On Wed, 2005-09-21 at 00:11 +0200, Lionel Ulmer wrote:
Correct me if I am wrong (I know absolutely nothing about how the listview code works) but a flaw of the 'exception based' solution would be if the application allocated memory that would re-use the just freed memory pointed by 'infoPtr'.
This is true only if we rely on the access violation exception (which is a possibility, I admit). But the test patch that I sent checked the hwnd after each notify message with IsWindow(), and if invalid it was throwing a custom exception.
So no, it does not suffer from the flaw that you mentioned.
On Tue, Sep 20, 2005 at 11:32:38PM -0400, Dimi Paun wrote:
This is true only if we rely on the access violation exception (which is a possibility, I admit). But the test patch that I sent checked the hwnd after each notify message with IsWindow(), and if invalid it was throwing a custom exception.
OK, so next time I will read the patch before saying stupid things :-)
Lionel