Jump to content

Bug in my code or a API memory leak ?


Recommended Posts

#include <WinAPIGdi.au3>
#include <WinAPIGdiDC.au3>
#include <WinAPIHObj.au3>
#include <WinAPISysWin.au3>
#include <WindowsConstants.au3>
Global $hWnd = WinGetHandle ("")
$WinPos = WinGetPos($hWnd)
$iStart_x = $WinPos[2]/2-0.5
$iStart_y = $WinPos[3]/2+11.5
$iLength = 20
$iWidth = 2

While 1
    $hDC = _WinAPI_GetWindowDC($hWnd);get desired window handle
    $hPen = _WinAPI_CreatePen($PS_SOLID, $iWidth, 0xFF)
    $o_Orig = _WinAPI_SelectObject($hDC, $hPen)

    _WinAPI_DrawLine($hDC, $iStart_x - $iLength, $iStart_y, $iStart_x - 2, $iStart_y) ; horizontal left
    _WinAPI_DrawLine($hDC, $iStart_x + $iLength, $iStart_y, $iStart_x + 2, $iStart_y) ; horizontal right
    _WinAPI_DrawLine($hDC, $iStart_x, $iStart_y - $iLength, $iStart_x, $iStart_y - 2) ; vertical up
    _WinAPI_DrawLine($hDC, $iStart_x, $iStart_y + $iLength, $iStart_x, $iStart_y + 2) ; vertical down
    _WinAPI_MoveTo($hDC, $iStart_x, $iStart_y + $iLength)
    _WinAPI_LineTo($hDC, $iStart_x, $iStart_y + 2)
    if WinExists ($hWnd) = 0 Then ExitLoop
WEnd
    ; clear resources
    _WinAPI_RedrawWindow($hWnd, 0, 0, $RDW_INVALIDATE + $RDW_ALLCHILDREN)
    _WinAPI_SelectObject($hDC, $o_Orig)
    _WinAPI_DeleteObject($hPen)
    _WinAPI_ReleaseDC(0, $hDC)

 

I got this code from help file. Modified it a little bit because i want it in a loop. That way cross is drawn none stop rather then show up for quarter of a second.

When inl loop, memory consumption in creases by 4kb per 250ms (i think) and once it goes over 512kb (or something like that) cross no longer drawn.
So memory consumption increase is a memory leak in my opinion but i dont know where.
The fact that cross no longer drawn after so much memory has leaked, its a bug then ?

 

What do u think ?
I am new to winapi and the whole draw with api stuff so i am not very knowledgeable here.

 

 

 

 

 

Link to comment
Share on other sites

  • Developers

You are calling 

$hDC = _WinAPI_GetWindowDC($hWnd);get desired window handle

each loop without doing a _WinAPI_ReleaseDC() function to release the device context.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Thanks Jos.

Is there any reason why cross wont draw over some windows that use GPU ?
This script draws red cross in the exact same program on my laptop but on desktop it wont.

I am trying to draw on top of a program that renders 3d graphics.
Could that be video card related issue ? Laptop and my home desktop have two different video cards but same brand.
Not sure why it wont work on home desktop computer.

Link to comment
Share on other sites

22 hours ago, Jos said:

You are calling 


$hDC = _WinAPI_GetWindowDC($hWnd);get desired window handle

each loop without doing a _WinAPI_ReleaseDC() function to release the device context.

Jos

So taking $hDC outside the loop would resolve it ?

#include <WinAPIGdi.au3>
#include <WinAPIGdiDC.au3>
#include <WinAPIHObj.au3>
#include <WinAPISysWin.au3>
#include <WindowsConstants.au3>
Global $hWnd = WinGetHandle ("")
$WinPos = WinGetPos($hWnd)
$iStart_x = $WinPos[2]/2-0.5
$iStart_y = $WinPos[3]/2+11.5
$iLength = 20
$iWidth = 2
$hDC = _WinAPI_GetWindowDC($hWnd);get desired window handle
While 1
    $hPen = _WinAPI_CreatePen($PS_SOLID, $iWidth, 0xFF)
    $o_Orig = _WinAPI_SelectObject($hDC, $hPen)

    _WinAPI_DrawLine($hDC, $iStart_x - $iLength, $iStart_y, $iStart_x - 2, $iStart_y) ; horizontal left
    _WinAPI_DrawLine($hDC, $iStart_x + $iLength, $iStart_y, $iStart_x + 2, $iStart_y) ; horizontal right
    _WinAPI_DrawLine($hDC, $iStart_x, $iStart_y - $iLength, $iStart_x, $iStart_y - 2) ; vertical up
    _WinAPI_DrawLine($hDC, $iStart_x, $iStart_y + $iLength, $iStart_x, $iStart_y + 2) ; vertical down
    _WinAPI_MoveTo($hDC, $iStart_x, $iStart_y + $iLength)
    _WinAPI_LineTo($hDC, $iStart_x, $iStart_y + 2)
    if WinExists ($hWnd) = 0 Then ExitLoop
WEnd
    ; clear resources
    _WinAPI_RedrawWindow($hWnd, 0, 0, $RDW_INVALIDATE + $RDW_ALLCHILDREN)
    _WinAPI_SelectObject($hDC, $o_Orig)
    _WinAPI_DeleteObject($hPen)
    _WinAPI_ReleaseDC(0, $hDC)

Still does not work..

Cross goes away second later.

Link to comment
Share on other sites

  • Developers
1 hour ago, tonycst said:

Still does not work..

Cross goes away second later.

I was referring to your Memory Leak question caused by $hDC = _WinAPI_GetWindowDC().

By the way: I see you are still doing a similar thing with : $hPen = _WinAPI_CreatePen()  without  _WinAPI_DeleteObject($hPenin the closeloop

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

So i need to delete it each time its drawn ?

_WinAPI_SelectObject($hDC, $o_Orig)
    _WinAPI_DeleteObject($hPen)

Putting these into the loop does not help. It still stops drawing after a second.

Link to comment
Share on other sites

  • Developers
2 minutes ago, tonycst said:

So i need to delete it each time its drawn ?

or only create it one time?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

But if windows is moved, how do i go about doing that ?

Does not matter what i do.

Even without the loop, its drawn for one second and BOOM, its gone. I suppose you never tried to run the script.

Link to comment
Share on other sites

Have a look here:

Also gdi in this manner will not persist past a screen redraw 

So if you move a window the screen redraws and you are expected by windows to redraw your gdi data

 

There are some examples in this thread saving a bitmap of the current screen in order to redraw (by UEZ)

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...