Article View: cmu.comp.lang.c++
Article #4Re: C++ WINDOWS QUESTION
From: Frederic LACHASSE
Date: Tue, 24 Oct 1995 14:53
Date: Tue, 24 Oct 1995 14:53
78 lines
2797 bytes
2797 bytes
Daniel Teitelbaum <dt2m+@andrew.cmu.edu> wrote:
>I posted this question about 10 days ago and got no answer. Just for
>the heck of it, I'll try again. This time, I'm including the code.
>I have a function that figures out coordinates and draws and fills
>rectangles. The function is called DrawEngineer1. All I pass this
>function is the device context which then gets passed to standard
>windows calls like FillRect. The program runs
>fine, draws the rectangles, and then bombs when it tries to exit this
>function. Some sort of array index out of bounds error.
>If, instead of making a function call I copy the code of the called
>function into the body of the calling function, the program works fine.
>Is there a problem with passing the device context?
>-----------------------------------------*
[snip]
>void TMyAppWindow::DrawEngineer1(HDC PaintDC)
>{
> HBRUSH gbr, cbr;
>
>
> RECT rc;
> RECT *rectptr;
> for (int i = 0; i < 4; i++)
> {
> rc.left = 50+(i*50);
> rc.right = 100+(i*50);
> rc.top = 150+(i*50);
> rc.bottom = 200+(i*50);
> *rectptr = rc;
========= bug ==========
replace by:
rectptr = &rc;
> if ( (i % 2) == 0)
> {
> cbr = CreateSolidBrush(RGB(0, 255, 255));
> FillRect(PaintDC, rectptr, cbr);
> }
> else
> {
> cbr = CreateSolidBrush(RGB(200, 0, 50));
> FillRect(PaintDC, rectptr, cbr);
> }
> }
>
> TextOut(PaintDC, 83, 83, "Hello", 5);
> //******************************************************************
> //RunSim1();
[snip]
note: better to remove the "RECT *rectptr" variable and use
FillRect(PaintDC, &rc, cbr);
Consequence of your glitch: you copy the content of rc into the RECT
object pointed by rectptr. As rectptr is uninitialized, the behaviour
is mostly chance: if very lucky, the memory location exist and
changing it has no ill effect and everything work. If less lucky, the
memory is not valid and Windows will GPFs. If the memory happens to be
used by the system, anything can happen: resource no longer available,
freezing, booting...
inlining your fonction just move the memory location of rectptr in the
stack, so luck has provided with a not too bad memory location for
your RECT object.
Frederic LACHASSE (ECP 86)
CompuServe: 100530,2005
Internet: lachass@worldnet.net
Technical Details
Message-ID:
<46j2k7$hmm@aldebaran.sct.fr>Path:
arc1-ca.pugleaf.net!rain.fr!world-net!usenetReferences:
<EkWdp9K00iWPA3tmVz@andrew.cmu.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!rain.fr!world-net!usenet From: lachass@worldnet.net (Frederic LACHASSE) Newsgroups: cmu.comp.lang.c++,comp.lang.c++ Subject: Re: C++ WINDOWS QUESTION Date: Tue, 24 Oct 1995 14:53:18 GMT Organization: World-Net information exchange, Internet provider. Message-ID: <46j2k7$hmm@aldebaran.sct.fr> References: <EkWdp9K00iWPA3tmVz@andrew.cmu.edu> NNTP-Posting-Host: client36.sct.fr X-Newsreader: Forte Free Agent 1.0.82