Crash of process when using self referencing shared_ptr in class

Discussions about product bugs & problems!
Note: This is no replacement for the Official ETM Support!
1 post • Page 1 of 1
flindecke
Posts:69
Joined: Wed Jun 24, 2015 1:54 pm

Crash of process when using self referencing shared_ptr in class

Post by flindecke »

I have found a bug during the usage of classes and shared_ptr and the usage of a workaround of using this as a shared_ptr.

Assume we have the following class:

Code: Select all

class ClassA {
  shared_ptr _this;

  ClassA() { }
  public static shared_ptr create() {
    shared_ptr result = new ClassA();
    result._this = result;
    return result;
  }
};
This class can be created by the static factory method ClassA::create

Background Information:
In a method of ClassA we are not able to get the shared_ptr of the instance of this method. The syntax this refers to the class itself and not to the shared_ptr of this class.
I do not know a way to get the shared_ptr of this.
So i used the factory method which modifies the created instance.

Problem:

Code: Select all

shared_ptr _instance;
_instance = ClassA::create();
works as expected, but

Code: Select all

shared_ptr _instance;
_instance = ClassA::create();
_instance = ClassA::create();
will crash the process.

I think this is due to a double delete on the C++ side, when the first instance is not used anymore and the "destructor" is called. This code tries to get rid of the shared_ptr _this as well and this is the big crash (IMHO)

I hope this will help others to find other solutions.

At my own problem i have found another solution (which has to do with dpConnectUserData and the callback needs to access the shared_ptr).

With best regards
Frank Lindecke

1 post • Page 1 of 1