Sign in to follow this  
Followers 0
wraithdu

DllStruct and Array memory handling?

7 posts in this topic

I'm curious how memory is handled for DllStructs and Arrays. For instance, in most windows API functions where a structure is used, the user must create the structure and pass the pointer to the function. The user is responsible for dealing with memory allocation and freeing. In user created functions the same idea applies.

However in AutoIt it's a common practice (perhaps not a good one) to create local structures or arrays in a user function, then return the function or array. Now I thought AutoIt was supposed to destroy all local variables, including structures and arrays, when they go out of scope. So this technically shouldn't work. Is this the case? Or is AutoIt intelligently creating a copy of the array or structure when it is returned from a function, and freeing the original memory? Or does it not free the original memory only if it is returned?

I ask because this could be a huge memory leak if the local arrays and structures are not destroyed when going out of scope (and not a return value), and the user does not manually free them ($struct or $array = 0). I'd like to get a better understanding of how AutoIt handles these memory allocations / deallocations to prevent such memory leaks. Thanks!

Share this post


Link to post
Share on other sites



It's kind of a reference thing. Since the return value creates another reference to the data, it isn't destroyed.

Share this post


Link to post
Share on other sites

So is there some sort of garbage collector that runs at the end of a script which frees up all the allocated memory?

Share this post


Link to post
Share on other sites

Sort of.

Part of it is also that at the end of the program, Windows will try to free things that the program allocated locally.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

This is the beauty with using a language that using variants and dynamic memory allocation. You don't need to worry about how the memory is handled. I actually think it's impossible to create pure memory leaks in autoit (calling dll's that allocates memory doesn't count) since if you loose the ability to access the variable autoit removes it.

I'm only guessing here but it would be a fair guess that autoit store some kind of reference count for each dllstruct/array.

Edited by monoceres

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

Sort of.

Part of it is also that at the end of the program, Windows will try to free things that the program allocated locally.

Sort of not. AutoIt does not have a garbage collector. When an object is goes out of scope it's destroyed, full stop. There is no delayed/deferred/scheduled cleanup. A variable goes out of scope, it's gone. Return values are a special case, obviously.

monoceres said it best. You don't need to worry about memory so stop worrying about it. I find it a little insulting that you think there is even potential we'd overlook such a glaring logic hole there.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Thanks everyone.

@Valik

I didn't mean to imply any sort of error on the part of the devs. I was referring to user created memory leaks. They're easy enough to create from bad code in other languages, so I wanted to be sure and avoid any coding practices in AutoIt that might leak.

Edited by wraithdu

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