0

I would like to test a Qt piece of code that is using signals/slots with std::shared_ptr as parameters.

My issue is that the QSignalSpy class, that works well for all the other type of parameters, does not seem to play nice when it comes to use standard shared pointers. Indeed, the takeFirst method returns a list of QVariant. I can convert the argument to a shared_ptr doing something like that:

QVERIFY(arguments.at(0).value<std::shared_ptr<Canard>>() == canardInstance); // verify the first argument

But then I end up with 2 smart pointers referencing the same heap memory, and it crashes when going out of the current scope. Is there a way to use QSignalSpy in my case ?

Thanks !

CanardMoussant
  • 913
  • 8
  • 22
  • Do you need a shared pointer? Qt does not work well with shared pointer because it has it's own kind of "memory management". If I have to what I usually do as a workaround is storing the shared_ptr in the class so they keep the memory alive and then work with raw-ptr (or an index) to access them in slots. – Hayt Sep 12 '16 at 08:51
  • Yes I do not want a QObject with a parent, but rather something that will be shared with several threads and deleted when no one needs it. – CanardMoussant Sep 12 '16 at 09:35
  • I haven't worked with those yet but you can try a look at `QSharedPointer`. Else I won't see the a way to use std shared pointer with Qt easily. – Hayt Sep 12 '16 at 11:40
  • My bad, actually it behaves as planned with shared_ptrs, I had a memory corruption for another reason... ! Closing the question – CanardMoussant Sep 12 '16 at 12:48

1 Answers1

1

Actually I missed completely the point. It was working fine (meaning the reference counting is working as expected) but I got a memory corruption for another reason. I tried using QSharedPointer and get the same behavior.

CanardMoussant
  • 913
  • 8
  • 22