I'm building a windows aplication in Go using pure win32 api functions. My application suddenly hangs or gets stuck out of nowhere randomly. No error message or panic from go debugger. And the worst part is the problem is not appearing all the time but occasionally. I tried to put some 'log.Println' here and there in my program to trace exactly where it's getting stuck at and it appeared it's getting stuck at random places at random function calls but mostly, it's getting stuck at win32 api function calls like 'DefWindowProc' or 'GetMessage'. Over time my project got bigger and the more it got bigger the more often the problem seems to appear. So I tried to minimize the codes, simplify and comment out codes as much possible to see if the problem still occurs and I don't know it isn't helping much cause the problem just occurring randomly and my mind is a mess now.
I can just post codes from my original program but I'm not sure if those would help or not. I can show them if required. Now I've discovered something else which may or may not be related to my original problem. I was thinking if the garbage collector causing it since it gets called randomly if I'm doing something that is not GC friendly. I really don't know but here is this simple code. I never called the GC manually in my original application codes but here I'm doing it for test purpose.
package main
import (
"log"
"runtime"
"syscall"
"github.com/AllenDang/w32"
)
func main() {
w32.CreateWindowEx(
0, syscall.StringToUTF16Ptr("Button"), syscall.StringToUTF16Ptr("Hello World!"),
w32.WS_OVERLAPPEDWINDOW|w32.WS_VISIBLE,
100, 100, 1200, 800, 0, 0, 0, nil)
runtime.GC()
var msg w32.MSG
for {
log.Println("Calling 'GetMessage'")
if w32.GetMessage(&msg, 0, 0, 0) == 0 {
break
}
log.Println("End Calling 'GetMessage'")
w32.TranslateMessage(&msg)
w32.DispatchMessage(&msg)
}
return
}
The program doesn't go past the 'GetMessage' it gets stuck there. But if I comment out the 'runtime.GC()' then it does work perfectly. I'm coming from C++ and new to Go. I don't know much about how Go language's garbage collector works and I don't seem find any garbage that's getting collected there in those codes. Please help me out I would greatly appreciate it. Thanks in advance.
CreateWindowExcall returns a value. Perhaps that call created some data which needs to persist for the lifetime of the object?w32package is being used correctly and yet is effected by GC, that is a bug in thew32package.runtime.LockOSThreadruntime.LockOSThreadsolved my problem. Thanks a lot! :D