Assertion from ReferenceCountedObject::Ptr on exit

I have a component class that holds a ReferenceCountedObject::Ptr, e.g.

class MyComponent : public Component
    MyComponent (MyObject* myObject) : myObject (myObject) {}
    ~MyComponent () {}


    MyObject::Ptr myObj;

I insantiate this class like so: 

MyObject::Ptr myObject = new MyObject(); 
addAndMakeVisible (myComponent = new MyComponent (myObject));

When I close the app, I get an assertion from ReferenceCountedObject telling me that the object has already been deleted. I've tried adding delete statements in the class destructor but this triggers a different assertion (trying to delete an object that no longer exists!)

How do I prevent the assertion being thrown? 

The ctor of MyComponent uses a raw pointer parameter whereas you pass a ReferenceCountedObjectPtr argument?

Do that:

MyComponent (const MyObject::Ptr& myObject) : myObj (myObject) {}

or that:

addAndMakeVisible (myComponent = new MyComponent (new myObject()));


Hmmm.. I tried your suggestions but still get the assertion:

    /** Decreases the object's reference count.

        If the count gets to zero, the object will not be deleted, but this method

        will return true, allowing the caller to take care of deletion.


    bool decReferenceCountWithoutDeleting() noexcept


        jassert (getReferenceCount() > 0);

        return --refCount == 0;


Oops, sorry. I guess my suggestion doesn’t change anything since JUCE uses intrusive RC.

No more idea thus :wink:

No problem, thank you for your suggestions anyway! 

I would assume the assertion is correct and you are deleting the object twice. I would put a break point in the MyObject destructor just to check where it gets deleted.