[Qt-interest] Mixing QT and non QT data structures in c++ causes runtime error
Ross Driedger
ross at earz.ca
Sat Feb 6 21:02:36 CET 2010
On 6-Feb-10, at 2:15 PM, qt-interest-request at trolltech.com wrote:
> Message: 3
> Date: Sat, 6 Feb 2010 17:00:26 +0000 (GMT)
> From: F B <fbabetz at yahoo.it>
> Subject: [Qt-interest] Mixing QT and non QT data structures in c++
> causes runtime error
> To: qt-interest at trolltech.com
> Message-ID: <681183.92890.qm at web27904.mail.ukl.yahoo.com>
> Content-Type: text/plain; charset=utf-8
>
> Hi,
> I'm doing a little software project and I have to separate the
> logical part of this program from the graphical interface; I used
> standard c++ data structures to do the logical part (for example,
> arrays allocated into the heap, strings, etc.) and obviously, some
> custom classes that inherit from QWidget or other QT classes for the
> graphical part.
> Every part compiles and works fine but when I add the logical
> component as data field in the graphical part I obtain a runtime
> error.
There is nothing stopping you from using non-Qt data structures in
your classes. I do it all the time, If you want to really separate
data from the graphics, I would recommend that you develop a model-
view-controller kind of architecture.
BTW, some purists will point out that it is Qt and not 'QT'.
>
> This is a simplified version of my code:
>
> //file.h
> GraphicalClass : public QWidget
> {
> private:
> Logical_Data_field t; // adding this data field in the structure
> cause a runtime error
> QT_data_field** gc;
> QGridLayout* l;
> public:
> GraphicalClass(int =20); // constructor
> };
>
> //file.cpp
> #include"file.h"
> GraphicalClass::GraphicalClass(int nc): t(nc), gc(new
> QT_data_field*[nc]), l(new QGridLayout())
> {
> // (...)
> }
>
> The Logical_Data_field is structured in this way:
> //file.h
> class Logical_Data_field
> {
> private:
This looks like the 'C way of doing things. Since you are using C++ I
would highly recommend using either stl or Qt's container classes.
stl will let you do all you need to do holding vectors inside of
vectors without having to worry about memory allocation.
> const int numberofbox; // number of cell of the array
> cas** c; // array of pointers allocated into the heap
> ind n;
> public:
> // constructor, destructor and other methods
> };
>
> The error I obtained is this:
> *** glibc detected *** ./GraphicalClass: malloc(): memory
> corruption: 0x0898d590 ***
> followed by a backtrace.
First of all, do not use malloc() in C++. new is superior in very
many ways. Also, do not 'free' memory that has been new'd -- delete
it instead.
>
>
> I also tried to initialize the Logical_Data_field in the
> GraphicalClass with a "new", but the error is still present.
> If I put that Data_field inside the code of the constructor of the
> GraphicalClass the program run but I obtain frequent segmentation
> fault launching it.
Just a suspicion that you have a loose pointer somewhere. Can't tell
too much from what you have posted.
Ross Driedger
ross_at_earz.ca
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100206/df8a19dc/attachment.html
More information about the Qt-interest-old
mailing list