logo
down
shadow

Object deleted when its member function is being executed?


Object deleted when its member function is being executed?

By : Cameron Booth
Date : November 24 2020, 05:44 AM
I hope this helps you . It is probably undefined behavior, unless you are extra careful.
Deleting the object while your thread accesses it in an unordered (non-synchronized, basically) manner is undefined behavior, even if you get lucky.
code :


Share : facebook icon twitter icon
Is it undefined behavior to have an object be deleted before its member function returns?

Is it undefined behavior to have an object be deleted before its member function returns?


By : David
Date : March 29 2020, 07:55 AM
hop of those help? No, it's okay, if you are careful: https://isocpp.org/wiki/faq/freestore-mgmt#delete-this
Why I can access member functions even after the object was deleted?

Why I can access member functions even after the object was deleted?


By : saiful
Date : March 29 2020, 07:55 AM
hop of those help?
So, my question is, why I'm still able to call Go_XXX_Your_Self() and Identify_Your_Self() even after the object was deleted?
code :
delete MC1;
MC1 = nullptr;
if(my_ptr) {
   // my_ptr is most possibly valid (though you can still go wrong)
   // use my_ptr
}
MyClass* some_ptr = nullptr;
...
Is it possible to detect if the object that a bound member function refers to is deleted or destroyed

Is it possible to detect if the object that a bound member function refers to is deleted or destroyed


By : user3310820
Date : March 29 2020, 07:55 AM
wish of those help std::bind can accept smart pointers, so you can simply pass std::shared_ptr to it.
code :
std::shared_ptr<A> p_a(new A);
std::function<int()> f = std::bind(&A::add, p_a, 1, 1);
member variable's reference count does not decrement after object was deleted

member variable's reference count does not decrement after object was deleted


By : user3126890
Date : March 29 2020, 07:55 AM
it should still fix some issue I have simplified your example to make it easier for mentally counting the references.
What happens is that you have to count a reference for the object, another for the self inside the method, but also other references for the same object in the arguments of any function call running at the moment you invoke the sys.getrefcount, including that function too, because it also holds a copy of the reference in its own local scope.
code :
import sys

class B():
    def __init__(self):
        print('setting up b...')
    def counter(self):
        return sys.getrefcount(self)

class A():
    def __init__(self):
        print('setting up a...')

if __name__ == '__main__':
    a = A()
    b = B()
    print(sys.getrefcount(a))
    print(sys.getrefcount(b))

    print(b, b.counter())  # 5 refs to b. Why 5?
    print(b, B.counter(b)) # Because this is what really happens.
    print(b.counter(), b ) # 4 refs to b

    print(b,b,b.counter()) # 6 refs to b
setting up a...
setting up b...
2
2
<__main__.B object at 0x7f11df905940> 5
4 <__main__.B object at 0x7f11df905940>
<__main__.B object at 0x7f11df905940> <__main__.B object at 0x7f11df905940> 6
a = A()
A()
C++: object deleted (created with new) but member function still working (?)

C++: object deleted (created with new) but member function still working (?)


By : John McKernan
Date : March 29 2020, 07:55 AM
this one helps. Just for curiosity and experimenting I wrote following code and now am trying to understand whats happening after delete... why is the cat object still meowing ?? ,
why is not crashing
code :
Cat cat;
cat.meow();
auto cat_ptr = std::make_unique<Cat>();
cat_ptr->meow();
std::vector<Cat> cats;
std::vector<std::unique_ptr<Cat>> cat_ptrs;
Related Posts Related Posts :
  • C++ Dynamically Allocated Array; Size set by quantity of user input; Writing to a file;
  • enums to be defined as uint8_t variables in structure
  • C++ Overloaded Constructor issues
  • Fast CSV parser in C++
  • MFC GUI application rewrite - threading, messaging, Document/View, etc. Advice needed
  • How to get frame feed of a video in OpenCV?
  • saving data structure c++ to disk
  • Vector of pointers: some clarification needed
  • scaling a bitmap without losing quality
  • How to evaluate a function directly?
  • QNetworkAccessManager: How to change post data at createRequest function
  • Returning std::move of a local variable
  • Is there a fast linear SVM library with a good C++ interface?
  • What is the purpose of name mangling in C++?
  • C++ Why the void function is not working eventhough I called it?
  • How the pointer and the pointer's adress can share the same memory adress?
  • error C2220: warning treated as error - no 'object' file generated
  • exception of strcpy function in c++ console program
  • How to access USB barcode scanner data directly from USB port using C/C++ without driver
  • Under which circumstances will std::vector.clear() call a destructor?
  • Template partial specialisation and dependent names
  • best way to share data between c codes
  • C++ ignores if statement conditions
  • How to set the argv[ ] to be case-insensitive in a Win32 Console Application?
  • How to fix error "clang: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)"?
  • C++ How do I print a .txt file verbatim?
  • Creating two dimensional array of class
  • How do I correctly use COMMTIMEOUTS with OVERLAPPED IO mode reading from a Serial port
  • An assert macro which expands to static_assert when possible?
  • How to write a copyconstructor for a class which has HANDLE as a member in win32 c++ application?
  • C++ saving info such as tree in a file
  • Transforming an expression template tree
  • How to overload an operator with multiple parameters like a + b + c?
  • C++ 11 with Raspberry Pi performances
  • Make a C++ class look like a numpy array using swig
  • Postfix incrementer overloading for nested enumerated types
  • Is there a tidy way of associating metadata with functions in C++
  • QObject::installEventFilter(): Cannot filter events for objects in a different thread
  • LNK2005 error with Zxing C++
  • C++ Doubly Linked List with Pointers: Object of class isn't constructed properly
  • Using a random string generator in c++ constructor
  • What should I use instead of void as one of the alternative types in an variant?
  • C++ return value from multithreads using reference
  • How to connect multiple TCP IP clients to same server port using c++
  • Defaul compiler generates the reference operator (In C++)?
  • Unable to change directory time stamp after using FILE_FLAG_BACKUP_SEMANTICS
  • vector handling displaying output
  • WSAGetLastError returns WSAENOTSOCK - Cause?
  • C++: How to overload pow for user type?
  • C++ using arrays as multidimensional despite initalising it as 1D with pointer
  • How negate std::is_integral for use in tag dispatch?
  • Retrieve serial number from USB memory (Windows environment c++)
  • g++ error: invalid preprocessing directive #INCLUDE
  • C++ What is the std::for_each() function parameter type?
  • C++: Read individual lines from text file, sort words alphabetically
  • Saving 'this' address into a variable
  • c++ command line arguments in ubuntu terminal
  • Convert "Cartesian coordinates" to "polar coordinates with respect to user specified origin"
  • In what order are local scoped objects destructed?
  • How to use SDL_MapRGB with SDL 2.0
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org