Solved

Resource recovery after using SetWindowRgn

Posted on 1997-04-03
9
545 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

739 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