Results 1 to 13 of 13

Thread: Can't delete an object (instance of structure with a nested structure)

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Can't delete an object (instance of structure with a nested structure)

    I have two structures:



    struct W1 {int v1;...};
    struct W2 {
    W1 my_w1;
    .....
    };

    As you can see W2 has a W1 inner member.


    I have some functions to do the work :; (pseudo code)
    do
    Create the instance my_w2 = new w2;
    general code to do some things.
    delete my_w2;
    loop

    I have discover that when delete my_w2 keeps my_w1 information ????
    How can I delete my_w2 effectively ? What I'm doing bad ?

    Also... Is there a way to reset (empty) an object ? (or have I to delete it ?)
    Thanks

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Can't delete an object (instance of structure with a nested structure)

    I have discover that when delete my_w2 keeps my_w1 information ????
    What do you mean by "keeps my_w1 information", how do you know it keeps it? after delete call, my_w2 will be 0

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Can't delete an object (instance of structure with a nested structure)

    What else is in W1 structure, maybe something allocated on heap ? In that case, you'll need to free the memory allocated on heap manually (or better - write a destructor for W1 to do this for you).

  4. #4
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't delete an object (instance of structure with a nested structure)

    This are my 2 structures:

    Qt Code:
    1. struct W_perfil_header {
    2. double pk;
    3. unsigned short int num_sups;
    4. double pd,px,py,pz,azi;
    5. };
    6.  
    7. struct W_perfil {
    8. W_perfil_header header;
    9. std::vector<W_sup> V_sups;
    10. };
    To copy to clipboard, switch view to plain text mode 


    the code that work with them pseudocode)

    Qt Code:
    1. class.h
    2. Public :
    3. W_perfil * w_perfil;
    4.  
    5. class.cpp
    6. create_perf() {
    7. w_perfil = new W_perfil;
    8.  
    9. do things ....
    10. w_perfil->header.num_sups++;
    11. ....
    12.  
    13. close_perf() {
    14. delete w_perfil; } // I dont know if it is necessary, I have a new at create_perf...
    To copy to clipboard, switch view to plain text mode 


    Ok
    I suppose that initially
    w_perfil->header.num_sups is = 0.
    w_perfil->header.num_sups are going to value 1 to 10 ( usually)
    I'm surprised that w_perfil->header.num_sups reached 671
    ( I have 671 calls to create_perf and close_perf ...)

    I have search c++`delete on the web and I discover that some people has the same problem as me.... But I keep on with the problem.

    I hope you can give some tips...
    Thanks

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Can't delete an object (instance of structure with a nested structure)

    I suppose that initially
    w_perfil->header.num_sups is = 0.
    w_perfil->header.num_sups are going to value 1 to 10 ( usually)
    I'm surprised that w_perfil->header.num_sups reached 671
    ( I have 671 calls to create_perf and close_perf ...)
    It's hard to tell from the code you posted, because its important what "do things" is doing with your structures. Also we can't say where create_perf and close_perf are called. Why do you think 671 calls to a method is too much ? Are you experiencing a memory leak or something ?
    // I dont know if it is necessary, I have a new at create_perf...
    You must delete everything allocated with new. If you call it like this:
    Qt Code:
    1. obj->create_perf();
    2. obj->create_perf();
    3. // without calling close_perf() in between
    To copy to clipboard, switch view to plain text mode 
    you have created a memory leak, because previously allocated memory assigned to w_perfil pointer is not released (w_perfil points to new memory block, old pointer value is lost, so you cannot delete it anymore)

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Can't delete an object (instance of structure with a nested structure)

    I don't see any problem with your code, I mean you don't have a problem, or at-least you are not telling us the actual problem.

    Quote Originally Posted by tonnot
    I suppose that initially
    w_perfil->header.num_sups is = 0.
    w_perfil->header.num_sups are going to value 1 to 10 ( usually)
    I'm surprised that w_perfil->header.num_sups reached 671
    ( I have 671 calls to create_perf and close_perf ...)
    it does not essentially mean that, it means you have do things .... for 671 times, in a single call of create_perf()

    Each time create_perf() is called it start with a new copy of W_perfil, and start with num_sups = 0

    Quote Originally Posted by tonnot
    // I dont know if it is necessary, I have a new at create_perf...
    it is necessary.

  7. #7
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't delete an object (instance of structure with a nested structure)

    Thanks.
    I think I have found the origin of the problem.
    I have not itiallized the default initial values of the members of my structure.
    Initializing them, I solve my problem.

    Can be this (initializing) the origin of the problem ?
    Thanks.

  8. #8
    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: Can't delete an object (instance of structure with a nested structure)

    We have no idea, you still didn't tell us what the problem is. Especially the part about "keeping my_w1 information". After all these posts and our responses you should really learn to express yourself in a clear and informative manner.
    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.


  9. #9
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Can't delete an object (instance of structure with a nested structure)

    As wysota said, no idea. Uninitialized variables can cause (m)any kinds of problems, which can cause nightmares later in the development if not found in early days. It has always been a good practice to have default ctor for a class / struct.

  10. #10
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't delete an object (instance of structure with a nested structure)

    Definitively, the problem was initialization.
    By any reason, the variables of my structs not initializated was the problem.
    Now all worsk fine.

    I could not explain why now my prog works, but it works.
    When I say that a var was "keeping my_w1 information", I really want to say "remember " (my english, sorry sorry sorry very much ). I think that as I didnt initiallized the vars, the vars remember the last value.

    So, my final conclusion is that in any way deleting an object and inmediatelly create can to have no effect. (I have seen some links about it on the web ). Am I right ?

    And I final stupid question, why C++ initialized some vars to max values (in example a int to 65535) ???? Or a bool to true ? (Is there any way to give initial value of numeric vars to zero, bools to false)

    Thanks, specially for your patience ...
    Last edited by tonnot; 26th June 2011 at 10:31.

  11. #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: Can't delete an object (instance of structure with a nested structure)

    Quote Originally Posted by tonnot View Post
    So, my final conclusion is that in any way deleting an object and inmediatelly create can to have no effect. (I have seen some links about it on the web ). Am I right ?
    No, you are wrong.

    And I final stupid question, why C++ initialized some vars to max values (in example a int to 65535) ????
    C++ doesn't initialize any of your values. They are either random values residing in the chunk of memory where the variable is created or the compiler preinitializes the variables with some known value (often called a "canary") in debug mode. Seeing such value (it's usually multibyte) should make you aware it is related to an uninitialized area of memory.
    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.


  12. #12
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Can't delete an object (instance of structure with a nested structure)

    Somebody has tell me at other forum ( about deleting)
    The OS merely marks the memory block as free - it does not have to do anything else and certainly it does not have to do anything immediately
    So if you do what you did, and immediately try to use the object again, you may find valid values.
    So, maybe a delete follow by an inmediatelly new can have no effect ?
    Thanks WY

  13. #13
    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: Can't delete an object (instance of structure with a nested structure)

    What do you mean by "no effect"? If you delete an object then its destructor gets called. Then when you create an object then it may be located in a different or the same address in memory and the constructor gets called. If you access an area of memory where an object once was and nothing overwrites this area in the meantime, you'll be reading values that used to belong to the object. If you mean that you can get the same values after reallocating an object, then yes, you can get the same values. If all memory space is preinitialized with zeroes and you don't initialize any memory yourself then every object will contain only zeroes, with or without deleting anything. But they will certainly be different objects.

    Tonnot -- programming is not only about knowing syntax of some programming language. It's also, or rather mainly, the knowledge of how computers work, why they work this way and what "physical" result each statement of your program has. It is obvious you are missing all those skills. Do everyone (yourself included) a favour and read a good book on computers and spend an hour a day on the Web reading and understanding how computers work. And I don't mean asking on forums but reading papers and books on computer science. You will save yourself a lot of time and nerves this way. And investing in a couple of English lessons (especially grammar) would be a good thing to do as well, sometimes it is very hard to understand what you want.
    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. Tree structure
    By ikm in forum Newbie
    Replies: 1
    Last Post: 7th August 2009, 20:19
  2. Folder structure of Qt SDK
    By piotr.dobrogost in forum Installation and Deployment
    Replies: 19
    Last Post: 14th July 2009, 09:17
  3. Treeview Structure
    By bismitapadhy in forum Qt Programming
    Replies: 2
    Last Post: 3rd June 2009, 13:44
  4. Data Structure help!
    By darshan in forum Newbie
    Replies: 8
    Last Post: 18th February 2009, 12:47
  5. Structure and pointers
    By zorro68 in forum General Programming
    Replies: 3
    Last Post: 22nd October 2007, 14:38

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.