Solved

Resource recovery after using SetWindowRgn

Posted on 1997-04-03
9
535 Views
Last Modified: 2011-10-03
How can I use "SetWindowRgn" without losing resources?  TurboPower's Memsleuth tells me that I lose a Region resource every time.  The following code produces the fault - or is a Memsleuth fault?


procedure TForm1.Button1Click(Sender: TObject);
var R:HRgn;
begin
  R := CreateRectRgn(0, 0, Width, Height);
  SetWindowRgn(Handle, R, false);
  SetWindowRgn(Handle, 0, false);
  DeleteObject(R);  { this call is unsucessful }
end;
0
Comment
Question by:wfy
  • 6
  • 3
9 Comments
 
LVL 3

Accepted Solution

by:
mheacock earned 200 total points
ID: 1335114
I believe you are misusing the SetWindowsRgn function
above.

Windows owns the region after you create it.  You should never try freeing it after you've created it.  Windows will do that for you.  So when your program closes down, MemSleuth will notify you that some memory has not been freed (the Rgn) since Windows has probably not freed it yet.

You will not lose memory...Windows will free the Rgn soon after your program shuts down.

Here's a blurb from the API help file:

"After a successful call to SetWindowRgn, the operating system owns the region specified by the region handle hRgn. The operating system does not make a copy of the region. Thus, you should not make any further function calls with this region handle. In particular, do not close this region handle.  

To obtain the window region of a window, call the GetWindowRgn function. "

0
 
LVL 3

Expert Comment

by:mheacock
ID: 1335115
Concerning your comment about multiple calls to SetWindowRgn...

Each Window handle has but one 'region', therefore multiple
calls SHOULD automatically (automagically) release the previous
HRGN, then reset it to the new value.

I'm not positive on this point, so you may want to do some
testing, then get back to me via a comment here.
0
 

Author Comment

by:wfy
ID: 1335116
Hi,  I have done some testing - on Win95 and WinNT.  On Win95 I terminated the test after a few hundred thousand repititions of the sample code.  My PC was very unhappy - the program took about 90 seconds (or so) to close down.  It was running through MemSleuth and MemSleuth had to be terminated with Ctrl-Alt-Delete.  The test on WinNT has now been going for 1.5 million repititions, and it is complaining of being low on memory.  So there definitely is a problem with this code being repeatedly called.  Maybe it would help if I stored the region handle between successive calls, and used the same handle again, without recreating the region?  But the Windows Documentation says that the window now owns the region and I should not alter it after calling SetWindowRgn.  So.... does that mean that SetWindowRgn should never be called repeatedly, but only once per window???  (I hope not).
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 3

Expert Comment

by:mheacock
ID: 1335117
Experiment some more...use the same handle repeatedly...see if
it makes your system unstable...try various ways of deleting
the HRGN object after the call to SetWindowRgn...perhaps
SetWindowRgn just uses a copy of the HGN you use in CreateRectRgn??  The docs are pretty sparse on this function and I wasn't even able to locate it in my Win32 API Bible.  It's in the Delphi help docs...but that is it.  There might be some more help on Microsoft...perhaps a search of their site for SetWindowRgn might turn up something...

I'd be interested in your results...if you don't mind posting them to this question, I'd be curious to see them.

0
 
LVL 3

Expert Comment

by:mheacock
ID: 1335118
0
 
LVL 3

Expert Comment

by:mheacock
ID: 1335119
Here's some sample code I found:

HRGN tempRgn = CreateRectRgnIndirect(&rcIXect);
SetWindowRgn(GetOuterWindow(), tempRgn, TRUE);
 
if (m_hRgn != NULL)
    DeleteObject(m_hRgn);
m_hRgn = tempRgn;


I guess you need two variables to hold the rect.  Create
a temp region, SET temp region, delete previous region (m_hRgn),
assign temp region to m_hRgn.

I also see they are using CreateRectRgnIndirect which is different than your CreateRectRgn.  Perhaps this is the key.

Happy programming.
0
 

Author Comment

by:wfy
ID: 1335120
Firstly, using the same handle repeatedly is real bad - the system becomes unstable very quickly. (less than 10 iterations)
I have tried deleting previous regions passed to SetWindowRgn using DeleteObject as your code suggests.  But the return value from DeleteObject always indicates that it is unsuccessful, and Memsleuth's reports still indicate loss of region resources.
I doubt that using CreateRectRgn or CreateRectRgnIndirect will make any difference.  Using a parameter of true or false to SetWindowRgn (3rd parameter) doesn't seem to make any difference either.
0
 

Author Comment

by:wfy
ID: 1335121
Firstly, using the same handle repeatedly is real bad - the system becomes unstable very quickly. (less than 10 iterations)
I have tried deleting previous regions passed to SetWindowRgn using DeleteObject as your code suggests.  But the return value from DeleteObject always indicates that it is unsuccessful, and Memsleuth's reports still indicate loss of region resources.
I doubt that using CreateRectRgn or CreateRectRgnIndirect will make any difference.  Using a parameter of true or false to SetWindowRgn (3rd parameter) doesn't seem to make any difference either.

P.S.  You MS WWW link gave me an empty search.  Could you e-mail the actual page to me??  wfy@ee.ed.ac.uk
0
 
LVL 3

Expert Comment

by:mheacock
ID: 1335122
Just go to MS...click the SEARCH graphic at the top of the
page...search for SetWindowRgn.  There is a code link...it
gives the sample I gave above.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question