Results 1 to 12 of 12

Thread: deleting invalid pointer

  1. #1
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default deleting invalid pointer

    This is a C++ issue rather than a QT issue in my view hence the post here.

    When I run the functions below I get

    "Program received signal SIGABRT, Aborted.
    free(): invalid next size (fast): 0x083101b0 ***"
    plus a hundred or so lines of related errors.

    I believe I am deleting an invalid pointer but cannot see where I am going wrong. What I'm trying to do is write two functions that gathers the non empty and non txt containing cells from my spreadsheet selected range then sums the numbers and puts the answer in the cell below the selected range. If there is only one number the functions work, if the range has two or more cells than I get the crash above. Can anyone help sort this out?

    Qt Code:
    1. void Spreadsheet::sumColumn() // sums selected cells and dumps answer below lowest cell
    2. {
    3. QTableWidgetSelectionRange range = selectedRange(); // get selected range
    4. int col=currentColumn(); //get current column position
    5. int rowcount= range.rowCount(); // work out number of rows selected
    6. int *number= new int; // create pointer to number of items to be collected
    7. double *dataSet = new double; // create pointer for array of data to be collected
    8. double total=0; // initialise total
    9. collectData(number, dataSet); // get data from sheet see next function
    10. int numberOfItems=0; // set this to zero just in case
    11. numberOfItems=(int) *number; // cast to make numberOfItems = number of filled cells from collect function
    12.  
    13. for (int n=0; n<numberOfItems; n++)
    14.  
    15. total= total+ dataSet[n]; // calculate sum
    16.  
    17. QString num = QString::number(total); // converts any number type to string -very easy!
    18.  
    19. setFormula(rowcount, col, num); // put sum on sheet in cell below selected range this works
    20.  
    21. // in future preferably needs check for overwriting of cell if not empty
    22. // also needs to add row if all rows in sheet are selected
    23.  
    24. delete number; // clean up memory
    25. delete dataSet;
    26. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Spreadsheet::collectData(int *count, double *data)
    2. {
    3.  
    4. QList<QTableWidgetItem *> items = selectedItems();
    5.  
    6. int totalcount=0;
    7.  
    8. foreach (QTableWidgetItem *item, items) // loop through sheet selected items
    9. {
    10. bool ok;
    11. double value = item->text().toDouble(&ok);
    12.  
    13. if (ok && !item->text().isEmpty()) // ignore empty cells and cells with anything but 0-9. in
    14. {
    15. //data[totalcount]=0.0; // set each element to zero just in case
    16. data[totalcount]=value; // now fill array with values from sheet
    17. totalcount++; //increment count for only filled cells with numbers in
    18. }
    19.  
    20. }
    21. *count=totalcount; // set pointer to total count
    22. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: deleting invalid pointer

    Congratulations. You 'newed' memory for exactly one double value. And filled it with totalcount values. Nice example of memory corruption.

  3. #3
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: deleting invalid pointer

    Thanks for your reply I thought pointers and arrays were the same thing? What do I do instead?

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: deleting invalid pointer

    You still have to tell the compiler how much memory to allocate. If you intend on using 1 double your code is fine. If you intend on using 2 doubles, you would have to use "double *dataSet = new double[2];" and so on. Your delete code would then change too from "delete dataSet" to "delete[] dataSet".

  5. #5
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: deleting invalid pointer

    Thanks for your reply. I'm aware of how to declare the size for known size, but the problem is the size is unknown before I pass the pointer into the collect function. I can think of one way around this which is to use rowcount (and columncount) from the range to set the array size but this would allocate more memory than I need since it counts empty cells.

  6. #6
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: deleting invalid pointer

    Quote Originally Posted by hollowhead View Post
    Thanks for your reply. I'm aware of how to declare the size for known size, but the problem is the size is unknown before I pass the pointer into the collect function. I can think of one way around this which is to use rowcount (and columncount) from the range to set the array size but this would allocate more memory than I need since it counts empty cells.
    Why not use QVector?

  7. #7
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: deleting invalid pointer

    That's a brilliant idea Kumosan since it can declared at one size and dynamically resized. However, I've not used anything like this before. C++ GUI Programming with Qt 4 shows how to declare it and use it a bit. Any advice on its use in this case would be most welcome....

    Declare it thus as a pointer? in my main class declaration? and how do I pass it into my collectData function?

    Qt Code:
    1. QVector<double> *myArray(10);
    To copy to clipboard, switch view to plain text mode 

    Presumably I can do something like this to fill it.

    Qt Code:
    1. foreach (QTableWidgetItem *item, items) // loop through sheet selected items
    2.  
    3. {
    4.  
    5. bool ok;
    6.  
    7. double value = item->text().toDouble(&ok);
    8.  
    9. if (ok && !item->text().isEmpty()) // ignore empty cells and cells with anything but 0-9. in
    10.  
    11. {
    12.  
    13. //data[totalcount]=0.0; // set each element to zero just in case
    14.  
    15. myArray.append(value); // now fill array with values from sheet
    16. //or
    17. myArray[totalcount]=value; // now fill array with values from sheet
    18.  
    19. totalcount++; //increment count for only filled cells with numbers in
    20.  
    21. }
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: deleting invalid pointer

    Nope, though you can use it as pointer, it is not too good.

    In your .h file:
    QVector<double> myArray;

    In your .cpp:

    myArray.append(value);

    Do not:
    myArray[totalcount]=value; // now fill array with values from sheet
    With the append you already have added the values.

    You don't need totalcount at all.
    Below how your collectData could look like

    Qt Code:
    1. void Spreadsheet::collectData(QVector<double> &arr) {
    2. QList<QTableWidgetItem *> items = selectedItems();
    3. foreach (QTableWidgetItem *item, items) // loop through sheet selected items {
    4. bool ok;
    5. double value = item->text().toDouble(&ok);
    6. if (ok && !item->text().isEmpty()) // ignore empty cells and cells with anything but 0-9. in {
    7. arr.append(value);
    8. }
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    You don't need totalcount. You can always get the count from myArray.size();
    Last edited by Kumosan; 28th April 2010 at 16:16. Reason: spelling corrections

  9. #9
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: deleting invalid pointer

    Cheers mate makes sense. Thanks for your help have programmed in C but not C++ until recently. Hence never used the std C++ vector either. You learn from your mistakes.....

  10. #10
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: deleting invalid pointer

    Thanks worked like a dream two things I discovered which I will add for those who maybe interested in the future in this thread.
    The easiest way to iterate over the vectors filled range is to use the count() function
    Qt Code:
    1. for (int n=0; n<myArray.count(); n++)
    2.  
    3. total+=myArray[n]; // calculate sum
    To copy to clipboard, switch view to plain text mode 

    In addition using append on its own in the collect function meant the answer was only correct the first time subsequent time the answer was wrong since more and more data was being added to the array and non was being deleted. The way round this is when you have finished with the data in the vector

    Qt Code:
    1. myArray.clear();
    To copy to clipboard, switch view to plain text mode 

    This seems according to the documentation to clear everything and release the memory.

  11. #11
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: deleting invalid pointer

    Quote Originally Posted by hollowhead View Post
    Thanks worked like a dream two things I discovered which I will add for those who maybe interested in the future in this thread.
    The easiest way to iterate over the vectors filled range is to use the count() function
    Qt Code:
    1. for (int n=0; n<myArray.count(); n++)
    2.  
    3. total+=myArray[n]; // calculate sum
    To copy to clipboard, switch view to plain text mode 
    Easiest way?
    What about:

    Qt Code:
    1. foreach(double v, myArray)
    2. total += v;
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: deleting invalid pointer

    Fantastic I'm a fan of QVector, already its really powerful. I'll change the code below I know it worked with foreach but not how. I have posted a follow-up question following on from above I hope you don't mind... All the code below works but I don't fully understand why and I think there is a better way of doing things but all attempts to do so have failed hence the post. I wanted to put my sum() function in a separate source file so I created a stats.cpp and stats.h thus;

    Qt Code:
    1. #ifndef STATS_H
    2. #define STATS_H
    3.  
    4. class Stats
    5. {
    6. public:
    7.  
    8. double sum(QVector<double> &dataArray);
    9.  
    10. private:
    11.  
    12. QVector<double> dataArray;
    13.  
    14. };
    15.  
    16. #endif
    To copy to clipboard, switch view to plain text mode 

    and stats.cpp file

    Qt Code:
    1. #include "stats.h" /* contains function prototypes and class defition */
    2.  
    3. /*Stats::Stats()
    4. {
    5.  
    6. }
    7.  
    8. */
    9.  
    10. /* first sum */
    11.  
    12. double Stats::sum(QVector<double> &dataArray)
    13.  
    14. {
    15.  
    16. double sum=0.0;
    17.  
    18. for (int n=0; n<dataArray.count(); n++)
    19. sum+=dataArray[n]; // calculate sum
    20. // dataArray.clear();
    21. return sum;
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    then modified my sumColumn() function to

    Qt Code:
    1. double total=0; // initialise total
    2. collectData(myArray); // get data from sheet see next function
    3. Stats myStats;
    4. total=myStats.sum(myArray);
    5. // etc as before
    To copy to clipboard, switch view to plain text mode 

    It works but I am unhappy about two things.
    The first is that I've instantiated an object of the Stats class in my sumColumn() function which works but seems clumsy. However, I try declaring it anywhere else it won't compile. I've tried spreadsheet class definition (since sumColumn() is called within this class) and the spreadsheet header file (Public:) also stats.h (same). I also created a stats constructor but it won't compile with it there. If I write more functions in stats I will have to do this each time within each function and that is naff.

    The second question is do I need a stats constructor? Its just a library with a math function in. If I create a minimalist one commented out in the code above it won't compile. My understanding is that C++ creates one with everything public if non is declared?

Similar Threads

  1. deleting internal pointer in QModelIndex
    By rickbsgu in forum Qt Programming
    Replies: 18
    Last Post: 24th December 2008, 03:24
  2. *** glibc detected ***: free(): invalid pointer
    By codebehind in forum Qt Programming
    Replies: 4
    Last Post: 20th September 2008, 23:45
  3. itemChange() glibc invalid pointer
    By dreamer in forum Qt Programming
    Replies: 2
    Last Post: 3rd May 2008, 10:04
  4. QSqlRecord becoming invalid
    By mikro in forum Newbie
    Replies: 5
    Last Post: 3rd October 2006, 19:00
  5. invalid sizeHint()
    By roleroz in forum Qt Programming
    Replies: 2
    Last Post: 25th August 2006, 23:11

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.