Results 1 to 11 of 11

Thread: costructor/destructor problem

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default costructor/destructor problem

    hi, I coded this:
    Qt Code:
    1. Ne::Ne(): {
    2. printf ("costructor \n");
    3. _w = new double[10];
    4. }
    5.  
    6. Ne::~Ne() {
    7. printf ("destructor \n");
    8. delete[] _w1;
    9.  
    10. }
    11. class Lay {
    12. std::vector <Ne> _ne;
    13. ...............
    14. }
    15. //in main.cpp
    16. lay._ne.resize(2); //here crash
    To copy to clipboard, switch view to plain text mode 
    I can see that when resize is called happen this:
    Qt Code:
    1. costructor
    2. destructor
    3. destructor
    4. and crash......
    To copy to clipboard, switch view to plain text mode 
    I wonder why does 'destructor' start? and 2 times? crash reason maybe can be destroy 2 times the same portion of memory.....but ....
    thanks
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: costructor/destructor problem

    Quote Originally Posted by mickey View Post
    I wonder why does 'destructor' start? and 2 times? crash reason maybe can be destroy 2 times the same portion of memory
    It's exactly the same problem which I've mentioned in other thread. Implement copy constructor and operator= for Ne and you'll see what's happening.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: costructor/destructor problem

    Sorry, but maybe the situation is more hard:
    Qt Code:
    1. class Network {
    2. private:
    3. Lar ilay;
    4. .....................
    5. };
    6.  
    7. void Network::_create() {
    8. ilay._ne.resize(10);
    9.  
    10. }
    11. //lay.h
    12. #include "ne.h"
    13. #include <vector>
    14. class Network;
    15. class Lay {
    16. protected:
    17. std::vector <Ne> _ne; //it isn't a pointer
    18. friend Network;
    19. public:
    20. Lay();
    21. ~Lay();
    22. };
    23. Lay::Lay {
    24. }
    25. class InputLay : public Lay {
    26. public:
    27. InputLay(){ printf ("cosntructor lay INPUT\n"); };
    28. ~InputLay(){ printf ("desntructor lay INPUT\n");};
    29. };
    30.  
    31. //ne.h
    32. Ne::Ne(): {
    33. printf ("cosntructor\n");
    34. //_w1=0;
    35. _w1 = new double[10];
    36. }
    37.  
    38. Ne::~Ne() {
    39. printf ("destructor \n");
    40. delete[] _w; //without this works..
    41. }
    42. and it print this:
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. cosntructor ne
    2. destructor ne
    3. destructor ne
    To copy to clipboard, switch view to plain text mode 
    But why does it happen when resize is executed??? why destructor is called 2 times during resize? I don't understand even why it's called!!
    It seems me that I haven't done any copy assignment...
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: costructor/destructor problem

    Quote Originally Posted by mickey View Post
    It seems me that I haven't done any copy assignment...
    But resize() did. It's defined this way:
    Qt Code:
    1. void resize( size_type num, const TYPE& val = TYPE() );
    To copy to clipboard, switch view to plain text mode 
    You can't have empty cells in a vector, so if the resized vector is longer than original one, val will be used to fill those additional cells.

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: costructor/destructor problem

    hi, now it seems ok, but I still have some questions...
    Qt Code:
    1. Ne& Ne::operator=(const Ne& n) {
    2. printf (" operator = ne \n");
    3. if (this != &n) {
    4. delete [] _w;
    5. _w = new double[10];
    6. *_w = *n._w;
    7. }
    8. return *this;
    9. }
    10.  
    11. Ne::Ne(const Ne& n): _w(0.5f) {
    12. printf ("cosntructor of copy ne \n");
    13. _w = new double (sizeof n._w);
    14. *_w = *n._w;
    15. }
    To copy to clipboard, switch view to plain text mode 
    during resize (it's write previous post), output is this:
    Qt Code:
    1. cosntructor ne
    2. cosntructor of copy ne
    3. cosntructor of copy ne
    4. cosntructor of copy ne
    5. destructor ne
    6. destructor ne
    To copy to clipboard, switch view to plain text mode 
    1. why 3 constructor copy and not 2???
    2, I declarede operator= as public; but it doens't print the message; so I thought it isn't necessary; then I declare it as private..but compiler gets me error like 'I can't access private member...' why? it seemed it isn't used.....
    3. last: If I don't implement my own operator=, it work fine!!
    4. is it necessary in initiazation list of copy costructor _w(0.5) )?
    then, It isn't clear if it's used or not...
    thanks
    Last edited by mickey; 15th February 2007 at 22:57.
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: costructor/destructor problem

    Quote Originally Posted by mickey View Post
    1. why 3 constructor copy and not 2???
    Once again take a look at resize() declaration. In STL implementation that comes with GNU libstdc++ it's defined like this:
    Qt Code:
    1. resize(size_type __new_size, value_type __x = value_type())
    To copy to clipboard, switch view to plain text mode 
    • First a temporary object will be created (because of TYPE()). That's why you get the first and the last message.
    • Then val is created using the copy constructor, so you get first message about copy constructor and another one about destructor.
    • Finally the two new cells are initialized using copy constructor.


    Qt Code:
    1. Ne::Ne(const Ne& n): _w(0.5f) {
    2. printf ("cosntructor of copy ne \n");
    3. _w = new double (sizeof n._w);
    4. *_w = *n._w;
    5. }
    To copy to clipboard, switch view to plain text mode 
    This won't work. sizeof n._w is 4 (on 32-bit systems), because that's the size of a pointer. And why do you initialize _w to 0.5f if it's a pointer? Finally *_w = *n._w will copy only _w[0].

    It should be something like:
    Qt Code:
    1. Ne::Ne( const Ne& n )
    2. : _w( 0 ), _size( n._size )
    3. {
    4. printf ( "cosntructor of copy ne \n" );
    5. _w = new double[ _size ];
    6. for( size_t i = 0; i < _size; ++i ) {
    7. _w[i] = n._w[i];
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    Or simply use std::vector<double> and forget about all of the troubles.

  7. The following user says thank you to jacek for this useful post:

    Kumosan (16th February 2007)

  8. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: costructor/destructor problem

    (But I don't see something abiut my second question....)
    I guess I have to do the same inside operator= redefinition!
    Why do you use _w(0) ??? it isn't necessary (?)
    thank you.
    Regards

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: costructor/destructor problem

    Quote Originally Posted by mickey View Post
    But I don't see something abiut my second question....
    operator= isn't used by resize(), but other vector methods might need it.

    Quote Originally Posted by mickey View Post
    Why do you use _w(0) ???
    Because it's a lot better value than 0.5f.

    Quote Originally Posted by mickey View Post
    it isn't necessary (?)
    No, it isn't.

  10. #9
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: costructor/destructor problem

    I'm sorry for continue questions but it's happening this strange things:
    the program crash on return 1 of main() . It seems desctuctor of ne is called to infinity......it seems like a loop, then it do infinite delete[] _w....
    ---------------------edited---------------------------
    Sorry, I did 'clean solution' and after I compiled again and it doens't crash any more. What can be? Is it normal? thanks
    Last edited by mickey; 16th February 2007 at 02:13.
    Regards

  11. #10
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: costructor/destructor problem

    Quote Originally Posted by mickey View Post
    Sorry, I did 'clean solution' and after I compiled again and it doens't crash any more. What can be? Is it normal? thanks
    When you build your program inside an IDE (f.e. Visual Studio), it usually just builds the files which have been changed or have not been compiled yet. If you Rebuild your program everything gets compiled again. The difference is, that with a normal build command, it can happen that you keep your old buggy code in the already generated object file and that object file is then linked into your executable, resulting in crashing software (because the bug is still in the object files). I'm too tired right now to remember the rule of thumb we have at our company, but I think it was something along the lines of "as long as you do not change a header file there's no need for a rebuild". I am certain jacek or wysota will gladly correct me on this

  12. #11
    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: costructor/destructor problem

    Quote Originally Posted by Methedrine View Post
    I am certain jacek or wysota will gladly correct me on this
    I doubt that, we rarely use Visual Studio And my rule of thumb is "when using VS, when correcting existing code always rebuild, just in case...". Too much time wasted chasing inexistent bugs...

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.