Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8001059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:59:18+00:00 2026-06-04T15:59:18+00:00

In my program I have a memory DC to which I frequenty draw, then

  • 0

In my program I have a memory DC to which I frequenty draw, then in response to WM_PAINT I quickly BitBlt it to the screen. I’ve always done the drawing like this, the memory DC is created in response to WM_CREATE btw.

void Draw()
{
    HFONT hPrevFont = (HFONT)SelectObject(m_hMemDC, m_hFont);
    // draw text
    SelectObject(m_hMemDC, hPrevFont);
}

Which should be correct, I’ve also found out now that I can use SaveDC and RestoreDC for that (especially useful when having a lot of GDI objects)

void Draw()
{
    int nSavedDC = SaveDC(m_hMemDC);
    SelectObject(m_hMemDC, m_hFont);
    // draw text
    RestoreDC(m_hMemDC, nSavedDC);
}

Now I wonder whether it is ok to just save the state of the DC after I created it, and restore it when I’m deleting it. Like this:

int OnCreate(...)
{
    m_hMemDC = CreateMemDC();
    m_nSavedDC = SaveDC(m_hMemDC);
}

void Draw()
{
    SelectObject(m_hMemDC, m_hFont);
    // draw text
}

void OnDestroy()
{
    RestoreDC(m_hMemDC, m_nSavedDC);
    DeleteMemDC(m_hMemDC);
}

This should be alright I think, It’s not like anyone else is using my memory DC in the mean time. What do you think?

Heck, is it even necessary to save and restore the DC at all in this case?

EDIT: Yes, I do realize I should delete the memory DC first before deleting the font (because otherwise it would still be used by the memory DC).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:59:20+00:00Added an answer on June 4, 2026 at 3:59 pm

    The danger is that you might leave an object selected into your memory DC that can be used by only one DC at a time. Traditionally, you deselect the object out of the DC at the end of your Draw cycle so that the object is available to be selected into another DC. If you delay the deselect, then nobody else can use the object since it’s still in your DC.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C++ program which, during execution, will allocate about 3-8Gb of memory
I have a program like this: it compiles files in memory and then executes
I have a program that needs a lot of memory and want to set
I have a program that starts up and creates an in-memory data model and
I have a python program that is going to eat a lot of memory,
I have a 'server' program that updates many linked lists in shared memory in
In an embedded program I have a screen object that needs to manage a
I have a program which accepts 2 N -digit numbers, multiplies them using threads
I have a program which I believe to be running in .NET 4.0, but
I have a memory leak in my C# program and cannot determine who is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.