Sign in to follow this  
Followers 0
Anas

Memory Leak in $STDOUT_CHILD

14 posts in this topic

Hello everyone,

I've been experiencing a memory leak in one of my scripts, and I've traced it back to $STDOUT_CHILD.

After some research, I found some old >topics (2007) about it, which indicates it's already solved, but for some reason, I still have a memory leak..

This is a (faster?) version of Valik's example, I think it meets the three conditions mentioned in the Help file, also, according to taskmgr.exe, the handles count is not continuously rising:

HotKeySet('{ESC}', 'Quit')

While 1
    $PID = Run('ping -t 0.0.0.0', '', @SW_HIDE, 2)
    Sleep(50)
    StdioClose($PID)
    StdoutRead($PID)
    ProcessClose($PID)
    Sleep(50)
    StdoutRead($PID)
    StdioClose($PID)
    Sleep(10)
WEnd

Func Quit()
    ProcessClose($PID)
    Exit
EndFunc

Am I missing something here?

Share this post


Link to post
Share on other sites



Which version of AutoIt do you run? Which operating system? Bitness (32/64 bit)?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

AutoIT v3.3.8.1

Windows 7 Ultimate x64 (but I'm running the script in x86)

is it running normally in your system?

Share this post


Link to post
Share on other sites

What is the purpose of this script going to be? Because you're using StdioClose($PID) you're not going to be able to read the IOStream using StdoutRead($PID) more than once. You're also killing the process then trying to read the stream, after the process isn't there and after you've closed the stream. I don't get the logic flow of this scriptlet.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

The only purpose of this example is to test and confirm the memory leak. (reading the topic I linked earlier might make things more clear)

I've added the StdioClose/StdoutRead twice before/after the proccess have been started/closed (for atleast 50ms) to make sure the process/handle is created/Closed normally, and that I've met the first/second conditions (Run function in Help file).

Although I forgot the "-t" flag in the run command.

Share this post


Link to post
Share on other sites

While 1
    $PID = Run('ping -t 0.0.0.0', '', @SW_HIDE, 2)
    Sleep(50)
    ;StdioClose($PID)
    StdoutRead($PID)
    ProcessWaitClose($PID)
    Sleep(50)
    ;StdoutRead($PID)
    ;StdioClose($PID)
    ;Sleep(10)
WEnd


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Hello JohnOne

Your example have the same problem, and it also doesn't close the handles. (btw, you forgot to remove the "-t" flag, which is why it worked for you)
besides, it won't work if the process is not going to end by it self.

Share this post


Link to post
Share on other sites

JohnOne's script, with the -t removed, doesn't cause any kind of memory increases as it's running. It goes up briefly when it runs Ping, but when that stops and after reading the stream, it goes right back down to what it was before.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Hello BrewManNH

How long have you kept it running? you have to give it a few moments since the full pings takes more time to finish.
After a few moments (In my system atleast) it tripled the handles, and it memory usage increased by almost a third.

Share this post


Link to post
Share on other sites

Left it for about 5 minutes without memory leak.

But there is something strange about it though, while trying it like this

While 1
    $PID = Run('ping 127.0.0.1', @SystemDir, @SW_HIDE, 8)
    ConsoleWrite("Run Error: " & @error & @LF)
    While ProcessExists($PID)
        Sleep(10)
    WEnd
    $Read = StdoutRead($PID)
    ;StderrRead($PID)
    ConsoleWrite($Read & @LF)
    StdioClose($PID)
    ProcessClose($PID)
    ProcessWaitClose($PID)

    Sleep(50)
    ;StdoutRead($PID)
    ;StdioClose($PID)
    ;Sleep(10)
WEnd

Func Quit()
    ProcessClose($PID)
    Exit
EndFunc   ;==>Quit

It is erratic, in that it works sometimes and not others, sometimes it will get through 1, 2, 3 or 4 loops and sometimes 0.

It gets stuck where the process still exists in the inner while loop.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

If your replace your Run line with the "Dir C:" command instead of "Ping", what do you see happening? It's not AutoIt, it's because you're opening handles with Ping that it doesn't appear to be closing correctly.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Left it for about 5 minutes without memory leak.

But there is something strange about it though, while trying it like this

While 1
    $PID = Run('ping 127.0.0.1', @SystemDir, @SW_HIDE, 8)
    ConsoleWrite("Run Error: " & @error & @LF)
    While ProcessExists($PID)
        Sleep(10)
    WEnd
    $Read = StdoutRead($PID)
    ;StderrRead($PID)
    ConsoleWrite($Read & @LF)
    StdioClose($PID)
    ProcessClose($PID)
    ProcessWaitClose($PID)

    Sleep(50)
    ;StdoutRead($PID)
    ;StdioClose($PID)
    ;Sleep(10)
WEnd

Func Quit()
    ProcessClose($PID)
    Exit
EndFunc   ;==>Quit

It is erratic, in that it works sometimes and not others, sometimes it will get through 1, 2, 3 or 4 loops and sometimes 0.

It gets stuck where the process still exists in the inner while loop.

 

Your last example works fine, but it doesn't explain why though, anyway, I have to be able to close the process my self and not wait for it.

and removing the inner loop gets it stuck..

If your replace your Run line with the "Dir C:" command instead of "Ping", what do you see happening? It's not AutoIt, it's because you're opening handles with Ping that it doesn't appear to be closing correctly.

You are right, but I'm sure (or was ..) it use to work just fine...

 

just to make sure it's not a problem in my OS, did any of you get the same results as I did (using my First example)? and what is your OS?

if yes, then how should I handle this?

 

Thank you for your help and time.

Share this post


Link to post
Share on other sites

I let it run for about 20 minutes and noticed the handles count going up, the memory used went up but slowly. Swapping out Ping for Dir and that problem went away completely. The problem is Ping, not AutoIt.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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