[plt-dev] GDI handle leak when misusing set-width on a pen%

From: Jakub Piotr Cłapa (jpc-ml at zenburn.net)
Date: Mon Jul 27 17:46:06 EDT 2009

I had some problems with a long running MrEd app which I managed to 
distill into this small script:

#lang scheme/gui
(define bm (make-object bitmap% 100 100))
(define dc (make-object bitmap-dc% bm))

(for ([i (in-range 100)])
   (send dc set-pen "white" 5 'solid)
   (send (send dc get-pen) set-width 1)
   (send dc set-pen "white" 1 'solid))

After running the loop the GDI handle count in Process Explorer jumps by 
100 and it never gets back (even after a custodian shutdown by Ctrl-K). 
OTOH in my actual app custodian shutdowns seemed to release these 
handles (at least sometimes). After the counter reaches 10000 (on the XP 
Prof. computer I tested on) strange things happen when the app (or the 
system) tries to redraw anything on the screen.

After re-reading the documentation I understand that set-width can be 
used neither on pens selected in a DC nor on ones that are obtained from 
a pen-list (which IIUC is implied by using set-pen) so I screwed up 
twice. ;]
Maybe an exception would be helpful here?

Jakub Piotr Cłapa

Posted on the dev mailing list.