admin管理员组

文章数量:1317915

For my internship, I'm developing a simple tcl Windows-only app using C++Builder 12.

The app is used to do some simple CRUD with an interface. I'm using SQLite as a database, and also FireDAC, although that may not be relevant.

The app is divided into 2 main menus and one of them does not seem to obey the same rules.

Whenever I click on "nouvelle rallonge" (new extension) or "nouveau tube" (new tube), I call another window as well as Append() on the right table, and after filling the required field(s) I Post() it.

The problem is that the Append() acts weirdly. On the rest of the app, it works as intended. But here, it gets the value of the already-existing entry in the database (displayed in the TDBGrid) and I can modify it, but I cannot create a new entry.

I use the same window "architecture" (i.e : using TDBEdit) and code logic as the other pages where it works perfectly fine.

Here is the code causing issues:

2nd main page :

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "menuApp.h"
#include "menuRallonges.h"
#include "datamodule.h"
#include "gestionRallonge.h"
#include "gestionTube.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFMenuRallonges *FMenuRallonges;
extern bool isEditing;
//---------------------------------------------------------------------------
__fastcall TFMenuRallonges::TFMenuRallonges(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFMenuRallonges::btRallongeClick(TObject *Sender)
{
    TFRallonge *FRallonge = new TFRallonge(this);
    short res = FRallonge->ShowModal();
    delete FRallonge;

}
//---------------------------------------------------------------------------
void __fastcall TFMenuRallonges::btTubeClick(TObject *Sender)
{
    TFTube *FTube = new TFTube(this);
    short res = FTube->ShowModal();
    delete FTube;

}
//---------------------------------------------------------------------------

void __fastcall TFMenuRallonges::FormShow(TObject *Sender)
{
   DataModule1 = new TDataModule1(this);
    isEditing=false;
    DBGrid1->DataSource->DataSet->Refresh();
    DBGrid2->DataSource->DataSet->Refresh();
}
//---------------------------------------------------------------------------

void __fastcall TFMenuRallonges::btModifRallongeClick(TObject *Sender)
{
   isEditing=true;
   btRallongeClick(this);
   FormShow(this);
}
//---------------------------------------------------------------------------

void __fastcall TFMenuRallonges::btModifTubeClick(TObject *Sender)
{
   isEditing=true;
   btTubeClick(this);
    FormShow(this);
}
//---------------------------------------------------------------------------


void __fastcall TFMenuRallonges::FormDestroy(TObject *Sender)
{
    if (DataModule1 != nullptr)
    {
        delete DataModule1;
        DataModule1 = nullptr;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFMenuRallonges::FormClose(TObject *Sender, TCloseAction &Action)
{
    FormDestroy(this);
}
//---------------------------------------------------------------------------

Tube page:

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "datamodule.h"
#include "menuRallonges.h"
#include "gestionTube.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFTube *FTube;
extern bool isEditing;
//---------------------------------------------------------------------------
__fastcall TFTube::TFTube(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFTube::Button1Click(TObject *Sender)
{
    Button1->SetFocus();
    try {
        DataModule1->TubevideTable->Post();
    }
    catch (const Exception &E) {
        ShowMessage("Erreur lors de l'insertion : " + E.Message);
    }
}
//---------------------------------------------------------------------------

void __fastcall TFTube::FormShow(TObject *Sender)
{

    if (isEditing==true) {
//      ShowMessage("editing true");
    }
    else
    {
//      ShowMessage("editing false");
//      DataModule1->TubevideTable->Last();
        DataModule1->TubevideTable->Append();

    }

}
//---------------------------------------------------------------------------
void __fastcall TFTube::Button2Click(TObject *Sender)
{
    DataModule1->TubevideTable->Cancel();
}
//---------------------------------------------------------------------------

I don't really know what is causing this, so if you have any idea please tell me.

For my internship, I'm developing a simple tcl Windows-only app using C++Builder 12.

The app is used to do some simple CRUD with an interface. I'm using SQLite as a database, and also FireDAC, although that may not be relevant.

The app is divided into 2 main menus and one of them does not seem to obey the same rules.

Whenever I click on "nouvelle rallonge" (new extension) or "nouveau tube" (new tube), I call another window as well as Append() on the right table, and after filling the required field(s) I Post() it.

The problem is that the Append() acts weirdly. On the rest of the app, it works as intended. But here, it gets the value of the already-existing entry in the database (displayed in the TDBGrid) and I can modify it, but I cannot create a new entry.

I use the same window "architecture" (i.e : using TDBEdit) and code logic as the other pages where it works perfectly fine.

Here is the code causing issues:

2nd main page :

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "menuApp.h"
#include "menuRallonges.h"
#include "datamodule.h"
#include "gestionRallonge.h"
#include "gestionTube.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFMenuRallonges *FMenuRallonges;
extern bool isEditing;
//---------------------------------------------------------------------------
__fastcall TFMenuRallonges::TFMenuRallonges(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFMenuRallonges::btRallongeClick(TObject *Sender)
{
    TFRallonge *FRallonge = new TFRallonge(this);
    short res = FRallonge->ShowModal();
    delete FRallonge;

}
//---------------------------------------------------------------------------
void __fastcall TFMenuRallonges::btTubeClick(TObject *Sender)
{
    TFTube *FTube = new TFTube(this);
    short res = FTube->ShowModal();
    delete FTube;

}
//---------------------------------------------------------------------------

void __fastcall TFMenuRallonges::FormShow(TObject *Sender)
{
   DataModule1 = new TDataModule1(this);
    isEditing=false;
    DBGrid1->DataSource->DataSet->Refresh();
    DBGrid2->DataSource->DataSet->Refresh();
}
//---------------------------------------------------------------------------

void __fastcall TFMenuRallonges::btModifRallongeClick(TObject *Sender)
{
   isEditing=true;
   btRallongeClick(this);
   FormShow(this);
}
//---------------------------------------------------------------------------

void __fastcall TFMenuRallonges::btModifTubeClick(TObject *Sender)
{
   isEditing=true;
   btTubeClick(this);
    FormShow(this);
}
//---------------------------------------------------------------------------


void __fastcall TFMenuRallonges::FormDestroy(TObject *Sender)
{
    if (DataModule1 != nullptr)
    {
        delete DataModule1;
        DataModule1 = nullptr;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFMenuRallonges::FormClose(TObject *Sender, TCloseAction &Action)
{
    FormDestroy(this);
}
//---------------------------------------------------------------------------

Tube page:

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "datamodule.h"
#include "menuRallonges.h"
#include "gestionTube.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFTube *FTube;
extern bool isEditing;
//---------------------------------------------------------------------------
__fastcall TFTube::TFTube(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFTube::Button1Click(TObject *Sender)
{
    Button1->SetFocus();
    try {
        DataModule1->TubevideTable->Post();
    }
    catch (const Exception &E) {
        ShowMessage("Erreur lors de l'insertion : " + E.Message);
    }
}
//---------------------------------------------------------------------------

void __fastcall TFTube::FormShow(TObject *Sender)
{

    if (isEditing==true) {
//      ShowMessage("editing true");
    }
    else
    {
//      ShowMessage("editing false");
//      DataModule1->TubevideTable->Last();
        DataModule1->TubevideTable->Append();

    }

}
//---------------------------------------------------------------------------
void __fastcall TFTube::Button2Click(TObject *Sender)
{
    DataModule1->TubevideTable->Cancel();
}
//---------------------------------------------------------------------------

I don't really know what is causing this, so if you have any idea please tell me.

Share Improve this question edited Jan 30 at 1:53 Remy Lebeau 598k36 gold badges503 silver badges848 bronze badges asked Jan 28 at 9:04 Alan FinitiAlan Finiti 6510 bronze badges 9
  • Why is this post tagged with delphi and pascal? – Fred62 Commented Jan 28 at 9:18
  • @Fred62 my bad, since c++ Builder uses delphi too and since github told me the code was majnly pascal i thought it was relevant, but yes i should remove those tags thanks. – Alan Finiti Commented Jan 28 at 9:40
  • @Jarod42 VCL components are always allocated on the heap. That's how the whole GUI library works. You can't allocate them on the stack so your comment isn't helpful. – Lundin Commented Jan 28 at 11:32
  • Are you sure you should be calling this Append method from a FormShow event? Sounds fishy. Normally FormShow is what gets executed when the window reappears from being minimized or hidden behind other stuff. Similarly DataModule1 = new TDataModule1(this); from another FormShow is fishy, this looks like a memory leak bug. I'm not really following this code at all. Mixing up FormShow vs FormCreate? – Lundin Commented Jan 28 at 11:35
  • 1 @AlanFiniti why are you creating a new instance of TDataModule1 on each button click? That is very suspicious. You are not destroying the previous instance, so it just hangs around in memory until the Form is destroyed. Also, your OnDestroy handler is redundant since you are assigning this as the Owner of each TDataModule1 instance. – Remy Lebeau Commented Jan 30 at 1:58
 |  Show 4 more comments

1 Answer 1

Reset to default 2

As @RemyLebeau said, the creation of a new instance of 'DataModule1' is suspicious. Even if it worked fine on the other page, i don't really know why but on the other it was causing the bug this post's about. I just removed the line from the functions and now everything works fine, thanks everyone !

本文标签: