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

x
?
Solved

API to delete GDI objects that are not in use

Posted on 2003-11-19
5
Medium Priority
?
814 Views
Last Modified: 2008-02-01
Is there an API that I can call, that will free up the GDI objects that I have created, and finished using ?

We have a heavy application in terms of GDI objects count, and this might be a work around for the main problem.
0
Comment
Question by:UriS
[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
  • 3
5 Comments
 
LVL 3

Expert Comment

by:NBrownoh
ID: 9783409
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9783515
You have to be careful when creating and deleting GDI objecst as you can suck the life (memory) out of a system really fast by creating too many GDI objects.

As NBrownoh pointed out, you use the DeleteObject() API to delete your objects.

Here is some wisdom I just learned earlier today.  When you use the SelectObject() API, it returns a handle to the previously selected object.  You should then put this object back as the selected object and before deleting the object you just created and used.

Dim hbrush As Long
Dim ret As Long
   
hbrush = GetStockObject(NULLBRUSH) ' create an object
ret = SelectObject(hDC, hbrush) ' store handle to previous object
Polygon hDC, points(0), Me.numVertices ' use the object
SelectObject hDC, ret ' put previous object back
DeleteObject hbrush ' delte the object we created

Regards,

Idle_Mind
0
 
LVL 2

Author Comment

by:UriS
ID: 9784912
how can I delete the objects without first saving a pointer to them?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 9788277
You can't.

Idle_Mind
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 9828296
When you create objects and no longer have pointers to them, you create what is commonly known as a "memory leak".  The API calls you are making are not part of Visual Basic and as such, do not fall under its garbage collection scheme.  You have to manage your pointers and free the resources yourself.  Failure to do so will cause the system memory to be lost to all applications running until the system is rebooted.

Idle_Mind
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

604 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