Results 1 to 20 of 20

Thread: Two ways, Which one is better?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.


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

    talk2amulya (30th March 2009)

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

    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

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.


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, 13:29
  2. Replies: 2
    Last Post: 16th July 2010, 16:03
  3. Model-view: Display items in different ways
    By taboom in forum Qt Programming
    Replies: 3
    Last Post: 13th August 2007, 20:05
  4. ways to exit appl.
    By whoops.slo in forum Newbie
    Replies: 1
    Last Post: 29th January 2007, 19: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
  •  
Qt is a trademark of The Qt Company.