Required lifetime of gdi objects?

When an object is selected into a CDC, are the values copied or does the DC
keep a pointer to the object?  The point is, is it OK to select a local object
or does it need to be dynamically allocated?  I can find several examples of
selecting fonts in the help, but none that shed light on this point.

Can you do this (where font selection is carried out in a separate function
from the TextOut calls):
//===============================================
   CFont* pOldFont = NULL;

   {
      LOGFONT logFont;
      CFont font;

      logFont.lf... = ...;
      if (font.CreateFontIndirect(&logFont))
         pOldFont = pDC->SelectObject(&font);
   }   // <<== 'font' goes out of scope here.

   pDC->TextOut(...);

   if (pOldFont != NULL)
      pDC->SelectObject(pOldFont);
//===============================================
or do you need to do this?
//===============================================
   CFont* pOldFont = NULL;

   {
      CFont* pFont = new CFont;
      LOGFONT logFont;

      logFont.lf... = ...;
      if (pFont->CreateFontIndirect(&logFont))
         pOldFont = pDC->SelectObject(pFont);
   }

   pDC->TextOut(...);

   if (pOldFont != NULL) {
      CFont* pFont = pDC->SelectObject(pOldFont);

      delete pFont;
   }
//===============================================

--
Regards,

Joe Hotchkiss,
http://joe.hotchkiss.com

XXXXXXXXXXXXXXXXXXXXXXXXX
X   joe.hotchkiss       X
X   at baesystems.com   X
XXXXXXXXXXXXXXXXXXXXXXXXX



0
2/25/2004 5:15:49 PM
vc.mfc 33608 articles. 0 followers. Follow

2 Replies
614 Views

Similar Articles

[PageSpeed] 20

Joe,

Objects are not copied, and moreover, you will not be able to delete them
(in the GDI-sense, not the C++-one). DeleteObject is called by the
CGDIObject dtor, but this will fail, as the font (or whatever) will still be
selected into a CDC. Result: an ugly resource leak. So, the CGDIObject dtor
will have to be called *after* the GDI-object is unselected from the CDC.

Johan Rosengren
Abstrakt Mekanik AB

"Joe Hotchkiss" <nospam@baesystems.com> a �crit dans le message de
news:403cd734$1@baen1673807.greenlnk.net...
> When an object is selected into a CDC, are the values copied or does the
DC
> keep a pointer to the object?  The point is, is it OK to select a local
object
> or does it need to be dynamically allocated?  I can find several examples
of
> selecting fonts in the help, but none that shed light on this point.
>
> Can you do this (where font selection is carried out in a separate
function
> from the TextOut calls):
> //===============================================
>    CFont* pOldFont = NULL;
>
>    {
>       LOGFONT logFont;
>       CFont font;
>
>       logFont.lf... = ...;
>       if (font.CreateFontIndirect(&logFont))
>          pOldFont = pDC->SelectObject(&font);
>    }   // <<== 'font' goes out of scope here.
>
>    pDC->TextOut(...);
>
>    if (pOldFont != NULL)
>       pDC->SelectObject(pOldFont);
> //===============================================
> or do you need to do this?
> //===============================================
>    CFont* pOldFont = NULL;
>
>    {
>       CFont* pFont = new CFont;
>       LOGFONT logFont;
>
>       logFont.lf... = ...;
>       if (pFont->CreateFontIndirect(&logFont))
>          pOldFont = pDC->SelectObject(pFont);
>    }
>
>    pDC->TextOut(...);
>
>    if (pOldFont != NULL) {
>       CFont* pFont = pDC->SelectObject(pOldFont);
>
>       delete pFont;
>    }
> //===============================================
>
> --
> Regards,
>
> Joe Hotchkiss,
> http://joe.hotchkiss.com
>
> XXXXXXXXXXXXXXXXXXXXXXXXX
> X   joe.hotchkiss       X
> X   at baesystems.com   X
> XXXXXXXXXXXXXXXXXXXXXXXXX
>
>
>


0
2/23/2004 6:01:38 PM
It shouldn't matter. In fact, the objects are passed "by reference" by passing in the
handle. The simplest solution is shown below...

On Wed, 25 Feb 2004 17:15:49 -0000, "Joe Hotchkiss" <nospam@baesystems.com> wrote:

>When an object is selected into a CDC, are the values copied or does the DC
>keep a pointer to the object?  The point is, is it OK to select a local object
>or does it need to be dynamically allocated?  I can find several examples of
>selecting fonts in the help, but none that shed light on this point.
>
>Can you do this (where font selection is carried out in a separate function
>from the TextOut calls):
>//===============================================
>   CFont* pOldFont = NULL;  
*****
Useless. Get rid of this declaration. There is no need for it. You don't need the
variable. SaveDC/RestoreDC handles this for you.
Add up here
CFont font;
so it is not scoped as shown below
******
>
>   {
>      LOGFONT logFont;
>      CFont font;
>
>      logFont.lf... = ...;
>      if (font.CreateFontIndirect(&logFont))
>         pOldFont = pDC->SelectObject(&font);
****
      int save = pDC->SaveDC();

       if(font.CreateFontIndirect(&logFont))
           pDC->SelectObject(&font);
>   }   // <<== 'font' goes out of scope here.
****
You are dead. The CFont must exist as long as it is selected. 
So as I indicated, move the declaration upwards
*****
>
>   pDC->TextOut(...);
>
>   if (pOldFont != NULL)
>      pDC->SelectObject(pOldFont);
*****
Replace the two lines above with the simpler
     pDC->RestoreDC(save);
*****
>//===============================================
>or do you need to do this?
NO. There is no need to do the dynamic allocation. It wastes time, space, and conceptual
simplicity. It requires an explicit delete, which is not needed above. Let the font be
destroyed when it goes out of scope, but don't let it go out of scope until it is
deselected.


>//===============================================
>   CFont* pOldFont = NULL;
>
>   {
>      CFont* pFont = new CFont;
>      LOGFONT logFont;
>
>      logFont.lf... = ...;
>      if (pFont->CreateFontIndirect(&logFont))
>         pOldFont = pDC->SelectObject(pFont);
>   }
>
>   pDC->TextOut(...);
>
>   if (pOldFont != NULL) {
>      CFont* pFont = pDC->SelectObject(pOldFont);
>
>      delete pFont;
****
This is an unnecessarily complex and cumbersome method. In addition, the pFont that you
created with new is lost forever, and since it is lost, it is not possible to delete it.
The pFont you get from the SelectObject is a completely different pFont, or should be
assumed to be. There is no reason to expect it is the same.

You are taking a simple problem and making it difficult by trying to scope the name
improperly. Just move the font name outside the braces and your problems go away. Use
SaveDC/RestoreDC and you don't need to introduce all sorts of variables to hold "old"
copies (when I do graphics, fonts, pens, brushes, regions, ROP modes, etc. are all fair
game, and I got real tired of trying to track what could be variables for up to 30 or so
different DC parameters, which may themselves have many variants such as a half-dozen
brushes. And trying to track what needs to be deleted is a losing game. You will get it
wrong eventually, so why waste time even worrying about it. Let the destructors do their
own cleanup. Just make sure none of the objects are selected, and RestoreDC guarantees
that.. Note that SaveDC/RestoreDC can be nested many levels deep, with each SaveDC having
a matching RestoreDC. 
*****
>   }
>//===============================================

Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15975)
2/25/2004 8:59:30 PM
Reply:

Similar Artilces:

Macro Help Requires
I have two macros to run when somebody changes my excel sheet. The first one is assigned to a button, and just checks the spreadsheet for field "A/L" :- Sub RunIf() For Each c In Range("C3:F14") If c.Value = "A/L" Then Call Add_Appointment End If Next End Sub Question 1) Will this script look at every field from c3 to f14 eg c3, c4, c5, c6 etc. Question 2) Is there a way to update this script to only pick up amendments, or will this need to be in my second macro? My second macro creates an appointment within outlook calender:- Sub Add_Ap...

How to move all dbo schema objects to another schema?
Does anyone know of a way to move all objects in a db from schema dbo to another schema? mat (mat@notarealdotcom.adr) writes: > Does anyone know of a way to move all objects in a db from schema dbo to > another schema? In theory: SELECT 'ALTER SCHEMA newschema TRANSFER dbo.' + quotename(name) FROM sys.objects WHERE schema_id = 1 Excute and run result. But before you go ahead, beware of that in practice it is a lot more difficult. For starters you should probably add a WHERE clause to the SELECT, so that you don't include object types like cons...

SystemMailbox Object User Missing
Our Exchange 2003 server is running fine. However, in one information store we have both the SMTP and System Attendant mailboxes but but the SystemMailbox is missing (is that okay?). In the other three Information Stores the SystemMailbox is there, but with no associated users. Most posts refer to http://support.microsoft.com/kb/316622 However when it discusses how to recreate the associated object user it explains how to create the SystemMailbox too. I already have those. Can't we reconnect the existing SystemMailboxes to the new user we just created in Exchange System Manager?....

Percent of Objective
Here's the scenario: I have 5 areas of objectives, 2 measure in time, 2 in percent, and 1 in decimal. Going over objective in the Time and Decimal objectives is BAD, going over in the Percentage is GOOD! Example: You have a monthly blue widget objective (in time) 3:30 (3 hrs 30 min). At the end of the month you achieve 3:30, therefore you are at 100% of your objective. Lets say that Obj is in A3 and achieved is in A2; so A2/A3= 100%. Now lets say that the achieved is 4:10. If you use same formula of A2/A3 you have reached 119% of objective. However going OVER 3:30 is a bad thing in this...

An Object Can Not Be Found
After verifying my e-mail accounts are configured properly, I click the Add/Send button and I get the following error "The operation failed. An object can not be found." All I wanted to do was start completely over with Outlook 2003 with NO previous info, accounts, address books, etc. It should not be this damn hard to get Outlook to work as expected. Any insight would be appreciated. Thanks, Mark Fixed. "Mark McCasland" <mmccaslaATairmailDOTnet> wrote in message news:eNs%239505DHA.2432@TK2MSFTNGP10.phx.gbl... > After verifying my e-mail accounts are config...

Copying/Mirroring Objects, Images, and Text
Hello. I am having some trouble. I am printing out CD labels. The label stickers i have are two to a page and i have created one, and know where the other needs to go to line up properly, but i cant find a simple way to copy/paste it down there. I have two shape objects ( inner and outer circle of the cd ), some text boxes with text, and multiple image files. I cant find any way to select them all together and copy/paste it. Even if there was a way to mirror everything vertically downward that would work, as the orientation of the print, on the sticker, makes no difference. ...

Re-creating Great Plains Database Objects
Hi All, I just recovered data from corrupt Great Plains mdf files for GP 8.0 and imported to the newly created databases (Dynamics and the Company), I want to recreate sps,Functions,rules and views that came out of the box during the initial installation. I tried using the Great Plains utility tool ,it pickups up the database but I am failing to run the special Upgrade feature. Any ideas folks? -- "The Best thing in life is life" Ibutho, If this is already a corrupted environment, the best thing to do is scratch that installation and start over. Drop all GP databases on y...

How to identify which window is below my CWnd object?
Hi, I have a floating CWnd (moving on mouse move) with WS_POPUP style and tomost z-order. if there is a window below my CWnd object to avoid trailing or ghost images I need to Redraw that window. This is an MDI application. So at compile time I don't know which window will be there. Is there any api by which I will be able to find the window below my CWnd object. I tried with FindWindowEx() and GetWindow() but its not working. Thanks in advance. On Jun 11, 8:45=A0pm, sujeet27k...@gmail.com wrote: > Hi, > =A0 =A0 =A0 =A0 =A0 =A0I =A0have a floating CWnd (moving on mouse ...

Creating CRM Contact Error
Hi All I'm trying to create a contact record in MS CRM but I keep getting the same error message back from the CRMContact.srf webservice even though I'm providing all the correct detials. I'm using C# and the code is as follows: CUserAuth oUserAuth = oBizUser.WhoAmI(); strContactXML = "<contact><salutation>Mr</salutation><firstname>Fred</firstname><lastname>Quimby</lastname><telephone1>1111 791072</telephone1><telephone2>11111 822254</telephone2><telephone3></telephone3><fax>01705 823999<...

Forcing Entourage to require signing in BEFORE displaying e-mail
Version: 2008 Operating System: Mac OS X 10.5 (Leopard) Processor: Intel Email Client: Exchange Is it possible to have Entourage require signing in BEFORE it displays the already downloaded e-mail? Many of our school secretaries have expressed concern that once they logon to their computer, anyone could walk up to their computer and click the Entourage icon and see their e-mail. <br><br>Thank you, <br> Mike Reemsnyder On 2010-04-13 14:36:56 -0400, MIchael_Reemsnyder@officeformac.com said: > Exchange Is it possible to have Entourage require signing in BEFORE it...

server API & multi-creation of objects 04-08-04
Hello! I have the following scenario: I get data from a webservice. These data are (temporarily) stored in a XML file on the server. The data contain: 1 account, numerous customer addresses and numerous contacts. When I save the account a post-callout routine stores these data in MSCRM. It works fine, but: VERY SLOW. In my test case I have 26 customer addresses and 21 contacts. The whole post-callout routine needs 22 seconds. (When it is called the first time, it needs 33 seconds.) Through some tests I found out that 6 to 7 seconds of the time is consumed by the XSLT transformation ...

Can't hide far right columns:"Shift objects off sheet"??
I've got some constants and such in the far right-most columns, where my average user wouldn't have any need of going. Just to add an extra measure of safety, though, I wanted to either hide the columns or set the column width to something like 0.1. Excel won't let me do either - the error message is "Can't shift objects off the sheet." But there are no "objects" - just values in cells. What am I looking at? Is there another way to accomplish this? Ed Which version? Should be no problem in XL2003. BTW: Far right is not a good solution since it inflat...

Unable to add OLE object
Who can help here? I have made a form with a tab-control with several tabs. Within these tabs I have created subforms linked to the key of the main form. The data in the header of the main form comes from a query that gets the information from a table and one field from a query (this query groups a field based on the same key field as in the table of the main form). The properties of the main form are set to accept additions, changes and deletions, however, I can not right click on the OLE object and insert a photo. If I create a new form based on the table only, I can add a new OLE o...

CCommand object serialize problem
my code like this: CComand<CDynamicStringAccessor> rs0; ... rs0.Open( session,sqlstr,&ps); CFile theFile; theFile.Open(curdir+"db.dat",CFile::modeWrite); CArchive ar(&theFile, CArchive::store); ar.WriteObject((CObject *)&rs0); when it runs to ar.WriteObject((CObject *)&rs0),the system always report error: the memory "0x00000001" used by code "0x00000001" can not be read what wrong with it ? what I want is to store the rs0 from memory into hard disk,so I can use it after then by read it from hard disk how can I do it correct...

Why AD objects created always have the "domain admins" as owner ?
Hello, my account is member of the domain admins. Every time that I create an object, the owner displayed is the Domain Admins and not my user account. I know that there is a GPO for files and folders created by an admin to specify that the user who created the object is the owner (and not the admin if the user is an admin too) but is it possible to do the same with an AD object (and so let a user the user as the owner of an object even if it is member of the domain admins groups)? thank you -- Eric Hi That's by design, Domain Admins have and should have al...

vb6 run time error 5 when Outlook 2007 Explorer object displayed
hi all I have a vb6 program which integrates with MS Outlook and was working fine until the client upgraded their PC's - the OS stayed at XP Pro but Office was upgraded from 2003 to 2007. The app is now throwing a runtime error 5 "invalid procedure call or argument". This is difficult for me to debug as I my dev machine is W2K so I cannot install Office 2007 on it. But as far as I can tell the error is occurring when the code gets to the line myExplorer.Display - this is in response to the user double clicking a calendar item in Outlook. Just wondering is...

Is "SA" required to clear disconnected users in the activity window?
We have users continually needing help clearing the login info when they get disconnected from GP. I'd like our help desk to be able to do this. Can access to the User Activity window be granted to certain users so our help desk can clear the disconnected users? The User Activity window is under the Setup/System menu and requires the system password. We don't want the help desk to be able to do anything else under the Setup/System menu. I think it's just a matter of window security, so I need to be able to explain to the person responsible what they need to do. In versio...

Numeric data in a CString object
Hi, how can i establish if a CString object contains numeric data or something else (letters,....)? I use SpanExcluding() with a string that contains all the numeric characters you want to include (I.E., ")(0123456789.,"). The string returned should be empty if there are no invalid characters. Or course, you could always loop through the string yourself and use something like isdigit() to do something similar. Tom "Mittik" <accia77@hotmail.com> wrote in message news:_yYOf.18266$ou2.9332@tornado.fastwebnet.it... > Hi, > how can i establish if a CStr...

Does HQ require a dedicated server?
I currently have 2 store running RMS v1.2. I am adding another store and I want to add HQ. Do I need a dedicated server? There seems to be a real lack of info on the Microsoft website. Are there any good manuals for HQ? We have always installed a server and a full version of SQL Server with HQ Installs - usually Small Business Server Premium. HQ databases grow much more quickly that Store databases, and the 2GB MSDE limit will quickly become a problem. Also, a server is generally a much more robust piece of equipment, with at least some level of redundancy built in. You should...

Connection object error using RetrieveGlobals9.dll
Hello all, I'm having a sudden problem with a vba customization. I'm inserting a record in the pubs database using the RetrieveGlobals9.dll. The record insert works for sa but not for other users. The code (pasted below) taken from the RetrieveGlobals9 documentation always returns userinfo.status of 2 as soon as userinfo is instantiated when any user besides sa runs the vba code. I'm guessing it's something to do with password encryption in GP9 but I have verified that RetrieveGlobals9.dll is most recent and is properly registered. Any leads on how to further troub...

OLE Object
Hello There, please say me how will i retrieve the OLE Object from Microsoft Access into my program..if through a OLE Component please do say from where will i load the component..because in VB we have an OLE Component..... ...

WHEN I TRY TO SEND/RECEIVE IT SAYS "OBJECT NOT FOUND"
i updated outlook yesterday and now everytime i try to send/receive i get an error message that says "An object was not found". What does this mean??? "joan walker" <joan walker@discussions.microsoft.com> wrote in message news:3B1C9A3E-6DB3-4F38-B3BA-D59E67A77E84@microsoft.com... > i updated outlook yesterday and now everytime i try to send/receive i get an > error message that says "An object was not found". What does this mean??? What do you mean "updated Outlook"? Did you perform an upgrade, or did you install a service pack or patch?...

Domain logon required 2nd time when I try to view mail in Outlook2003/WinXP-SP2
WinXP SP2 did not fix the following problem: I am prompted for domain user name and password a second and sometimes a third time even though I had logged on to the SBS domain. This happens when I try to get my e-mail messages with my client's Outlook 2003 / WinXP SP2 System where these messages are stored on SBS Exchange. This dows NOT happen with my WinXP or WinXP SP2 / Outlook 2000 client systems Knowledge Base Article #820863 did not provide a fix for my computer. Couple of thoughts... 1) Check the event logs to see if the machine has lost its trust relationship. (Wi...

says "object could not be found" when sending email
After I transfered all my files from my laptop to my new desktop using Intelimover I tried to get on my Outlook to view and send email. Everything had vanished. No email accounts, no saved emails or folders. So I set up my hotmail account again to start everything over. When I tried to send an email, even forwarding or replying to one, this error popped up saying "The operation failed. Object cannot be found". So then the email does not get sent and proceeds to sit in my Outbox folder. I have reinstalled Office XP, searched through all help articles related, tried eve...

how do you remove an embedded object
How do you remove an embedded picture/graphic in a word document? Click on it to select and press Delete. -- Suzanne S. Barnhill Microsoft MVP (Word) Words into Type Fairhope, Alabama USA http://word.mvps.org "Cindy H" <Cindy H@discussions.microsoft.com> wrote in message news:C2942435-008B-40A9-8089-0C0D35DDAE75@microsoft.com... > How do you remove an embedded picture/graphic in a word document? > ...