Jump to content
  • 0
Jacob R

Dynamically allocated V. Not, which to do?


 //! Dynamically allocated WbtSystem object to communicate with the WBT.
    WbtApi::WbtSystem* m_WBT;

  I sometimes use pointers and call them from my mainwindow source in QT. Occasionally i do not use them properly and it leads to segment faults.  I often at that point just make all the pointers into variables and run it that way. Is there anything wrong with this?

  Can you explain the difference in performance i will see from using dynamically allocated pointers versus using statically declared variables?

Share this post

Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

There is little to no actual performance difference, when you do it dynamically you have to keep track of when things exist / nullness / lifecycle / etc.

With that being said, unless you are using the class for callbacks, there is no good reason to not just spawn a WbtSystem wherever and whenever you need one on the stiack. The communications between the API classes and the daemon are instantiated in a behind the scenes singleton, so all of the really heavy lifing only happens the first time.


However, if you are using callbacks with Qt please be advised that the callbacks happen on a separate thread, and should not interact directly with *any* QObjects, this is not safe. (http://doc.qt.io/qt-4.8/threads-qobject.html#signals-and-slots-across-threads)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now