Results 1 to 6 of 6

Thread: How to properly delete structures?

  1. #1
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default How to properly delete structures?

    Hello:

    I have several memory problems (my program starts to consume wild amounts of memory) in one of my applications and I'm thinking that it is because I don't properly delete certain items like structures.

    For example I've got a Structure A wich contains a QVector of reals and a QVector of pointers. I enter a function that returns a Structure B which only contains 1 Pointer and 1 Value. So I do somemething like

    Qt Code:
    1. StructB function(){
    2. StructA a;
    3. StructB b;
    4. //Do stuff that initializes a and b
    5. //In this stuff, there is a line that goes like b.pointer = a.vector[some_index];
    6. return b;
    7. }
    To copy to clipboard, switch view to plain text mode 

    How do I properly delete a? Or don't I have to?

    Then I have a different situation. Where I have a PClass which is a parent class and two Descendants class calles ClassA and ClassB. Then I have a ClassC that uses all three in the following manner:

    In ClassC.h there is a private variable declared like:
    PClass *c;

    In ClassC.cpp there is some code that says:
    Qt Code:
    1. switch (some_variable){
    2. case 0:
    3. c = new ClassA();
    4. break;
    5. case 1:
    6. c = new ClassB();
    7. break;
    8. }
    To copy to clipboard, switch view to plain text mode 

    The user might then select something else. The ClassC object is NEVER destroyed until the end of the program. So if the user selects something else before the code above I wrote the following line:

    delete c; //After this comes the switch.

    Is this the right way to resolve this? Also Both Classes B and A have an extensive ammount of QVectors... Should I destroy (using QVector::clear() command) all the vectors in the Classe's Destructor?

    Thanks for any help.

  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: How to properly delete structures?

    Quote Originally Posted by aarelovich View Post
    Qt Code:
    1. StructB function(){
    2. StructA a;
    3. StructB b;
    4. //Do stuff that initializes a and b
    5. //In this stuff, there is a line that goes like b.pointer = a.vector[some_index];
    6. return b;
    7. }
    To copy to clipboard, switch view to plain text mode 
    If you have such structure then returning b might cause a problem in itself unrelated to memory consuption. You are probably returning a pointer to a local object. Of course it depends on where "a" gets its values from...

    How do I properly delete a? Or don't I have to?
    You don't have to (and even shouldn't) delete "a" but you might have to delete its contents. (for(int i=0;i<a.size();i++) delete a[i]; or qDeleteAll(a)


    In ClassC.h there is a private variable declared like:
    PClass *c;
    "private" to what scope? Some object or is it a global variable?

    Is this the right way to resolve this?
    I'm... not sure what it is supposed to do

    Also Both Classes B and A have an extensive ammount of QVectors... Should I destroy (using QVector::clear() command) all the vectors in the Classe's Destructor?
    Vectors clear themselves upon destruction but they only destruct objects they carry, so if you have a vector of pointers, only pointers will get 'destroyed', not the objects behind them. In that case you have to call qDeleteAll() on each vector (calling clear() is not enough because of the same reason).
    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
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to properly delete structures?

    Wysota:

    Thanks a lot here is a bit more info on some of your own questions.

    In the first code a (the structure that contains the QVector of pointers) is set in yet another functions that goes soemthing like this:

    Qt Code:
    1. StructA function2(){
    2. StructA a;
    3. //Code that fills the pointer vectors. This has as many uses of the reserved word new
    4. //as vectors are needed for this function to return.
    5. returnb a;
    6. }
    To copy to clipboard, switch view to plain text mode 

    As I said before, One and only one of the potentially many pointers (Although in my current problem there are only 3) is needed. This is decided in function() and that needed pointer is assigned to b as the code showed before. So is this wrong? Should I try to do it some other way?

    The second code you wrote (the for cicle with the deletes) I have tried. however not exactly like that. See since I return one of the values in the b.pointer assignation, that specific value (when i == some_index) I don't delete, because I thought then b.pointer would be pointing at nothing. Is this wrong?
    Also this code has gotten me some weird behaviour in the code of function2. By weird behaviour I mean that the program crashes. And when I use qDebug() to find where it crashes, I get that it does so before code lines that contain local (local to function2) variable declarations. There are four declarations and the crash seems to occur on two of them. It varies on which, each time I execute the program.

    The private variable PClass *c is private to the class itself but the entire class has access to it.

    When I aske if it was the right way to resolve "ths" I was referring to my use of the
    Qt Code:
    1. delete c
    2. switch (some_variable){
    3. case 0:
    4. c = new ClassA();
    5. break;
    6. case 1:
    7. c = new ClassB();
    8. break;
    9. }
    To copy to clipboard, switch view to plain text mode 

    To properly use a new class every time the user changes what they want to do. And by right way to solve this, I mean that it doesn't cause some sort of memory leak.

    To answer your last comment, The QVectors in classes A and B (those that are declared in their respective .h files as private global class variables) are all QVectors of doubles. However there are cases where I have QVector < QVector <qreal> >. So where and how should I dispose of this? In the distructor? and how? using qDeleteAll?

    Thanks for any help.

  4. #4
    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: How to properly delete structures?

    Quote Originally Posted by aarelovich View Post
    As I said before, One and only one of the potentially many pointers
    So delete all others before returning from the function or don't use pointers at all. Explicit sharing is a nice alternative to using bare pointers.

    The second code you wrote (the for cicle with the deletes) I have tried. however not exactly like that. See since I return one of the values in the b.pointer assignation, that specific value (when i == some_index) I don't delete, because I thought then b.pointer would be pointing at nothing. Is this wrong?
    Yes, that's correct. Simply skip deleting the value at 'some index'.
    Qt Code:
    1. for(int i=0;i<vector.size();i++){
    2. if(i==some_index) continue;
    3. delete vector.at(i);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Also this code has gotten me some weird behaviour in the code of function2. By weird behaviour I mean that the program crashes.
    Yes, crashing apps are very weird

    And when I use qDebug() to find where it crashes, I get that it does so before code lines that contain local (local to function2) variable declarations. There are four declarations and the crash seems to occur on two of them. It varies on which, each time I execute the program.
    You are probably dereferencing an uninitialized pointer somewhere or you have a multithread application that tries to do what it is not supposed to.

    When I aske if it was the right way to resolve "ths" I was referring to my use of the
    Qt Code:
    1. delete c
    2. switch (some_variable){
    3. case 0:
    4. c = new ClassA();
    5. break;
    6. case 1:
    7. c = new ClassB();
    8. break;
    9. }
    To copy to clipboard, switch view to plain text mode 

    To properly use a new class every time the user changes what they want to do. And by right way to solve this, I mean that it doesn't cause some sort of memory leak.
    I still don't understand what it is supposed to do, but there is no memory leak here as long as you delete the previous value of c.

    To answer your last comment, The QVectors in classes A and B (those that are declared in their respective .h files as private global class variables) are all QVectors of doubles. However there are cases where I have QVector < QVector <qreal> >. So where and how should I dispose of this? In the distructor? and how? using qDeleteAll?
    You don't need to do anything. That's the beauty of good objective code
    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.


  5. #5
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to properly delete structures?

    Thank you so much for you answer. Although I still have a couple another question.

    What did you mean by "dereferencing an uninitialized pointer somewhere", because I thought if I did that the program would stop right there and then. I have as I had said in other post a two-threaded application. But one thread manages the gui and the other does all the work. They both don't communicate exept to start and at the end. So I'm defnitely sure that that is not it. Besides If I comment the code you have just showed me (the for with the deletes) the program works fine.

    Tomorrow when I get to work I'll try these things we've talked about and then return with some more info.

    Again thanks a lot.

    PD: What did you mean by "objective code"?

  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: How to properly delete structures?

    Quote Originally Posted by aarelovich View Post
    What did you mean by "dereferencing an uninitialized pointer somewhere", because I thought if I did that the program would stop right there and then.
    Here is an example:
    Qt Code:
    1. QPushButton *button;
    2. button->text();
    To copy to clipboard, switch view to plain text mode 
    Here (line #2) you try to dereference the pointer that is uninitialized and the application will crash.

    I have as I had said in other post a two-threaded application. But one thread manages the gui and the other does all the work. They both don't communicate exept to start and at the end. So I'm defnitely sure that that is not it.
    Don't be so sure. You might receive debug statements from one thread and at the same time the other crashes your application. You must be very careful when debugging threaded applications not to draw false conclusions.

    Besides If I comment the code you have just showed me (the for with the deletes) the program works fine.
    So you probably delete some data some other part of your application uses or you use invalid pointers.

    PD: What did you mean by "objective code"?
    Object oriented.
    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. Replies: 4
    Last Post: 19th February 2009, 12:10
  2. c++, placement delete upon exception
    By stinos in forum General Programming
    Replies: 6
    Last Post: 31st October 2006, 16: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.