tonycst

Using function without variable GOOD or BAD ?

8 posts in this topic

Help file says:

If a handle is returned, it must be released using _WinAPI_DeleteObject().

and gives example:

_ScreenCapture_Capture(@MyDocumentsDir & "\GDIPlus_Image1.jpg")

which makes no sense because there is no variable for the function and if you look at the complete example code, there is no _WinAPI_DeleteObject(). either.

Does that mean that if no variable is used to call one or another function, there is no need to worry about memory leaks ? (or clutter)

Related question: Does memory get cleared of all data once script exists  or it need to be cleared ?

 

thank ya

Share this post


Link to post
Share on other sites



Your 1st question doesn't make a lot of sense. If you use _ScreenCapture_Capture with a file name as the first parameter, then you won't get a handle returned, you get True if the image is saved ok.

No where does it say if you use a variable that you need to use _WinAPI_DeleteObject.

You're asking too broad a question and using a single function as an example for the question to which it doesn't apply to at all.

Most of the time when the script closes it clears its data, but not always in some cases, so it's best to clear them yourself prior to exiting.


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
11 hours ago, tonycst said:

which makes no sense because there is no variable for the function and if you look at the complete example code, there is no _WinAPI_DeleteObject(). either.

Using _ScreenCapture_Capture() depending on the first param you get : either a handle (and then you must be release it using _WinAPI_DeleteObject ) or a file (and then the used variables are released internally by the UDF )

Share this post


Link to post
Share on other sites

Oh ok i get it now.

I thought variable before _ScreenCapture_Capture is the handle.

I guess variable is a handle storage ?

Does that mean i should not use _ScreenCapture_Capture(@DesktopDir & "\Shapshot.jpg") without variable so that i can delete the object once picture is saved, correct ?

Thanks :)

Share this post


Link to post
Share on other sites

Per the help file:
 

Return Value

Returns a handle to an HBITMAP if $sFileName is empty.
    If called with $sFileName then the image is saved to the file and nothing is returned.

Remarks

If a handle is returned, it must be released using _WinAPI_DeleteObject().

 

So, if you do specify a filename to output the image to, nothing gets returned.  
If you don't specify a filename, an HBITMAP object handle is returned and therefore should be released when finished with using _WinAPI_DeleteObject

 

 

If you look at the ScreenCapture.au3 UDF, you'll see at the end of the _ScreenCapture_Capture() function it evaluates if $sFilename = "".

  • If $sFileName = "" Then Return handle else 
  • Continue script and provide the bitmap object as a parameter to the _ScreenCapture_SaveImage and set $bFreeBmp paramter to true (which executes _WinAPI_DeleteObject after saving the file).

So either way, with cleanup is (should be) done.

Share this post


Link to post
Share on other sites

one day ill understand all of what you said.

thanks !

Share this post


Link to post
Share on other sites

Let me take a small step back to make sure something fundamental is understood;  what is meant by a return?

When you execute a function, it typically "returns" something (i.e. value, string, array, etc.).  The variable placed in front of a function--i.e. $iMsg = Msgbox(...)--will be assigned what the function returns. 

Now, after that..does re-reading my post above help explain any better?

Share this post


Link to post
Share on other sites

i got that part yes.

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