Jump to content

[Solved] Destructors called on object creation?


Recommended Posts

I am trying a useless script just for giggles wherein the GUI and all of it's controls are self contained objects. However, when I first run the script the destructors for all of the controls are called once at startup and again at shutdown. I know that the destructors are supposed to be called once right? Any help will be appreciated.

There are two main classes: objCtrl and objGUI. All controls inherit from objCtrl.

The destructor for objGUI seems to work as intended.

Update: I think I got it. I was calling _AutoItObject_Create() in the objLabel and objButton functions. I removed those and now it works right!

Here is the script:

Example.au3

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=n
#AutoIt3Wrapper_Au3Check_Stop_OnWarning=y
#AutoIt3Wrapper_Au3Check_Parameters=-w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -d
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <AutoItObject.au3>

#include <objCtrl.au3>
#include <objLabel.au3>
#include <objButton.au3>
#include <objGUI.au3>

_AutoItObject_Startup()

Global $GUI = objGUI()
$GUI.Create( "Experiment" , 400 , 300 )
$GUI.OnEvent( -3 , "GUI_Exit" )

Global $label = objLabel( "Hey you!" , 20 , 30 , 100 , 30 )

Global $button = objButton( "Click Me!" , 10 , 100 , 100 , 100 )
$button.SetTip( "What are you staring at?" , "HEY" , 3 )
$button.OnEvent( "Hello" )

While 1
    Sleep( 100 )
WEnd

Func Hello()
    $label.SetPos( 0 , 0 )
    MsgBox(0,'',"hell0")
EndFunc

Func GUI_Exit()
    $label = 0
    $button = 0
    $GUI = 0
    Exit
EndFunc

objGUI.au3

#include-once

Func objGUI()
    Opt( "GUIOnEventMode" , 1 )

    Local $this = _AutoItObject_Class()

    $this.AddProperty( "" , $elscope_private )

    $this.AddProperty( "HWND" , $elscope_readonly , 0 )
    $this.AddProperty( "Title" , $elscope_readonly , '' )
    $this.AddProperty( "Width" , $elscope_readonly , -1 )
    $this.AddProperty( "Height" , $elscope_readonly , -1 )
    $this.AddProperty( "" , $elscope_readonly )

    $this.AddMethod( "Create" , "Gui_Create" )
    $this.AddMethod( "SetState" , "Gui_SetState" )
    $this.AddMethod( "OnEvent" , "Gui_OnEvent" )
    $this.AddMethod( "" , "Gui_" )

    $this.AddMethod( "" , "Gui_" , True )

    $this.AddDestructor( "GUI_Dtor" )

    Return $this.object
EndFunc

Func Gui_Create( $this , Const $title = '' , Const $width = -1 , Const $height = -1 )
    $this.Title = $title
    $this.Width = $width
    $this.Height = $height
    $this.HWND = Number( GUICreate( $this.Title , $this.Width , $this.Height ) )
    $this.SetState( @SW_SHOW )
    Return $this
EndFunc

Func GUI_OnEvent( $this , Const $specialID , Const $function )
    GUISetOnEvent( $specialID , $function , $this.HWND )
    Return $this
EndFunc

Func Gui_SetState( $this , Const $state )
    GUISetState( $state , $this.HWND )
    Return $this
EndFunc

Func GUI_Dtor( $this )
    GUIDelete( $this.HWND )
    Return $this
EndFunc

objCtrl.au3

#include-once

Func objCtrl()
    Local $this = _AutoItObject_Class()

    $this.AddProperty( "Handle" , $elscope_private , 0 )
    $this.AddProperty( "" , $elscope_private , '' )

    $this.AddProperty( "Data" , $elscope_readonly , '' )
    $this.AddProperty( "Left" , $elscope_readonly , -1 )
    $this.AddProperty( "Top" , $elscope_readonly , -1 )
    $this.AddProperty( "Width" , $elscope_readonly , -1)
    $this.AddProperty( "Height" , $elscope_readonly , -1 )
    $this.AddProperty( "" , $elscope_readonly , '' )

    $this.AddMethod( "SetData" , "Ctrl_SetData" )
    $this.AddMethod( "SetLeft" , "Ctrl_SetLeft" )
    $this.AddMethod( "SetTop" , "Ctrl_SetTop" )
    $this.AddMethod( "SetWidth" , "Ctrl_SetWidth" )
    $this.AddMethod( "SetHeight" , "Ctrl_SetHeight" )
    $this.AddMethod( "SetPos" , "Ctrl_SetPos" )
    $this.AddMethod( "SetHandle" , "Ctrl_SetHandle" )
    $this.AddMethod( "GetHandle" , "Ctrl_GetHandle" )
    $this.AddMethod( "SetTip" , "Ctrl_SetTip" )
    $this.AddMethod( "OnEvent" , "Ctrl_OnEvent" )
    $this.AddMethod( "SetBkColor" , "Ctrl_SetBkColor" )
    $this.AddMethod( "Button" , "Ctrl_Button" )
    $this.AddMethod( "Label" , "Ctrl_Label" )
    $this.AddMethod( "" , "Ctrl_" )

    $this.AddMethod( "" , "Ctrl_" , True )

    $this.AddDestructor( "Ctrl_Dtor" )

    Return $this.Object
EndFunc

Func Ctrl_SetPos( $this , Const $left = -1 , Const $top = -1 , Const $width = -1 , Const $height = -1 )
    $this.Left = $left
    $this.Top = $top
    $this.Width = $width
    $this.Height = $height
    GUICtrlSetPos( $this.Handle , $this.Left , $this.Top , $this.Width , $this.Height )
    Return $this
EndFunc

Func Ctrl_OnEvent( $this , Const $function )
    GUICtrlSetOnEvent( $this.Handle , $function )
    Return $this
EndFunc

Func Ctrl_SetData( $this , Const $data )
    $this.Data = $data
    GUICtrlSetData( $this.Handle , $this.Data )
    Return $this
EndFunc

Func Ctrl_SetLeft( $this , Const $left )
    $this.Left = $left
    Return $this
EndFunc

Func Ctrl_SetTop( $this , Const $top )
    $this.Top = $top
    Return $this
EndFunc

Func Ctrl_SetWidth( $this , Const $width )
    $this.Width = $width
    Return $this
EndFunc

Func Ctrl_SetHeight( $this , Const $height )
    $this.Height = $height
    Return $this
EndFunc

Func Ctrl_SetHandle( $this , Const $handle )
    $this.Handle = Number( $handle )
    Return $this
EndFunc

Func Ctrl_GetHandle( $this )
    Return $this.Handle
    Return $this
EndFunc

Func Ctrl_SetTip( $this , Const $tiptext , Const $title = Default , Const $icon = Default , Const $options = Default )
    GUICtrlSetTip( $this.Handle , $tiptext , $title , $icon , $options )
    Return $this
EndFunc

Func Ctrl_SetBkColor( $this , Const $color )
    GUICtrlSetBkColor( $this.Handle , $color )
    Return $this
EndFunc

Func Ctrl_Dtor( $this )
    MsgBox( 32 , $this.Handle , "Destructor is called now?" )
    GUICtrlDelete( $this.Handle )
    Return $this
EndFunc

objLabel.au3

#include-once

Func objLabel( Const $title = '' , Const $left = -1 , Const $top = -1 , Const $width = -1 , Const $height = -1 )
    Local $this = _AutoitObject_Create( objCtrl() )
    $this.SetLeft( $left )
    $this.SetTop( $top )
    $this.SetWidth( $width )
    $this.SetHeight( $height )
    $this.SetHandle( GUICtrlCreateLabel( $title , $this.Left , $this.Top , $this.Width , $this.Height ) )
    GUICtrlSetState( $this.GetHandle() , @SW_SHOW )
    Return $this
EndFunc

objButton.au3

#include-once

Func objButton( Const $title = '' , Const $left = -1 , Const $top = -1 , Const $width = -1 , Const $height = -1 )
    Local $this = _AutoitObject_Create( objCtrl() )
    $this.SetLeft( $left )
    $this.SetTop( $top )
    $this.SetWidth( $width )
    $this.SetHeight( $height )
    $this.SetHandle( GUICtrlCreateButton( $title , $this.Left , $this.Top , $this.Width , $this.Height ) )
    GUICtrlSetState( $this.GetHandle() , @SW_SHOW )
    Return $this
EndFunc
Edited by LaCastiglione
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...