Results 1 to 20 of 26

Thread: Basic question on new and delete

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic question on new and delete

    Quote Originally Posted by wysota
    ...you may want to reuse the code as part of some bigger framework and if you don't notice that missing delete (which is very likely to happen), you'll end up with a memory leaking process.
    Think that you mght have an application that runs for several hours and that the memory is allocated meny times without freeing it, eventually your memory resources will run out and your app will remind you a very popular OS back in '95

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Basic question on new and delete

    Another good reason for delete is that you may have, at another place, a memory leak. If you use a memory debugger to find these you will get much less warnings this way.

  3. #3
    Join Date
    Jan 2006
    Location
    Winnipeg, MB. Canada
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic question on new and delete

    Or simply create all your objects as value types (ala RAII) and just let them die of natural causes when they're out of scope. After all, pointers are SO yesterday

  4. #4
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic question on new and delete

    Quote Originally Posted by rh
    After all, pointers are SO yesterday
    I'm sooo with you. If you can avoid them you save yourself a lot of troubles

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Basic question on new and delete

    Quote Originally Posted by rh
    Or simply create all your objects as value types (ala RAII) and just let them die of natural causes when they're out of scope. After all, pointers are SO yesterday
    That's a very bad idea: In another thread somebody did that:
    Qt Code:
    1. for(int i = 0; i < 5; ++i)
    2. {
    3. QThreadDerivedClass t;
    4. t.start();
    5. }
    To copy to clipboard, switch view to plain text mode 
    Just after starting the thread the instance will be destructed because it went out of scope. You need pointers to avoid some very ugly code. Maybe use them through something like boost::shared_ptr with automatic reference counting, it's even exception safe.

  6. #6
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic question on new and delete

    Quote Originally Posted by Codepoet
    ...because it went out of scope...
    This was an implementation bug What would you prefer in the following situation?
    Qt Code:
    1. MyModalDialog modalDlg;
    2. modalDlg.exec()
    3. if(modalDialog.getSomething()==true){
    4. //do something;
    5. }
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. MyModalDialog *modalDlg = new MyModalDialog;
    2. modalDlg->exec()
    3. if(modalDialog->getSomething()==true){
    4. //do something
    5. }
    6. delete modalDlg;
    To copy to clipboard, switch view to plain text mode 
    Then after code being added by many others...
    Qt Code:
    1. MyModalDialog *modalDlg = new MyModalDialog;
    2. modalDlg->exec()
    3. if(modalDialog->getSomething()==true){
    4. //do something
    5. }else{
    6. return; //Ouch
    7. }
    8. // Lot's of code that might also return somewhere
    9. delete modalDlg;
    To copy to clipboard, switch view to plain text mode 

    I'm not saying don't use pointers, but why not avoid them whem they are not needed?

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Basic question on new and delete

    @yop: I cited not you but rh
    Yes one can (should?) avoid them if possible: Your example perfectly demonstrates it. It gets even worse when you work with exceptions.
    I never meant not using instances on the stack, that's most of the time ok but not always.

  8. #8
    Join Date
    Jan 2006
    Location
    Winnipeg, MB. Canada
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic question on new and delete

    The only real restriction to RAII-style initialized variables that i've encountered is, to state the obvious, that its type must be known at compile-time. Anything with runtime attributes (such as factory classes, etc.) still require pointers.

    My biggest qualm on using pointers? That damn "->" operator! C++ is *supposed* to be a language of type reduction, yet the arrow operator is 3x the keystrokes (don't forget the shift) as other dot-notated languages.

    If this makes me shallow (also i like ladies with big boobies), then so be it but in my opinion, RAII initialized variables are not only easier on the carpal tunnel, but are inherently safer (like GC without the overhead) and should be used whenever possible.

    I realize this is drifting slightly from the original post. If everyone is happy with me having the last word i'm good with that

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.