Sign in to follow this  
Followers 0
unity06

Memory Leak - Help Please

11 posts in this topic

I have been using the code below to take screen shots of various pages at time intervals.

My code is now fully working, but this function is eating memory very quickly.

Can someone please help me identify the memory leak?

I have tested my full function as it also downloads various images and only this part of the function causes memory problems

So I have narrowed down the problem

The code causing the problem is below:

Func _WebCapture($Url, $WebWidth = 1024)

    Local $oIE = ObjCreate("Shell.Explorer.2")
    Local $hGUI = GUICreate("", $WebWidth, 768, -1, -1)
    Local $cObj = GUICtrlCreateObj($oIE, 0, 0, $WebWidth, 768)

    _IELoadWaitTimeout(20000) ; 20 sec
    _IENavigate($oIE, $Url)
    _IELoadWait($oIE)

    Local $oDocument = $oIE.document
    Local $oBody = $oIE.document.body
    Local $oHtml = $oIE.document.documentElement

    $oBody.scroll = "no"
    $oBody.style.borderStyle = "none"
    $oHtml.style.overflow = 'hidden'
    $oBody.style.overflow = 'hidden'

    Local $oIViewObject2 = ObjCreateInterface($oDocument, "{00000127-0000-0000-C000-000000000046}", "Draw hresult(dword;long;ptr;ptr;handle;handle;struct*;struct*;bool;ulong_ptr);")

    If @error Then Return SetError(1, 0, 0)

    Local $BodyWidth = $oBody.scrollWidth
    Local $BodyHeight = $oBody.scrollHeight
    Local $RootWidth = $oHtml.scrollWidth
    Local $RootHeight = $oHtml.scrollHeight

    Local $Width = $BodyWidth
    Local $Height = $RootHeight
    If $BodyHeight > $Height Then $Height = $BodyHeight

    $oIE.width = $Width
    $oIE.height = $Height

    Local $hDC = _WinAPI_GetDC(0)
    Local $hMemDC = _WinAPI_CreateCompatibleDC($hDC)
    Local $hBmp = _WinAPI_CreateCompatibleBitmap($hDC, $Width, $Height)
    _WinAPI_SelectObject($hMemDC, $hBmp)

    Local $sRECT = DllStructCreate($tagRECT)
    DllStructSetData($sRECT, "Top", 0)
    DllStructSetData($sRECT, "Left", 0)
    DllStructSetData($sRECT, "Right", $Width)
    DllStructSetData($sRECT, "Bottom", $Height)

    $oIViewObject2.Draw(1, -1, 0, 0, $hDC, $hMemDC, $sRECT, 0, 0, 0)

    _WinAPI_DeleteDC($hMemDC)
    _WinAPI_ReleaseDC(0, $hDC)

    GUIDelete($hGUI)

    Return $hBmp
EndFunc   ;==>_WebCapture

Share this post


Link to post
Share on other sites



unity06,

Welcome to the AutoIt forum. :)

Try deleting the bitmap and the object you create as well as the DC. ;)

M23

P.S. I have deleted your other post as it contained absolutely no useful information at all. Nice to see that you gave some this time! :)


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

It's always good to include the source of the code you are posting, so folks don't think you are trying to pass the work off as your own.

Also, in this case, there is information on clean-up in the thread this code was taken from.

Share this post


Link to post
Share on other sites

Hi

Sorry if I am not following proper forum etiquette, I am on these forums the first time.

I have studied the function and from what I can see the memory resources should be freed by the few lines of code at the end of the function

_WinAPI_DeleteDC($hMemDC)
    _WinAPI_ReleaseDC(0, $hDC)

    GUIDelete($hGUI)

But they are not and the memory only increases, very rapidly

Share this post


Link to post
Share on other sites

unity06,

So did you try mu suggestion of deleting the 2 other things you create as well as the DC? :huh:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Melba23,

I have tried, but I am not certain if I am doing it right

As this script was created by another person

I can understand the general principles of how it works, but not fully

It is not a too complex script, but I am faily new to Scite / Autoit and may not be doing it right

can you please suggest with which commands to delete all the bitmap and object as well as the DC?

I am so close to finishing the project, everything works, only the memory leak is the issue now

Share this post


Link to post
Share on other sites

unity06,

Then here is a chance for to learn something. :)

You can see the lines where you create the bitmap and the object:

Local $hBmp = _WinAPI_CreateCompatibleBitmap($hDC, $Width, $Height)
_WinAPI_SelectObject($hMemDC, $hBmp)
Look in the Help file for the first of those functions and see what it tells you to do when you have finished with it.  As for the second - take a guess at what the function name to delete an object might be called and see if you can find it in the Help file. ;)

Come back if you get stuck. :)

 

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Melba thanks for the hint,

I have now added the lines of code which deletes the object after it is no longer needed

This line deletes the object

_WinAPI_DeleteObject($hBmp)

The below lines take care of DC

_WinAPI_DeleteDC($hMemDC)
_WinAPI_ReleaseDC(0, $hDC)

But the lines are not solving the problem still :(

I have analyzed the code line by line now while looking at memory usage window and found that the memory jump occurs at this point of the code where it says

_IENavigate($oIE, $Url)

It may be the case that it opens all the "virtual windows" but does not close them.to deal with it I attempted to add this line of code at the end of the function too but it did not help

_IEQuit($oIE)

Share this post


Link to post
Share on other sites

Any difference when adding:

GUICtrlDelete($cObj)
$oIViewObject2 = 0
$oIE = 0

I know that dev trancexx said it should be taken care of, but I'm wondering if you see any difference.

Share this post


Link to post
Share on other sites

I had those 3 lines added from the start, but no, does not make any difference

perhaps the original dev did not use this function in a loop to download many screenshots like I am doing so the testing was limited.

So I am stuck really as I now know which line of code creates the massive jump in memory which is:

_IENavigate($oIE, $Url)

But how to free that memory after every loop?

Share this post


Link to post
Share on other sites

Any difference if you only create the IE object once, outside the loop, then close it once all of the looping is done?

Share this post


Link to post
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
Sign in to follow this  
Followers 0