Heap corruption on Windows

Hi all,
My sample application crashes with “Heap corruption” error on a few windows machines. The same app works fine on a different Windows 10 version with the same Python and wxpython package. On debugging I figured out, my logger (Python’s logging library) is getting corrupted somehow on the machines where this is seen.

OS - Windows 10 French (1809 - build: 17763) - works on other windows versions
wxPython - 4.1.1 (pypi), Python 3.9.7 (stock)

Need some directions on further debugging this issue.

import wx
import wx.adv
import logging
import sys

logger = logging.getLogger()

handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')

TRAY_ICON = "path to some icon" # replace with path to a icon file. Not necessary to get the code working

def create_menu_item(menu, label, func):
    item = wx.MenuItem(menu, -1, label)
    menu.Bind(wx.EVT_MENU, func, id=item.GetId())
    return item

class SampleApp(wx.adv.TaskBarIcon):
    def __init__(self):
        self.Bind(wx.adv.EVT_TASKBAR_RIGHT_DOWN, self.on_right_down)

    def CreatePopupMenu(self):
        menu = wx.Menu()
        create_menu_item(menu, 'Say Hello', self.on_hello)
        create_menu_item(menu, 'Exit', self.on_exit)
        return menu

    def set_icon(self, path):
        icon = wx.Icon(wx.Bitmap(path))
        self.SetIcon(icon, 'hello world!!!')

    def on_right_down(self, event):

    def on_hello(self, event):
        print ('Hello, world!')

    def on_exit(self, event):

if __name__ == '__main__':
    logger.info('Starting App') # log 1
    app = wx.App()
    logger.info('App obj created') # log 2
    logger.info('App closed') # log 3

The loggers fail with heap corruption after calling wx.App(). As App is not yet started, log 2 still gets called from the python’s main thread. Not sure how this gets corrupted in few machines, and works just fine else where.

Commenting out log 2,3 will get the app working on those windows were it is failing.

You need to keep a reference to your TaskBarIcon object, to ensure it doesn’t get garbage-collected at an inopportune time. Just assign it to a variable when you create it:

    tbicon = SampleApp()


Your sample code works on my PC.
Python 3.8.6
wx.version 4.1.1
Windows10 20H2 64 bit

So, I have no idea but think firstly you should make sure that the versions are exactly the same and that it really is what you intended, e.g.,

print(sys.version) # -> 3.8.6
print(wx.__version__) # -> 4.1.1
print(logging.__version__) # -> 

I sometimes get caught in ‘shebang trap’. I have several versions of Python on my PC, and if the program is launched as $ py xxx.py and the shebang being written as #! python, then PY2 will be launched unintentionally, even if the default launcher is PY3.
To avoid this on Windows, we have to write the shebang as #! python3 or launch with version switch such as $ py -3.8 xxx.py.

PS2. Otherwise, the French windows must be too open. :wine_glass: