[Qt-interest] dereferencing a pointer that is invalid
Ross Driedger
ross at earz.ca
Thu Nov 5 00:10:40 CET 2009
On Wed, 04 Nov 2009 17:02:04 -0500, <qt-interest-request at trolltech.com>
wrote:
> Message: 2
> Date: Wed, 4 Nov 2009 12:36:25 -0800 (PST)
> From: Jason H <scorp1us at yahoo.com>
> Subject: Re: [Qt-interest] dereferencing a pointer that is invalid
> Would using QPointer<QClassName> help? (in the struct)
> And I wonder if you need the & in &mc[i], because you'll already get the
If you want to write it that way then, yes, it is necessary.
The C pointer notation would be:
mc + i
> address by indexing into the array without referenceing a member, IIRC.
> ----- Original Message ----
> From: navid navid <n_nnavid at yahoo.com>
> To: qt-interest at trolltech.com
> Sent: Wed, November 4, 2009 12:51:56 PM
> Subject: [Qt-interest] dereferencing a pointer that is invalid
> Hello,
> i have encountered crash at
> ========================================
> //header
> struct MyControls
> {
> QComboBox * my_combobox;
> QCheckBox * my_checkbox;
> };
> private:
> QList<MyControls *> my_control_list;
> //cpp
> MyControls mc[8];
> mc[0].my_combobox = ui->comboBox1;
> ....
> mc[7].my_combobox = ui->comboBox8;
> mc[0].my_checkbox = ui->checkBox1;
> ....
> mc[7].my_checkbox = ui->checkBox8;
> my_control_list.clear();
> for (int i=0; i<8; i++) {my_control_list.insert(i,&mc[i]);}
> if(ui->checkBox1->isChecked()) return 1;
> if (! my_control_list.at(0)->my_checkbox->isChecked()) //<- crash
> return 0;
I'm a firm believer in not using index based retrieval of objects in a
container unless it is absolutely required.
> ========================================
> the crash error is
> myapplication.exe exited with code -1073741819 //(0xC0000005)
> It looks like it is dereferencing a pointer that is invalid
What are you doing with the mc array? If it is out of scope then the
MyControls pointers of the QList are are no longer valid.
You could make the Qlist a collection of MyControls
QList<MyControls> my_control_list;
for (int i=0; i<8; i++) {my_control_list << mc[i];}
Then....
if (! my_control_list.at(0).my_checkbox->isChecked()) //These should work.
if (! my_control_list.at(1).my_checkbox->isChecked()) //
if (! my_control_list.at(2).my_checkbox->isChecked()) //
if (! my_control_list.at(7).my_checkbox->isChecked()) //
if (! my_control_list.at(8).my_checkbox->isChecked()) //By-One error --
Kaboom!
--
"My music is not difficult, my music is played badly."
Arnold Schoenberg
Ross Driedger
ross_at_earz.ca
More information about the Qt-interest-old
mailing list