Results 1 to 9 of 9

Thread: QObject factory (singleton pattern)

  1. #1
    Join Date
    Oct 2008
    Location
    Belgium
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QObject factory (singleton pattern)

    Hi, I've been trying to implement an object factory that constructs objects as singleton.
    Here's the get_instance of the singleton class, where 'object' refers to a template parameter.

    Qt Code:
    1. object& get_instance()
    2. {
    3. static object instance__;
    4. return instance__;
    5. }
    To copy to clipboard, switch view to plain text mode 

    The use of 'static' gets me into troubles when using a QObject (or any derived instance).
    Because QApplication takes control over a QObject's memory, the QObject is already deleted when QApplication is destructed.
    After main(), the destructors are called for all static objects, and for those QObjects, this causes a segmentation fault.

    Is there a clean way to use 'static' together with QObject?


    A simple example to demonstrate the issue on exit:

    Qt Code:
    1. int main(int argc__, char **argv__)
    2. {
    3. QApplication app__(argc__, argv__);
    4.  
    5. static QLabel lbl__("Hello world!");
    6. lbl__.show();
    7.  
    8. app__.exec();
    9.  
    10. return 0;
    11. }
    To copy to clipboard, switch view to plain text mode 

    When closing the label widget, the widget gets deleted twice, resulting in a segmentation fault.


    Thanks in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QObject factory (singleton pattern)

    the sample code you posted below does no segmentation fault when I try it.
    And I don't see why it should.
    Why do you think the label gets destroyed twice?
    Because QApplication takes control over a QObject's memor
    This is only true if you parent your object to QApplication object, which you didn't int your example.

    Hi, I've been trying to implement an object factory that constructs objects as singleton.
    Please explain more what you mean, since a factor for singleton is almost an oxymoron.
    A singleton can only have one instance of its type,so what for do you need a factory for it?

    The problem you have with static is probably bad usage of static.
    Show us your full code, or a full relevant segment.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QObject factory (singleton pattern)

    Can't you redesign? It's recommended to keep separate the GUI and the functionality. And the singleton <i guess> is part of functionality, not GUI, so take him out of QLabel (or other GUI thing).

  4. #4
    Join Date
    Oct 2008
    Location
    Belgium
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QObject factory (singleton pattern)

    Thanks guys for the quick reply.

    Yes you're right about factory / singleton It's the singleton I want to achieve.
    But forget about that, it's just the static part that bothers me.

    The sample code does segfault here on my machine. Are you sure you used a static QLabel?
    (I'm running on Debian Lenny, Qt-4.6.3, gcc 4.3.2)

    Run from command line and close the application properly to notice the cleanup problem (typically by clicking the X in the upper right corner), it won't work if you CTRL-C out of it.

    Anyone else who can reproduce the segfault (during application exit)?

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QObject factory (singleton pattern)

    I copy- pasted your code.
    But I run it on windows.
    And it doesn't segfaults when I run it out side the IDE.
    You didn't explain why it should.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Oct 2008
    Location
    Belgium
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QObject factory (singleton pattern)

    Strange that it runs fine on Windows... But you might get an unexpected blue screen later today (just kidding)

    The label doesn't have QApplication as its parent, it's 0. So I agree that it's not managed by QApplication and it's not deleted twice.
    At first sight, I assumed it had something to do with the object tree. My mistake, sorry about the confusion!

    The debugger gave me the following backtrace. Still not sure what's happening exactly, but it may explain why it isn't crashing on Windows, because it's clearly inside an X11 class where it segfaults.

    Qt Code:
    1. (gdb) run
    2. Starting program: test_app
    3. [Thread debugging using libthread_db enabled]
    4. [New Thread 0xb646b6d0 (LWP 12758)]
    5.  
    6. Program received signal SIGSEGV, Segmentation fault.
    7. [Switching to Thread 0xb646b6d0 (LWP 12758)]
    8. 0xb70f0f72 in ~QX11WindowSurface (this=0x8190438) at painting/qwindowsurface_x11.cpp:84
    9. 84 XFreeGC(X11->display, gc);
    10. (gdb) bt
    11. #0 0xb70f0f72 in ~QX11WindowSurface (this=0x8190438) at painting/qwindowsurface_x11.cpp:84
    12. #1 0xb70eaa55 in ~QWidgetBackingStore (this=0x81902e8) at painting/qbackingstore.cpp:858
    13. #2 0xb6f0802a in QWidgetPrivate::deleteExtra (this=0x818e9c0) at kernel/qwidget.cpp:1622
    14. #3 0xb6f0883f in ~QWidgetPrivate (this=0x818e9c0) at kernel/qwidget.cpp:235
    15. #4 0xb730650d in ~QLabelPrivate (this=0x818e9c0) at widgets/qframe_p.h:62
    16. #5 0xb6b06c78 in ~QObject (this=0x80491a0) at ../../include/QtCore/../../src/corelib/tools/qscopedpointer.h:62
    17. #6 0xb6f194dd in ~QWidget (this=0x80491a0) at kernel/qwidget.cpp:1496
    18. #7 0xb72feed1 in ~QFrame (this=0x80491a0) at widgets/qframe.cpp:242
    19. #8 0xb7305c33 in ~QLabel (this=0x80491a0) at widgets/qlabel.cpp:262
    20. #9 0xb6719481 in __run_exit_handlers (status=0, listp=0xb682d324, run_list_atexit=true) at exit.c:78
    21. #10 0xb67194df in *__GI_exit (status=0) at exit.c:100
    22. #11 0xb6700b5d in __libc_start_main (main=0x8048b64 <main>, argc=1, ubp_av=0xbfffd594, init=0x8048db0 <__libc_csu_init>,
    23. fini=0x8048da0 <__libc_csu_fini>, rtld_fini=0xb77ee9b0 <_dl_fini>, stack_end=0xbfffd58c) at libc-start.c:254
    24. #12 0x08048ad1 in _start ()
    25. (gdb)
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QObject factory (singleton pattern)

    Hmm...
    I don't know.
    It might be a compiler issue, or a Qt system specific implementation issue.
    Or- something to do with the a run time dependency which any QPaintDevice has with QApplication- and the way it is (destruction) implemented on Linux.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  8. #8
    Join Date
    Oct 2008
    Location
    Belgium
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QObject factory (singleton pattern)

    Could be many things yes... I'm going to avoid the use of static QObjects, until I know more about the details. Unfortunately, I can't spend too much time on it right now.

    Thanks again for helping out!

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QObject factory (singleton pattern)

    I have implemented QObjects as singletons in the past and I had no problems.
    I am quite sure that if you stick to the rules, and don't try exotic things, you wont have any trouble.
    I am quite confident that the problem you have is in your code/design and not things out side your code.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Designer and view factory
    By Rockem in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2009, 11:22
  2. Simple yet powerful object factory
    By mcostalba in forum Qt Programming
    Replies: 2
    Last Post: 23rd August 2007, 16:46
  3. How to use QMIME Sourse Factory
    By shamik in forum Qt Programming
    Replies: 1
    Last Post: 20th December 2006, 14:49
  4. Singleton pattern - end in recursion
    By probine in forum General Programming
    Replies: 6
    Last Post: 29th March 2006, 13:08
  5. trying to use singleton pattern
    By therealjag in forum Newbie
    Replies: 3
    Last Post: 20th February 2006, 01:20

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.