This error only appears for small timer values. Testing has shown that if the creation and destruction of objects are batched, that is, for large timer values the creation happens all at once and destruction happens all at once, the bug does not appear. When creation and destruction overlap, that is when the critical error Invalid memory read/write appears.
---------------------------
test9.ahk
---------------------------
Critical Error: Invalid memory read/write.
Line#
015: Return,this
016: }
018: {
019: FileAppend,"
" &this " 2. __Delete",log.txt
020: }
022: {
023: FileAppend,"
" &this " 1. Blank",log.txt
---> 024: Return
025: }
027: Exit
028: Exit
028: Exit
The program is now unstable and will exit.
---------------------------
OK
---------------------------
Test code: This is as small as I am able to reduce the code.
Code: Select all
SetBatchLines -1
FileDelete log.txt
Loop 10000 {
; Creates an object and immediately discards it.
new TestObj(100) ; If 10000 the error does not appear.
}
MsgBox done
class TestObj {
__New(time) {
; The reference to the object is held by SetTimer, and its lifespan is time.
if (time > 0) {
blank := ObjBindMethod(this, "blank") ; Calls Blank()
SetTimer % blank, % -time ; Calls __Delete.
}
return this
}
__Delete() {
FileAppend , % "`n" &this " 2. __Delete", log.txt
}
Blank() {
FileAppend , % "`n" &this " 1. Blank", log.txt
return
}
}