Modify

Opened 3 years ago

Closed 3 years ago

#3930 closed Bug (No Bug)

DllStructs created in one scope gets dropped if chained with a function that returns a re-structured-by-pointer version of it

Reported by: AutoXenon Owned by:
Milestone: Component: AutoIt
Version: 3.3.16.1 Severity: None
Keywords: Cc:

Description

Original thread: https://www.autoitscript.com/forum/topic/209074-dllstructs-created-in-one-scope-gets-dropped-if-chained-with-a-function-that-returns-a-re-structured-by-pointer-version-of-it

GUICreate('')
Local $unreliable_data, $reliable_data, $intermediate_ref, $labelunreliable = GUICtrlCreateLabel('',0,0,400,25), $labelreliable = GUICtrlCreateLabel('',0,30,400,25)
GUISetState()
HotKeySet('{esc}',quit)
While True
      $unreliable_data = parseStruct(createStruct())  ; struct technically goes out of scope, this saves a "slave struct" but the master struct gets dropped when it goes out of parseStruct's scope
      $intermediate_ref = createStruct()              ; struct scope gets transferred, this is a "master struct"
      $reliable_data = parseStruct($intermediate_ref) ; a "slave struct" of a master struct that is scoped here
      Sleep(100)                                      ; wait some time for garbage to get written into the freed memory 
      GUICtrlSetData($labelunreliable,DllStructGetData($unreliable_data,'msg'))
      GUICtrlSetData($labelreliable,DllStructGetData($reliable_data,'msg'))
WEnd


Func createStruct()
     Local $struct = DllStructCreate('char[16]')
     DllStructSetData($struct,1,'hello world')
     Return $struct
EndFunc

Func parseStruct($struct)
     Return DllStructCreate('char msg[16]',DllStructGetPtr($struct))
EndFunc

Func quit()
     Exit
EndFunc

The question is, should create-by-pointer structs count as valid references? I can imagine that if it is change to be so, it might cause headaches with ad-hoc throwaway "lens" structs unintentionally preventing the memory from being freed if the programmer is expecting it to not count as valid references. I guess it's up to you to decide on which tradeoff to pick.

Attachments (0)

Change History (4)

comment:1 by J-Paul Mesnage, 3 years ago

Reporter: changed from anonymous to AutoXenon

comment:2 by J-Paul Mesnage, 3 years ago

In fact your concern come from the fact that parameter pass to parseStruct() is a temporary variable.
So how can you map a permanent area with the DllStructGetPtr() and get a valid result.
That the reason why you get such behavior.

I will close the ticket as No bug.

Cheers

comment:3 by anonymous, 3 years ago

Thought so too, in which case I think this pitfall should probably be stated in the documentation, that the lifetime of DllStructs pertains to the scope where it is allocated, and so DllStructCreate when supplying a pointer will not count as valid references.

comment:4 by J-Paul Mesnage, 3 years ago

Resolution: No Bug
Status: newclosed

Modify Ticket

Action
as closed The ticket will remain with no owner.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.