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()
logger.setLevel(logging.DEBUG)

handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)

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())
    menu.Append(item)
    return item

class SampleApp(wx.adv.TaskBarIcon):
    def __init__(self):
        wx.adv.TaskBarIcon.__init__(self)
        self.set_icon(TRAY_ICON)
        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)
        menu.AppendSeparator()
        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):
        pass

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

    def on_exit(self, event):
        wx.CallAfter(self.Destroy) 

if __name__ == '__main__':
    logger.info('Starting App') # log 1
    app = wx.App()
    logger.info('App obj created') # log 2
    SampleApp()
    app.MainLoop()
    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()
1 Like

Hi,

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__) # -> 0.5.1.2 

EDIT PS.
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:

The windows build and locale are key to replicate the defect. On Windows 10 1809, with latest Python we can constantly replicate it, but not from 1903, 2004 or later editions.

Found this post:
A few more Windows locale-related remarks - Discuss wxPython

On following up, I did find few defects reported in Python and looks like the fix came from Windows UCRT team. But I did not find any official statement in any release notes.

just removing asctime from Log formatter gets the script working on Win 1809. Time formatting is a locale aware operation, and calling that after wx.app Init will crash.

Robin’s solution from here worked in my case.
wxPython4.1.1 Python3.8 locale wxAssertionError - Discuss wxPython

I’m not sure if its future proof, as it could break with Windows / Python changes

Related Python Bug links:
Issue 37945: [Windows] test_locale.TestMiscellaneous.test_getsetlocale_issue1813() fails - Python tracker

Issue 36792: [Windows] time: crash on formatting time with de_DE locale - Python tracker