Skip to content

[Bug]: Apparent memory issue? #21788

Closed

Description

@joelfrederico

Summary

If you insert an exception here:

def __init__(self, canvas, num):
the code has a segfault on quitting. This likely means that there's either a memory leak or double-free or some other memory issue someplace.

Proposed fix

Insert an exception here:

def __init__(self, canvas, num):

Build against a debug version of python and track down the issue using a debugger.

Activity

  1. QuLogic commented on Dec 18, 2021

    @QuLogic
    Member

    The backtrace says it is crashing in [window close] in FigureManager_dealloc. In FigureManager_new, the self->window is allocated, and in FigureManager_init it is initialized using initWithContentRect. If we raise from __init__ immediately, then the super-init is not called and the window is not initialized. Presumably an uninitialized window cannot be closed, but I'm not sure how to check if it has been.

  2. joelfrederico commented on Dec 18, 2021

    @joelfrederico
    ContributorAuthor

    It seems like the real issue is a misuse/disuse of tp_alloc, tp_init, tp_finalize, and tp_dealloc.

    The window ought to be allocated in a custom tp_alloc. Currently, this is done in tp_init.

    The tp_init should call some init method on the window.

    The window should be closed in tp_finalize. This should be decoupled from releasing it. I think that can be done by setting isReleasedWhenClosed to false during tp_init.

    The window should be released in tp_dealloc.

    Is that all correct?

  3. added this to the v3.6.0 milestone on Dec 18, 2021
  4. tacaswell commented on Dec 18, 2021

    @tacaswell
    Member

    @joelfrederico I milestoned this as 3.6 as you did not label this as a regression, but if there is an easy fix to reduce segfaults, we can definitely backport it.

  5. modified the milestones: v3.6.0, v3.6.1 on Sep 14, 2022
  6. modified the milestones: v3.6.1, v3.6.2 on Oct 6, 2022
  7. modified the milestones: v3.6.2, v3.6.3 on Oct 27, 2022
  8. modified the milestones: v3.6.3, v3.7.0 on Jan 11, 2023
  9. tacaswell commented on Feb 9, 2023

    @tacaswell
    Member

    Checking, the state is current the same, however if I am reading this correctly we only have the issue if we raise in the __init__?

  10. github-actions commented on Apr 13, 2026

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  11. tacaswell commented on Apr 13, 2026

    @tacaswell
    Member

    Suspect that this is still an issue.

  12. iccir commented on May 30, 2026

    @iccir
    Contributor

    Presumably an uninitialized window cannot be closed, but I'm not sure how to check if it has been.

    I don't yet possess deep knowledge regarding the proper use of tp_alloc/tp_new/tp_init in the Python world.

    In the Objective-C world, an -init… method should always be called immediately after +alloc. Calling any other instance method on an object you just allocated before calling -init… is undefined behavior.123

    I believe that each tp_new callback should only call tp_alloc and not instantiate an uninitialized Objective-C object. Each tp_init callback should then create a fully initialized Objective-C object via an +alloc/-init… pair.

    Footnotes

    1. "But the resulting object is not yet usable." from Object Allocation ↩

    2. "You must use an init... method to complete the initialization process." from +alloc ↩

    3. "immediately" and "An init message is coupled with an alloc … message in the same line of code" from -init ↩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions