Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

heap corruption detected after normal block



  • Hello
    I've got very wierd bug here , I've made a dialog in my main window ( not the first one ) , anyway the dialog work very well , and when i close the dialog nothing happen it's all ok , but when i close the main dialog after closeing the new dialog i got :
    alt text
    I've check my code and it's all ok ( even all of the feature work fine ) , but I've try something very wierd and it's work , in the dialog.h I've change the place of a varable and it's work as u can see here :

    alt text



  • Hi! Please show us you clientsdialog header and source file.



  • @Wieland here u go : download

    if u download it plz tell me :)

    Edit: Link removed -- @Wieland



  • The website you linked to tries to install plugins to my browser. I don't like malware, please just copy and paste the code here.



  • @Wieland sry about it , i didn't know that ,, anyway here :

    source:

    #include "clientsdialog.h"
    #include "ui_clientsdialog.h"
    #include <QDebug>
    #include <QSqlError>
    #include "mymsg.h"
    
    ClientsDialog::ClientsDialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::ClientsDialog)
    {
        ui->setupUi(this);
        QObject::connect(ui->name, SIGNAL(returnPressed()), this, SLOT(GetClientInfo()));
        List = new QStringList;
        HeadsTableView = new QStringList;
        client_query = new QSqlQuery;
        CartItems = new QSqlQueryModel(this);
        *HeadsTableView << "رقم الفاتورة" << "اسم المنتج" << "الباركود" << "السعر" << "الكميه" << " التخفيض" << "النوع" << "الاجمالي" << "رقم فاتورة المنتج" << "البائع" << "التاريخ" << "رقم العميل" << "تم اعاد ة المنتج" << "مدفوع";
        client_query->exec("SELECT * FROM [main].[clients]");
        while (client_query->next())
            *List << client_query->value(1).toString();
    
        search = new QCompleter(*List,this);
        search->setCaseSensitivity(Qt::CaseInsensitive);
        ui->name->setCompleter(search);
    }
    
    ClientsDialog::~ClientsDialog()
    {
        delete ui;
    }
    
    void ClientsDialog::GetClientInfo()
    {
        client_query->first();
        if(List->contains(ui->name->text()))
        {
            do
            {
                if(client_query->value(1).toString() == ui->name->text())
                {
                    ui->amount->setText(QString::number(client_query->value(2).toDouble()));
                    clientid = client_query->value(0).toUInt();
                    break;
                }
    
            }while(client_query->next());
            client_query->exec(QString("SELECT cart_id FROM [main].[sell_later_log] WHERE client_id = '%1' AND refund = 'NO' AND paid = 'NO'").arg(clientid));
            while(client_query->next())
            {
                if(ui->cart_id->findText(client_query->value(0).toString()) == -1)
                    ui->cart_id->addItem(client_query->value(0).toString());
            }
            ui->cart_id->setEnabled(true);
            ui->pushButton->setEnabled(true);
        }
    }
    
    void ClientsDialog::on_cart_id_currentIndexChanged(const QString &arg1)
    {
        double total =0;
        CartItems->setQuery(QString("SELECT * from main.sell_later_log WHERE cart_id = '%1' AND client_id = '%2' AND refund = 'NO' AND paid = 'NO'")
                            .arg(arg1.toUInt()).arg(clientid));
        for(int i=0;i<HeadsTableView->count();i++)
            CartItems->setHeaderData(i, Qt::Horizontal, HeadsTableView->at(i));
        ui->Cart->setModel(CartItems);
        ui->Cart->setColumnHidden(0,true);
        ui->Cart->setColumnHidden(8,true);
        ui->Cart->setColumnHidden(11,true);
        ui->Cart->setColumnHidden(12,true);
        ui->Cart->setColumnHidden(13,true);
        for(int i=0;i<CartItems->rowCount();i++)
            total += CartItems->data(CartItems->index(i,7)).toDouble();
        ui->total->setText(QString::number(total));
    }
    
    void ClientsDialog::on_pushButton_clicked()
    {
        QSqlDatabase::database().transaction();
        QSqlQuery query;
        for(int i=0;i<HeadsTableView->count();i++)
            query.exec(QString("UPDATE main.sell_later_log SET paid = 'YES' WHERE cart_id ='%1' AND barcode = '%2' AND amount = '%3' AND disscount = '%4' AND type = '%5' AND bill_number = '%6' ")
                       .arg(CartItems->data(CartItems->index(i,0)).toUInt()).arg(CartItems->data(CartItems->index(i,2)).toUInt()).arg(CartItems->data(CartItems->index(i,4)).toUInt()).arg(CartItems->data(CartItems->index(i,5)).toDouble())
                       .arg(CartItems->data(CartItems->index(i,6)).toString()).arg(CartItems->data(CartItems->index(i,8)).toUInt()));
        query.exec(QString("UPDATE main.clients SET cash = cash - '%1' WHERE name = '%2'").arg(ui->total->text().toDouble()).arg(ui->name->text()));
        QSqlDatabase::database().commit();
        ClientFinishPaymentMsg();
        ClientsDialog::close();
    }
    
    

    header:

    #ifndef CLIENTSDIALOG_H
    #define CLIENTSDIALOG_H
    
    #include <QDialog>
    #include <QSqlQuery>
    #include <QCompleter>
    #include <QSqlQueryModel>
    
    namespace Ui {
    class ClientsDialog;
    }
    
    class ClientsDialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit ClientsDialog(QWidget *parent = 0);
        ~ClientsDialog();
    private slots:
        void GetClientInfo();
        void on_cart_id_currentIndexChanged(const QString &arg1);
    
        void on_pushButton_clicked();
    
    private:
        Ui::ClientsDialog *ui;
        QStringList *List;
        QStringList *HeadsTableView;
        QSqlQuery *client_query;
        QSqlQueryModel *CartItems;
        QCompleter *search;
        unsigned int clientid;
    };
    
    #endif // CLIENTSDIALOG_H
    
    

  • Lifetime Qt Champion

    @MrLibya Is there a reason why you use pointers and allocate memory on the heap? In the destructor you do not free the memory you allocated in the constructor.

    QStringList *List;
    QStringList *HeadsTableView;
    QSqlQuery *client_query;
    QSqlQueryModel *CartItems;
    QCompleter *search;
    

    Why not just use normal variables?

    QStringList List;
    QStringList HeadsTableView;
    QSqlQuery client_query;
    QSqlQueryModel CartItems;
    QCompleter search;
    

  • Lifetime Qt Champion

    @MrLibya Just a note: you should stick to one naming convention. Currently you're mixing several different conventions:

    • HeadsTableView - camel case with first upper letter
    • client_query - underscore as delimiter
      Together with all this unnecessary pointer mess (sorry for being so harsh :-)) it looks really ugly.

    I would recommend to not to name variables/methods like HeadsTableView - the first letter should be lower case, else it looks like class name. headsTableView is better.



  • @jsulm I've missunderstand the pointer in Qt , cuz I've seen that Qt is auto destroy the pointers ,
    now i've got a look on this link
    and now I"m understand it very will , i will fix it thx :)


  • Lifetime Qt Champion

    @MrLibya Qt only destroys objects which have a parent derived from QObject. Most of the objects you create do not have a parent.
    And again: why do you use pointers? In your case there is no need for pointers and heap allocation via new at all.


Log in to reply