Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you really have to pass <code>Parameter</code> by pointer to your subscribers, then you should use <code>boost::shared_ptr</code>:</p> <pre><code>typedef boost::shared_ptr&lt;Parameter&gt; SharedParameterPtr; typedef boost::signals2::signal&lt; void ( SharedParameterPtr ) &gt; signalParameterChanged_t; signalParameterChanged_t m_signalParameterChanged; // The signal source void Parameter::raiseParameterChangedSignal() { SharedParameterPtr pParameterDeepCopied = new Parameter(*this); m_signalParameterChanged(pParameterDeepCopied); } // The subscriber's handler void ClientGui::onDeviceParameterChangedHandler( SharedParameterPtr pParameter) { cout &lt;&lt; pParameter-&gt;toString() &lt;&lt; endl; } </code></pre> <p>The shared parameter object sent to your subscribers will be automatically deleted when its reference count becomes zero (i.e. it goes out of scope in all the handlers).</p> <p>Is Parameter really so heavyweight that you need to send it to your subscribers via pointer?</p> <p><strong>EDIT:</strong></p> <p>Please note that using shared_ptr takes care of lifetime management, but will not relieve you of the responsibility to make concurrent reads/writes to/from the shared parameter object thread-safe. You may well want to pass-by-copy to your subscribers for thread-safety reasons alone. In your question, it's not clear enough to me what goes on thread-wise, so I can't give you more specific recommendations.</p> <p>Is the thread calling <code>raiseParameterChangedSignal()</code> the same as your GUI thread? Some GUI toolkits don't allow concurrent use of their API by multiple threads.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COHi Emile, Thank you very much for your informative reply and clear code samples. The boost::shared_ptr is a solution. In my test case deletion occurs as you described. You made me rethink. I would *prefer* to pass-by-value rather than pointers. Yet when using pass-by-value I am disturbed by all the copy constructor activity I see in my test case. My debug prints show the signal resulted in 6 unexpected calls to the copy constructor. After the slot handler, 6 calls to the destructor. Do you have any insight into what all the activity is? Thank you very much for your assistance! -Ed
      singulars
    2. COI can only account for two copy constructions in your own code: 1) Passing your parameter object by copy to signalParameterChanged_t::operator(), 2) signal2 library passing the parameter by copy to the handler. The rest must be from the internal workings of the signals library. I'm not going to attempt to parse the metaprogramming wizardry going on inside signals2, to see where the other copies take place. ;) Try to recompile with optimization turned on (say, -O2) and I bet some of the copy constructions will be optimized away.
      singulars
    3. COIf your Parameter class only has a few built-in types (like int, double, etc.) for member variables, then I say just go ahead and pass Parameter by copy. The nanoseconds spent making 6 copies will be negligible compared to the micro/milliseconds your GUI toolkit will spend rendering scalable fonts on the screen.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload