473,779 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

hDC lifetime

Does the device context handle for a graphics device change during the
lifetime of the underlying object?

I'm asking because I ran across some code that stores the hDC in the class
rather than call Graphics.GetHdc in the OnPaint method.
Nov 16 '05 #1
6 1583
"Chuck Bowling" <ch**********@s bcglobal-NO-SPAM.net> wrote in message
news:Ob******** ******@TK2MSFTN GP14.phx.gbl...
Does the device context handle for a graphics device change during the
lifetime of the underlying object?

I'm asking because I ran across some code that stores the hDC in the class
rather than call Graphics.GetHdc in the OnPaint method.


Theoretically you could, but you might get broken by future releases.
Imagine if MS decided that to change GetDC so that it wouldn't return the
underlying DC but rather a compatible DC, and then would blit the drawing
across on ReleaseDC. Suddenly your preciously held hDC would be worthless.
I'm assuming of course that ReleaseDC is called soon after GetDC.
Nov 16 '05 #2
Yes, this can be a serious problem. You should call ReleaseDC as soon as
possible. I assume if it's holding onto the handle, that it's not releasing
the DC. DCs are a very prescious resource. The number of available DCs at
any one time is very low. Something like 8, maybe less, if I recall
correctly.

So by not releasing it, you could run into problems where other, well
behaving apps, could find that there are no DCs available for them to do
their painting.

The DC may not change, but that's not the problem. The problem is that there
just aren't many of them.

Pete

"Sean Hederman" <us***@blogentr y.com> wrote in message
news:cv******** **@ctb-nnrp2.saix.net. ..
"Chuck Bowling" <ch**********@s bcglobal-NO-SPAM.net> wrote in message
news:Ob******** ******@TK2MSFTN GP14.phx.gbl...
Does the device context handle for a graphics device change during the
lifetime of the underlying object?

I'm asking because I ran across some code that stores the hDC in the class rather than call Graphics.GetHdc in the OnPaint method.


Theoretically you could, but you might get broken by future releases.
Imagine if MS decided that to change GetDC so that it wouldn't return the
underlying DC but rather a compatible DC, and then would blit the drawing
across on ReleaseDC. Suddenly your preciously held hDC would be worthless.
I'm assuming of course that ReleaseDC is called soon after GetDC.

Nov 16 '05 #3

"Sean Hederman" <us***@blogentr y.com> wrote in message
news:cv******** **@ctb-nnrp2.saix.net. ..
"Chuck Bowling" <ch**********@s bcglobal-NO-SPAM.net> wrote in message
news:Ob******** ******@TK2MSFTN GP14.phx.gbl...
Does the device context handle for a graphics device change during the
lifetime of the underlying object?

I'm asking because I ran across some code that stores the hDC in the
class rather than call Graphics.GetHdc in the OnPaint method.


Theoretically you could, but you might get broken by future releases.
Imagine if MS decided that to change GetDC so that it wouldn't return the
underlying DC but rather a compatible DC, and then would blit the drawing
across on ReleaseDC. Suddenly your preciously held hDC would be worthless.
I'm assuming of course that ReleaseDC is called soon after GetDC.


Yes it is. The GetHdc is followed immediately by a ReleaseDC and the DC is
stored in a variable. It appears to work but I get what you're saying.
Thanks.
Nov 16 '05 #4
Well actually, the DC is being released immediately. That's what I was
puzzled about. GetHdc aquires the handle and stores it as an IntPtr then
ReleaseHdc is called. Afterwards the IntPtr is used to draw with.

"Pete Davis" <pd******@NOSPA M.hotmail.com> wrote in message
news:-K************** ******@giganews .com...
Yes, this can be a serious problem. You should call ReleaseDC as soon as
possible. I assume if it's holding onto the handle, that it's not
releasing
the DC. DCs are a very prescious resource. The number of available DCs at
any one time is very low. Something like 8, maybe less, if I recall
correctly.

So by not releasing it, you could run into problems where other, well
behaving apps, could find that there are no DCs available for them to do
their painting.

The DC may not change, but that's not the problem. The problem is that
there
just aren't many of them.

Pete

"Sean Hederman" <us***@blogentr y.com> wrote in message
news:cv******** **@ctb-nnrp2.saix.net. ..
"Chuck Bowling" <ch**********@s bcglobal-NO-SPAM.net> wrote in message
news:Ob******** ******@TK2MSFTN GP14.phx.gbl...
> Does the device context handle for a graphics device change during the
> lifetime of the underlying object?
>
> I'm asking because I ran across some code that stores the hDC in the class > rather than call Graphics.GetHdc in the OnPaint method.


Theoretically you could, but you might get broken by future releases.
Imagine if MS decided that to change GetDC so that it wouldn't return the
underlying DC but rather a compatible DC, and then would blit the drawing
across on ReleaseDC. Suddenly your preciously held hDC would be
worthless.
I'm assuming of course that ReleaseDC is called soon after GetDC.


Nov 16 '05 #5
"Chuck Bowling" <ch**********@s bcglobal-NO-SPAM.net> wrote in message
news:Of******** ******@TK2MSFTN GP09.phx.gbl...
Well actually, the DC is being released immediately. That's what I was
puzzled about. GetHdc aquires the handle and stores it as an IntPtr then
ReleaseHdc is called. Afterwards the IntPtr is used to draw with.
So what's happenning is that the code is using knowledge about how the class
operates internally in order to work. It's a serious breaking of
encapsulation.
"Pete Davis" <pd******@NOSPA M.hotmail.com> wrote in message
news:-K************** ******@giganews .com...
Yes, this can be a serious problem. You should call ReleaseDC as soon as
possible. I assume if it's holding onto the handle, that it's not
releasing
the DC. DCs are a very prescious resource. The number of available DCs at
any one time is very low. Something like 8, maybe less, if I recall
correctly.

So by not releasing it, you could run into problems where other, well
behaving apps, could find that there are no DCs available for them to do
their painting.

The DC may not change, but that's not the problem. The problem is that
there
just aren't many of them.

Pete

"Sean Hederman" <us***@blogentr y.com> wrote in message
news:cv******** **@ctb-nnrp2.saix.net. ..
"Chuck Bowling" <ch**********@s bcglobal-NO-SPAM.net> wrote in message
news:Ob******** ******@TK2MSFTN GP14.phx.gbl...
> Does the device context handle for a graphics device change during the
> lifetime of the underlying object?
>
> I'm asking because I ran across some code that stores the hDC in the

class
> rather than call Graphics.GetHdc in the OnPaint method.

Theoretically you could, but you might get broken by future releases.
Imagine if MS decided that to change GetDC so that it wouldn't return
the
underlying DC but rather a compatible DC, and then would blit the
drawing
across on ReleaseDC. Suddenly your preciously held hDC would be
worthless.
I'm assuming of course that ReleaseDC is called soon after GetDC.



Nov 16 '05 #6
"Sean Hederman" <us***@blogentr y.com> wrote in message
news:cv******** **@ctb-nnrp2.saix.net. ..
"Chuck Bowling" <ch**********@s bcglobal-NO-SPAM.net> wrote in message
news:Of******** ******@TK2MSFTN GP09.phx.gbl...
Well actually, the DC is being released immediately. That's what I was
puzzled about. GetHdc aquires the handle and stores it as an IntPtr then
ReleaseHdc is called. Afterwards the IntPtr is used to draw with.


So what's happenning is that the code is using knowledge about how the
class operates internally in order to work. It's a serious breaking of
encapsulation.


Just to add to the pot, I just thought of something that *should* break the
code you're talking about: double-buffering. When the DoubleBuffer style is
set, the Graphics object passed to the Paint methods/events is not the
controls Graphics object, but rather an in-memory bitmap. Keeping the hDC of
this around would be useless at best. So, enabling double-buffering would
break the existing code. Not good.
"Pete Davis" <pd******@NOSPA M.hotmail.com> wrote in message
news:-K************** ******@giganews .com...
Yes, this can be a serious problem. You should call ReleaseDC as soon as
possible. I assume if it's holding onto the handle, that it's not
releasing
the DC. DCs are a very prescious resource. The number of available DCs
at
any one time is very low. Something like 8, maybe less, if I recall
correctly.

So by not releasing it, you could run into problems where other, well
behaving apps, could find that there are no DCs available for them to do
their painting.

The DC may not change, but that's not the problem. The problem is that
there
just aren't many of them.

Pete

"Sean Hederman" <us***@blogentr y.com> wrote in message
news:cv******** **@ctb-nnrp2.saix.net. ..
"Chuck Bowling" <ch**********@s bcglobal-NO-SPAM.net> wrote in message
news:Ob******** ******@TK2MSFTN GP14.phx.gbl...
> Does the device context handle for a graphics device change during
> the
> lifetime of the underlying object?
>
> I'm asking because I ran across some code that stores the hDC in the
class
> rather than call Graphics.GetHdc in the OnPaint method.

Theoretically you could, but you might get broken by future releases.
Imagine if MS decided that to change GetDC so that it wouldn't return
the
underlying DC but rather a compatible DC, and then would blit the
drawing
across on ReleaseDC. Suddenly your preciously held hDC would be
worthless.
I'm assuming of course that ReleaseDC is called soon after GetDC.



Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3681
by: Jason Heyes | last post by:
I am interested in the lifetime of a function argument in two cases. They are: 1. void foo(Bar bar); 2. void foo(const Bar &bar); In each case I call foo like so: foo(Bar());
8
2945
by: pt | last post by:
Hallo, i wonder how it is going to be of this code below regarding of the return of temporary object. Prototypes: =========== bool Activation(TCHAR *c); std::basic_string<TCHAR> GetFile();
0
1140
by: JD | last post by:
I'm currently writing a class to manage per-user configuration files, using the same format as the app.config file. This will be used in a Winforms app to manage user options, window positions, etc. When instantiated, the object will be available as a service at the application level. The question I have is about the most efficient way to manage the lifetime of the XmlDocument object that contains the XML file itself. Currently, I do...
14
3169
by: MuZZy | last post by:
Hi, Lately i've been (and still am) fixing some memory leaks problems in the project i just took over when i got this new job. Among the other issues i've noticed that for localy created objects it makes difference to explicitly put them to null after working with them is done - it somehow makes the GC collect them sooner, like here: void SomeFunc() { MyClass c = new MyClass();
1
1079
by: Eduardo Azevedo | last post by:
If I have a class with method Shared, when this objects are destroies what's happend with the objects Shared? How long is the lifetime of a function shared in the Class ? there are no instantiate tks Eduardo R Azevedo
14
4382
by: Frederick Gotham | last post by:
There is a common misconception, (one which I myself also held at one point), that a const reference can "extend the lifetime of a temporary". Examples such as the following are given: Snippet (1) ----------- #include <string> using std::string;
3
2341
by: nagashre | last post by:
class A { public: A():a(0), b(0){} handleMyMsg( char* aa, char*bb); private: processMessage();
3
1566
by: mario semo | last post by:
Hello, What does the C++ Norm says about the lifetime of compiler generated temporary variables? #include <stdio.h> class BaseRef { //--------------------------------------------------------------------------
6
2697
by: better_cs_now | last post by:
Hello all, class Foo {/* Details don't matter */}; class Bar { public: Bar(): m_Foo(/* Construct a Foo however it wants to be constructed */); const Foo &GetFoo() const { return m_Foo; } private:
5
1482
by: Juha Nieminen | last post by:
Let's assume we have a class like this: //--------------------------------------------------------- #include <iostream> class MyClass { public: MyClass() { std::cout << "constructor\n"; } ~MyClass() { std::cout << "destructor\n"; }
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10306
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.