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 8603319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:17:45+00:00 2026-06-12T02:17:45+00:00

I was wondering is there any way to create a dangling pointers in python?

  • 0

I was wondering is there any way to create a dangling pointers in python? I guess we have to manually delete an object for example and then the reference of that object will point at a location that has no meaning for the program.
I found this example Here

import weakref
class Object:
    pass

o = Object()    #new instance
print ("o id is:",id(o))
r = weakref.ref(o)
print ("r id is:",id(r))
o2 = r()
print ("o2 id is:",id(o2))
print ("r() id is:",id(r()))
print (o is o2)         

del o,o2
print (r(),r)   #If the referent no longer exists, calling the reference object returns None

o = r()         # r is a weak reference object
if o is None:
    # referent has been garbage collected
    print ("Object has been deallocated; can't frobnicate.")
else:
    print ("Object is still live!")
    o.do_something_useful()

In this example which one is the dangling pointer/reference? Is it o or r? I am confused.
Is it also possible to create dangling pointers in stack? If you please, give me some simple examples so i can understand how it goes.
Thanks in advance.

  • 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-06-12T02:17:46+00:00Added an answer on June 12, 2026 at 2:17 am

    If you create a weak reference, it becomes “dangling” when the referenced object is deleted (when it’s reference count reaches zero, or is part of a closed cycle of objects not referenced by anything else). This is possible because weakref doesn’t increase the reference count itself (that’s the whole point of a weak reference).

    When this happens, everytime you try to “dereference” the weakref object (call it), it returns None.

    It is important to remember that in Python variables are actually names, pointing at objects. They are actually “strong references”. Example:

    import weakref
    
    class A:
        pass
    
    # a new object is created, and the name "x" is set to reference the object,
    # giving a reference count of 1
    x = A() 
    
    # a weak reference is created referencing the object that the name x references
    # the reference count is still 1 though, because x is still the only strong
    # reference
    weak_reference = weakref.ref(x)
    
    # the only strong reference to the object is deleted (x), reducing the reference
    # count to 0 this means that the object is destroyed, and at this point
    # "weak_reference" becomes dangling, and calls return None
    del x
    
    assert weak_reference() is None
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if there was any way to create a FileInputStream object from
I was wondering whether there is any way to create custom Orders statuses in
I am wondering if there is any way to create a batch file that
I was wondering if there is any way to create/modify an orchestration by code
I have used Axis wsdl2Java, I am wondering is there any other way to
Is there any way to create an empty Popen() object, or perhaps some other
I was wondering if there's any way to create an alias for a list
I am just wondering if there is any way to create the same function
As the title says, I was wondering if there is any way to create
Just wondering is there any way to create a temporary sandbox environment for running

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.