Results 1 to 20 of 20

Thread: Two ways, Which one is better?

  1. #1
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Two ways, Which one is better?

    Hello! Long time no see!

    The following two fragments, which is better, why?

    Qt Code:
    1. void oneDialog::function()
    2. {
    3. QTestDialog* pt= new QTestDialog(this);
    4. // ........
    5. delete pt;
    6. }
    7.  
    8.  
    9.  
    10. void oneDialog::function()
    11. {
    12. QTestDialog dialog(this);
    13. // ........
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 


    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Two ways, Which one is better?

    What do you mean "better"? They are perfectly equivalent. The second one is less code to type in. If that counts as "better" then it's better.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Two ways, Which one is better?

    hey, i was wondering if in the second fragment, its done wrongly. cuz 'this' has been set as the parent of the dialog and as every parent deletes its childs when deleting themselves, when it will try to delete the dialog , the app should crash cuz the dialog is actually created on stack..

  4. #4
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Two ways, Which one is better?

    Quote Originally Posted by talk2amulya View Post
    hey, i was wondering if in the second fragment, its done wrongly. cuz 'this' has been set as the parent of the dialog and as every parent deletes its childs when deleting themselves, when it will try to delete the dialog , the app should crash cuz the dialog is actually created on stack..

    You mean that the dialog should be a modal one?

  5. #5
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Two ways, Which one is better?

    no, that wont make difference..all i mean is widgets and dialogs should always be created on heap..AFAIK

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Two ways, Which one is better?

    Quote Originally Posted by talk2amulya View Post
    no, that wont make difference..all i mean is widgets and dialogs should always be created on heap..AFAIK
    No, that's not true. A modal dialog will always be destroyed before its parent. Unless of course you explicitely shoot yourself in the leg but then the dialog shouldn't have been modal. It really doesn't matter which way you choose here. The stack based version is simply a few statements shorter to code and a tiny bit faster to execute.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Two ways, Which one is better?

    so will that concept apply even in the case of a normal dialog(non-modal)?

  8. #8
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Two ways, Which one is better?

    One goes out of scope by itself, one doesn't automatically go out of scope...

    If it is a modal dialog, the 2nd one works as good as the 1st one but with less codes... You can't say one is better than the other, it depends on situation...

  9. #9
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Two ways, Which one is better?

    Does this forum has any utilities to save a thread, such add to favouites?

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Two ways, Which one is better?

    Quote Originally Posted by talk2amulya View Post
    so will that concept apply even in the case of a normal dialog(non-modal)?
    No. A non-modal dialog has to be created on heap.

    Quote Originally Posted by HelloDan View Post
    Does this forum has any utilities to save a thread, such add to favouites?
    Yeah, there is a "Bookmarks" table at the bottom of the page.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Two ways, Which one is better?

    ok, so that means when i have a model dialog on STACK with another widget as parent..thats not wrong? one would think it should be...when parent will try to delete it, it should be crashing time..or am i getting smth really wrong about model dialog??.or maybe u can create a model dialog on stack and not give it a parent..in these examples, 'this' is parent, so be a dialog model or not, dialog creation on stack should be wrong.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Two ways, Which one is better?

    Quote Originally Posted by talk2amulya View Post
    ok, so that means when i have a model dialog on STACK with another widget as parent..thats not wrong?
    No, it's not. Lifespan of a modal dialog is short - most probably it is born and dies within the same function (method) call. So there is no risk the child will live longer than its parent because it is created on the stack within one of its parent's (sub)methods.

    one would think it should be...when parent will try to delete it, it should be crashing time..or am i getting smth really wrong about model dialog??
    Yes, it would crash. But the parent won't try to delete it because it opened the dialog for a reason and it wants to react on the choice/action performed by the user within the modal dialog.

    .or maybe u can create a model dialog on stack and not give it a parent..
    Yes, you can. But then the dialog will not really be modal.

    in these examples, 'this' is parent, so be a dialog model or not, dialog creation on stack should be wrong.
    No, it's fine :-) The parent lives longer than the child so the child will detach itself from the parent when it goes out of scope and is destroyed thus the parent will not try to delete it when it (the parent this time) goes out of scope.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Two ways, Which one is better?

    ok, i can follow this chain of thoughts..but one thing still bugs me..i read on some of the threads here that one should ALWAYS create widgets and dialogs on heap, layouts also..cuz once they go out of scope and parents try to delete them, crash occurs(obviously!).. so when u say child "detaches" itself from parent when goin out of scope..wont that occur in other cases too and parent wont try to delete ths child? and why the recommendation(in multiple threads) that these things SHOULD be created on stack..how and when(in what cases) would a child detach from parent? my main concern is how would a parent know the child has already gone out of scope and not try to delete it?

    another thing thats done here is dialog on heap is given a parent and then deleted in the same function..wont the parent try to delete the dialog object or dialog again detaches?i mean once u give a widget a parent, deleting it yourself should be kinda foolish? i can go on with queries but essence of my question is how does a parent know which child to delete and in what cases would a child detach itself from parent?

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Two ways, Which one is better?

    Quote Originally Posted by talk2amulya View Post
    ok, i can follow this chain of thoughts..but one thing still bugs me..i read on some of the threads here that one should ALWAYS create widgets and dialogs on heap, layouts also..cuz once they go out of scope and parents try to delete them, crash occurs(obviously!)..
    Only when both the parent and the child are created on the same frame of the stack or the child is created on the stack, the parent on the heap and you delete the parent while the stack frame holding the child is still valid. This is not the case here and it's one of few exceptions when it's ok to create a widget on stack.

    so when u say child "detaches" itself from parent when goin out of scope..wont that occur in other cases too and parent wont try to delete ths child?
    The problem is with double deleting the same object. Consider a situation when the parent is on the heap, the child is on the stack and you delete the parent. When this happens, the parent deletes all its children including the one child residing on the stack. So far this is fine. The trouble begins when the stack frame holding the child widget goes out of scope. The compiler calls destructors of all object residing in the frame including the child widget. Unfortunately the child has already been deleted earlier so we're trying to destroy an object that has already been destroyed which results in aborting the process.

    and why the recommendation(in multiple threads) that these things SHOULD be created on stack.
    With modal dialogs it is easier to create them on the stack because you don't have to worry about deleting them afterwards because the compiler does that for you.

    .how and when(in what cases) would a child detach from parent?
    When it's deleted. The case we're talking about is not related to not detaching from the parent but to double deletion caused by the compiler. And we can't overcome that so you shouldn't create widgets on the stack if you fear the parent might be deleted before the child is deleted.

    my main concern is how would a parent know the child has already gone out of scope and not try to delete it?
    The parent (during runtime) knows. The compiler (during compile time) does not.

    i mean once u give a widget a parent, deleting it yourself should be kinda foolish?
    I'd say it would be foolish if you couldn't delete an object if you wanted to.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. The following user says thank you to wysota for this useful post:

    talk2amulya (29th March 2009)

  16. #15
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Two ways, Which one is better?

    thanks wysota, so i was thinkin that parent might try to delete a child created on stack which should produce crash..so in nutshell, parent would know when a child is deleted and it wont try to delete it..but still, if i create an object on stack like this

    Qt Code:
    1. QWidget *someWidget = new QWidget();
    2. QDialog dialog(someWidget);
    3. delete someWidget; // crash here?
    To copy to clipboard, switch view to plain text mode 

    in the destructor of parent class(QWidget), they are calling delete on children even when they r created on stack which should produce instantaneous crash..it shouldnt even reach the compiler created code for removing the dialog object or would it? i tried something similar and although it sort off showed a weird crash at delete of child..but somehow it went ahead and executed the code further and then crashed later when the stack tried to delete the dialog object..how was this possible? plus doesnt this show real proof that creating qobjects on stack with a parent should be a big no-no? i mean yeh, the case of hellodan survives this, but shouldnt doing this be discouraged?

  17. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Two ways, Which one is better?

    Quote Originally Posted by talk2amulya View Post
    in the destructor of parent class(QWidget), they are calling delete on children even when they r created on stack which should produce instantaneous crash.

    it shouldnt even reach the compiler created code for removing the dialog object or would it?
    Hmm... I guess it depends on the operating system. It might detect you are trying to delete a pointer that is in stack address space which is obviously going to lead to a crash anyway and might terminate your process. But I guess it doesn't have to at least I'm not aware of any C++ rule about what happens after you delete a non-heap pointer.

    i tried something similar and although it sort off showed a weird crash at delete of child..but somehow it went ahead and executed the code further and then crashed later when the stack tried to delete the dialog object..how was this possible?
    That's the variant I described earlier - a double delete caused by the compiler.

    plus doesnt this show real proof that creating qobjects on stack with a parent should be a big no-no? i mean yeh, the case of hellodan survives this, but shouldnt doing this be discouraged?
    It is discouraged My opinion is that you can use any mechanism the language supports if you know what you are doing. You might even use "goto" if you want. And there is always a chance of shooting yourself in the foot and yet nobody prevents you from doing that. Consider this:

    Qt Code:
    1. Object o;
    2. delete &o;
    To copy to clipboard, switch view to plain text mode 

    or:
    Qt Code:
    1. Object *o;
    2. delete o; // with a bit of luck this will not always crash...
    To copy to clipboard, switch view to plain text mode 

    I might offend some of the people here but in general I'm against "enlightened users" writing software. If they implement crashing code, that's their choice, they have been warned. Someone who knows the language well and knows the internal behaviour of computer systems will know when to create something on the heap and when to create it on the stack.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. The following user says thank you to wysota for this useful post:

    talk2amulya (30th March 2009)

  19. #17
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Two ways, Which one is better?

    Quote Originally Posted by wysota View Post
    Hmm... I guess it depends on the operating system. It might detect you are trying to delete a pointer that is in stack address space which is obviously going to lead to a crash anyway and might terminate your process. But I guess it doesn't have to at least I'm not aware of any C++ rule about what happens after you delete a non-heap pointer.
    i believe its not dependent upon operating system. I tried my example with minGW compiler and VC++ compiler, both gave different results. on VC++ compiler, the application crashes while deleting an object created on stack, whereas when using minGW compiler, on the same operating system(Windows xp), it didnt crash there but when compiler is double deleting. Perhaps VC++ takes a stand on the deleting of non-heap pointers. Anyways, i think to answer the question of hellodan, one would say the creation of object on heap would be a better solution but thats just me

  20. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Two ways, Which one is better?

    Quote Originally Posted by talk2amulya View Post
    i believe its not dependent upon operating system. I tried my example with minGW compiler and VC++ compiler, both gave different results. on VC++ compiler, the application crashes while deleting an object created on stack, whereas when using minGW compiler, on the same operating system(Windows xp), it didnt crash there but when compiler is double deleting. Perhaps VC++ takes a stand on the deleting of non-heap pointers. Anyways, i think to answer the question of hellodan, one would say the creation of object on heap would be a better solution but thats just me
    It only means it is also dependent on the compiler.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  21. #19
    Join Date
    May 2008
    Posts
    155
    Thanked 15 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Two ways, Which one is better?

    Quote Originally Posted by wysota View Post
    What do you mean "better"? They are perfectly equivalent. The second one is less code to type in. If that counts as "better" then it's better.
    It also use one allocation less and has a slightly better chance to be exception save. I can't think of any situation where the first form is better.

  22. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Two ways, Which one is better?

    This is really an academic talk. One allocation more or less doesn't make a difference, Qt doesn't use exceptions so only your code can be throwing one so you should know how/where to catch it, etc. These two are practically equivalent, the stack based is simpler to type in which is completely meaningless as well. There is a potential situation when a heap based dialogue would lead to a memory leak but then read my post about "enlightened users"
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Is there any ways to check for nan inf and -inf in Qt?
    By aarelovich in forum Qt Programming
    Replies: 5
    Last Post: 2nd June 2016, 12:29
  2. Replies: 2
    Last Post: 16th July 2010, 15:03
  3. Model-view: Display items in different ways
    By taboom in forum Qt Programming
    Replies: 3
    Last Post: 13th August 2007, 19:05
  4. ways to exit appl.
    By whoops.slo in forum Newbie
    Replies: 1
    Last Post: 29th January 2007, 18:18

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.