SelectObject() Rules
-
Hi all, I'm suffering a major resource leak on Win95/98/Me in one of my drawing routines. I realise that before an object is destroyed one must ensure it is not selected in the device context by selecting the old object beforehand. However where my knowledge is lacking is with the use of multiple pens: For example suppose I have a red and a blue pen. Currently I store the old pen when selecting the red. I then use the red pen. Next I select the blue pen, ignoring the returned pen. After using the blue pen, before exiting I reselect the old pen (stored when selecting the red) I then call DestroyObject for both the red and blue pens. Is this sufficient or must I store the oldpen each time and restore it before selecting a new one? Surely not?! This leak is driving me insane! I know the leak is in this function because if I remove the call no crash occurs. However I cannot for the life of me see the leak! I've tried using the example here on CP to Save and Restore the DC state before and after drawing, without success. Thanks -- The Obliterator
-
Hi all, I'm suffering a major resource leak on Win95/98/Me in one of my drawing routines. I realise that before an object is destroyed one must ensure it is not selected in the device context by selecting the old object beforehand. However where my knowledge is lacking is with the use of multiple pens: For example suppose I have a red and a blue pen. Currently I store the old pen when selecting the red. I then use the red pen. Next I select the blue pen, ignoring the returned pen. After using the blue pen, before exiting I reselect the old pen (stored when selecting the red) I then call DestroyObject for both the red and blue pens. Is this sufficient or must I store the oldpen each time and restore it before selecting a new one? Surely not?! This leak is driving me insane! I know the leak is in this function because if I remove the call no crash occurs. However I cannot for the life of me see the leak! I've tried using the example here on CP to Save and Restore the DC state before and after drawing, without success. Thanks -- The Obliterator
If you are using MFC, every thing is taken care of if all the objects are local objects. But if you are programming in SDK you need to make calls to DeleteObject with the resource handle.
-
If you are using MFC, every thing is taken care of if all the objects are local objects. But if you are programming in SDK you need to make calls to DeleteObject with the resource handle.
I'm using MFC - and all is definitely not taken care of! Anyhow it looks like I've finally found the leak - I'll be testing on Win9x tomorrow to be sure! There was a pen constructed inside an if statement then selected. The old pen should have been reselected before leaving the if statement. As it was not I'm guessing the pen could not be correctly destroyed as its scope was lost. Thanks anyhow. -- The Obliterator
-
I'm using MFC - and all is definitely not taken care of! Anyhow it looks like I've finally found the leak - I'll be testing on Win9x tomorrow to be sure! There was a pen constructed inside an if statement then selected. The old pen should have been reselected before leaving the if statement. As it was not I'm guessing the pen could not be correctly destroyed as its scope was lost. Thanks anyhow. -- The Obliterator
Correct, a GDI object that is selected into a DC cannot be destroyed. --Mike-- Just released - RightClick-Encrypt v1.3 - Adds fast & easy file encryption to Explorer My really out-of-date homepage Sonork-100.19012 Acid_Helm
-
Correct, a GDI object that is selected into a DC cannot be destroyed. --Mike-- Just released - RightClick-Encrypt v1.3 - Adds fast & easy file encryption to Explorer My really out-of-date homepage Sonork-100.19012 Acid_Helm
Wouldn't you have thought it could assert in the destructor if selected in the DC??? -- The Obliterator
-
If you are using MFC, every thing is taken care of if all the objects are local objects. But if you are programming in SDK you need to make calls to DeleteObject with the resource handle.