Results 1 to 20 of 21

Thread: qRegisterMetaType- reference as argument in connect throwing run time error

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 14 Times in 14 Posts

    Default Re: qRegisterMetaType- reference as argument in connect throwing run time error

    Quote Originally Posted by anda_skoa View Post
    you would have the thread access a destroyed object._
    you are referring to below function I think & myData obj; is a class member in my actual code & It never gets destructed before thread finishes. again this is due to writing some code on fly sorry for that.

    Qt Code:
    1. void myclass::startThread()
    2. {
    3. myData obj; //this is class member in actual code & never gets destructed before thread finishes, I just wanted to show the reference variable problem here so I just didn't care this
    4.  
    5. emit startMyThread(obj);
    6. }
    To copy to clipboard, switch view to plain text mode 
    Quote Originally Posted by anda_skoa View Post
    Passing a reference is possible, just not via a queued connection._
    So what connect should I use, Direct connection making slot to execute in main thread. I think other connections more or less same as these 2 connections except unique connection.

    Quote Originally Posted by anda_skoa View Post
    What feature?
    To store a reference like a pointer? That you would have to take up with the C++ standard comittee._
    passing reference to thread when It is queued connection if there is no other way.
    Thanks :-)

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    Default Re: qRegisterMetaType- reference as argument in connect throwing run time error

    myData obj is destroyed immediately after emiting signal. Slot in another thread is run in the future when the object referenced by reference no longer exists.

  3. #3
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 14 Times in 14 Posts

    Default Re: qRegisterMetaType- reference as argument in connect throwing run time error

    Quote Originally Posted by Lesiok View Post
    myData obj is destroyed immediately after emiting signal. Slot in another thread is run in the future when the object referenced by reference no longer exists.
    That is what I clarified in the above reply, myData obj is actually class member of myclass in my original code. It never gets destroyed before worker thread finishes its work.
    Thanks :-)

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: qRegisterMetaType- reference as argument in connect throwing run time error

    Quote Originally Posted by prasad_N View Post
    you are referring to below function I think & myData obj; is a class member in my actual code & It never gets destructed before thread finishes. again this is due to writing some code on fly sorry for that.
    Then you should probably not have used a local variable definition.

    Quote Originally Posted by prasad_N View Post
    So what connect should I use, Direct connection making slot to execute in main thread. I think other connections more or less same as these 2 connections except unique connection.
    It depends on your requirements.
    If you want the slot to execute in the context of the emitter thread, use a direct connection.
    If you want to use the signal/slot bridge as a mean to conveniently copy data between threads, use a queued connection.


    Quote Originally Posted by prasad_N View Post
    passing reference to thread when It is queued connection if there is no other way.
    I wouldn't get my hope up. If the C++ Standard committee wanted to allow assigment of reference variables, that would have been possible a long time ago, especially since that has always been possible for pointers.

    I.e. if C programmers want to refer to the same object from two different variables (aliasing), they use pointers.
    C++ programmers can also use pointers and can use references for the special case where the alias can be initialized with the address of the object, e.g. passing a reference to a constructor, storing it in a reference member.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,335
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    317
    Thanked 871 Times in 858 Posts

    Default Re: qRegisterMetaType- reference as argument in connect throwing run time error

    Then you should probably not have used a local variable definition.
    @prasad_N:
    Or at least written it as a commented-out line, with an explanation that "obj" is a class member variable. How are we to understand that when you write it as you did, you weren't posting your actual code? A lot of the discussion here was based on your incorrect posting, and it could have been avoided if you had posted the actual code in the first place.

    C++ programmers can also use pointers and can use references for the special case where the alias can be initialized with the address of the object, e.g. passing a reference to a constructor, storing it in a reference member.
    And this has the added benefit over storing a pointer member variable in that you are absolutely assured that the member variable is a valid reference to an object instance, whereas anything can be assigned to a pointer member, including nothing.

  6. #6
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 14 Times in 14 Posts

    Default Re: qRegisterMetaType- reference as argument in connect throwing run time error

    Quote Originally Posted by d_stranz View Post
    A lot of the discussion here was based on your incorrect posting, and it could have been avoided if you had posted the actual code in the first place.
    There was a mistake & people are over emphasizing on that rather than a actual question, Anyhow I will take care from next time while posting.


    Quote Originally Posted by anda_skoa View Post
    It depends on your requirements.
    If you want the slot to execute in the context of the emitter thread, use a direct connection.
    If you want to use the signal/slot bridge as a mean to conveniently copy data between threads, use a queued connection.
    _
    None of them are not my requirements. As per my requirement & Replay's from you guys I think there is no solution for this at least for now (passing reference across threads if it it is queued connection).
    Thanks :-)

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: qRegisterMetaType- reference as argument in connect throwing run time error

    Yes, the only reference that can be stored is a pointer.

    Cheers.
    _

Similar Threads

  1. Replies: 0
    Last Post: 19th January 2012, 16:52
  2. Replies: 2
    Last Post: 9th May 2011, 11:38
  3. Replies: 6
    Last Post: 9th April 2011, 20:23
  4. qRegisterMetaType - problems with connect()
    By Macok in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2009, 23:55
  5. Replies: 2
    Last Post: 8th October 2007, 16:02

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.