Sign in to follow this  
Followers 0
PaulIA

DllStructCreate and Scope

12 posts in this topic

I have seen UDFs posted where people use DllStructCreate inside of a function and do not release the allocted memory before the function exits (nor release it later on in their scripts). I checked the help and it says:

Variables created inside functions are automatically destroyed when the function ends.

But I wouldn't classify the memory allocated by DllStructCreate as a variable. I've always freed structures in my functions before exit(and will continue to do so), but I was curious as to whether the memory allocated by DllStructCreate is released when the function exits or not.

Auto3Lib: A library of over 1200 functions for AutoIt

Share this post


Link to post
Share on other sites



The struct is "stored in a variable" so of course it's freed when the variable is destroyed.

Share this post


Link to post
Share on other sites

Sorry about all this, but I'm trying to clarify some things that popped into my brain as I was writing Auto3Lib. I appreciate your patience...

In another thread I asked why the memory created by DllStructCreate did not get initialized and got the reply:

All DllStruct stuff does is allocate raw memory and leaves it to the user to describe how to use this memory. That's not really the same thing as creating a variable.

So I took it from that thread (and the help file) that the memory allocated by DllStructCreate needed to be manually released since it was not the same thing as creating a variable. Bad assumption. From what you're saying, the memory created by DllStructCreate is freed when it goes out of scope like a normal variable, it's just not initialized on creation like a normal variable is.

So, that said, I can't create a function like this:

Func _InitStruct()
  Local $rStruct
  $rStruct = DllStructCreate("int")
  ...
  do something with $rStruct
  ...
  Return $rStruct
EndFunc

Because the memory allocated to $rStruct inside the function will be released when $rStruct goes out of scope. But I could do something like this:

Func _InitStruct(ByRef $rStruct)
  $rStruct = DllStructCreate("int")
  ...
  do something with $rStruct
  ...
EndFunc

Because I'm passing $rStruct by reference. Am I way off base here?


Auto3Lib: A library of over 1200 functions for AutoIt

Share this post


Link to post
Share on other sites

You're making this way more complicated than it is. Of course you can return the structure. Again, it would be stupid if you couldn't. It's not going to leak and it's not going to be pre-maturely destroyed. The implementation is probably reference counted.

Share this post


Link to post
Share on other sites

Uh... sorry. :lmao: It appeared that I was getting conflicting information on this and I was just trying to straighten it out in my head. I'll try not to ask stupid questions in the future.


Auto3Lib: A library of over 1200 functions for AutoIt

Share this post


Link to post
Share on other sites

I always did explicit destroy by $var = 0 (because I didn't know about this implicit free memory as Valik said), but this example is case when it needn't be done:

there is no memory alocation when you pass second parameter to DllStructCreate()

#include <GUIConstants.au3>
Const $WM_GETMINMAXINFO = 0x24

GUICreate("Test GUI",500,500,-1,-1,BitOR($GUI_SS_DEFAULT_GUI,$WS_SIZEBOX))
GUISetState (@SW_SHOW)
GUIRegisterMsg($WM_GETMINMAXINFO, "MY_WM_GETMINMAXINFO")

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
Wend
    
Func MY_WM_GETMINMAXINFO($hWnd, $Msg, $wParam, $lParam)
    $minmaxinfo = DllStructCreate("int;int;int;int;int;int;int;int;int;int",$lParam)
    DllStructSetData($minmaxinfo,7,400) ; min X
    DllStructSetData($minmaxinfo,8,250) ; min Y
    DllStructSetData($minmaxinfo,9,600) ; max X
    DllStructSetData($minmaxinfo,10,700) ; max Y
    Return 0
EndFunc

Share this post


Link to post
Share on other sites

@Zedna: Thanks for the tip and example. I was wondering about the exact implementation of the pointer function and your example answered it for me. Nice job.


Auto3Lib: A library of over 1200 functions for AutoIt

Share this post


Link to post
Share on other sites

It appeared that I was getting conflicting information on this and I was just trying to straighten it out in my head. I'll try not to ask stupid questions in the future.

The question was not stupid, methinks. I was wondering about that as well, especially after the "raw memory" comment.

It would be helpful if those in the know would try to answer such questions in a consistent manner instead of confusing us poor users who have no access to the source (because this is exactly the sort of question easily answered by a quick peek into the source).

YMMV.

Share this post


Link to post
Share on other sites

Perhaps instead of asking questions, you should do simple tests. I dare-say you can write a test that would determine the answer far faster than it would take to look up the answer in a code-base you are not familiar with. To address this particular question, the following code would suffice.

Global $vReturn = Test()
MsgBox(4096, "", "Outside function:" & @CRLF & DllStructGetData($vReturn, 1))


Func Test()
    Local $vStruct = DllStructCreate("char[256]")
    DllStructSetData($vStruct, 1, "Hello World!")
    MsgBox(4096, "", "Inside function:" & @CRLF & DllStructGetData($vStruct, 1))
    Return $vStruct
EndFunc

I don't think that simple test is beyond the capabilities of either of you.

Share this post


Link to post
Share on other sites

Actually Valik, I did write my own test script before posting. However, it had a bug that was returning the wrong results. That and the comments that I got from you about DllStructCreate emmory lead me to a conclusion that I now know was wrong. After that, I checked the help file and the forum for similar issues before posting.

So I screwed up. I've also apologized to you (more than once) for the mistake. If you were in my shoes, how would you feel right now? Think you'd want to do more AutoIt development, learn more by asking questions, share ideas with others? If you do you've got thicker skin than I've got.


Auto3Lib: A library of over 1200 functions for AutoIt

Share this post


Link to post
Share on other sites

My comment wasn't directed at you so much as thomasl although I did include you with the last sentence since I do think you are competent enough to come up with your own example.

If I were in your shoes, I wouldn't have asked the question. Seldom do I ask questions and I was that way even when learning the language(s) so that's not a by-product of knowing now what I did not know then. I imagine my skin is thicker as well. One has to have thick skin if they are going to be a successful asshole.

Share this post


Link to post
Share on other sites

One has to have thick skin if they are going to be a successful asshole.

Ok, now I'm laughing my ass off! :lmao: Truce...

Auto3Lib: A library of over 1200 functions for AutoIt

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