Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Resource recovery after using SetWindowRgn

Posted on 1997-04-03
9
Medium Priority
?
570 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 600 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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: 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

Technology Partners: 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!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

618 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