Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 569361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:17:13+00:00 2026-05-13T13:17:13+00:00

Background: I’m doing COM programming of National Instruments’ TestStand in Python. TestStand complains if

  • 0

Background: I’m doing COM programming of National Instruments’ TestStand in Python. TestStand complains if objects aren’t “released” properly (it pops up an “objects not released properly” debug dialog box). The way to release the TestStand COM objects in Python is to ensure all variables no longer contain the object—e.g. del() them, or set them to None. Or, as long as the variables are function local variables, the object is released as soon as the variable goes out of scope when the function ends.

Well, I’ve followed this rule in my program, and my program releases object properly as long as there are no exceptions. But if I get an exception, then I’m getting the “objects not released” message from TestStand. This seems to indicate that function local variables aren’t going out of scope normally, when an exception happens.

Here is a simplified code example:

class TestObject(object):
    def __init__(self, name):
        self.name = name
        print("Init " + self.name)
    def __del__(self):
        print("Del " + self.name)

def test_func(parameter):
    local_variable = parameter
    try:
        pass
#        raise Exception("Test exception")
    finally:
        pass
#        local_variable = None
#        parameter = None

outer_object = TestObject('outer_object')
try:
    inner_object = TestObject('inner_object')
    try:
        test_func(inner_object)
    finally:
        inner_object = None
finally:
    outer_object = None

When this runs as shown, it shows what I expect:

Init outer_object
Init inner_object
Del inner_object
Del outer_object

But if I uncomment the raise Exception... line, instead I get:

Init outer_object
Init inner_object
Del outer_object
Traceback (most recent call last):
...
Exception: Test exception
Del inner_object

The inner_object is deleted late due to the exception.

If I uncomment the lines that set both parameter and local_variable to None, then I get what I expect:

Init outer_object
Init inner_object
Del inner_object
Del outer_object
Traceback (most recent call last):
...
Exception: Test exception

So when exceptions happen in Python, what exactly happens to function local variables? Are they being saved somewhere so they don’t go out of scope as normal? What is “the right way” to control this behaviour?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:17:13+00:00Added an answer on May 13, 2026 at 1:17 pm

    Your exception-handling is probably creating reference loops by keeping references to frames. As the docs put it:

    Note Keeping references to frame
    objects, as found in the first element
    of the frame records these functions
    return [[NB: “these functions” here refers to
    some in module inspect, but the rest of the
    paragraph applies more widely!]]
    , can cause your program to
    create reference cycles. Once a
    reference cycle has been created, the
    lifespan of all objects which can be
    accessed from the objects which form
    the cycle can become much longer even
    if Python’s optional cycle detector is
    enabled. If such cycles must be
    created, it is important to ensure
    they are explicitly broken to avoid
    the delayed destruction of objects and
    increased memory consumption which
    occurs. Though the cycle detector will
    catch these, destruction of the frames
    (and local variables) can be made
    deterministic by removing the cycle in
    a finally clause. This is also
    important if the cycle detector was
    disabled when Python was compiled or
    using gc.disable(). For example:

    def handle_stackframe_without_leak():
        frame = inspect.currentframe()
        try:
            # do something with the frame
        finally:
            del frame
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 378k
  • Answers 378k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The ceil() function is implemented in the math library, libm.so.… May 14, 2026 at 9:06 pm
  • Editorial Team
    Editorial Team added an answer There is a time data type you can use (SQL… May 14, 2026 at 9:06 pm
  • Editorial Team
    Editorial Team added an answer How about something like this? private Dictionary<Type, Action<I>> _mapping =… May 14, 2026 at 9:06 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.