Jump to content

Memory Leak in $STDOUT_CHILD


Recommended Posts

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?

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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

Link to comment
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.

Link to comment
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.

Link to comment
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.

Link to comment
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

Link to comment
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.

Link to comment
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.

Link to comment
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

Link to comment
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.

Link to comment
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

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...