Access Violation beim Form Pointer
-
class AppMan{ public: AppMan( TForm1 * Form1 ):TimerCount(0),Form(Form1){} void IncreaseCount(); private: short TimerCount; TForm1 * Form; }; void AppMan::IncreaseCount(){ if ( ++TimerCount == 2 ){ Form1->Label2->Visible = true; // Access Violation }else if ( TimerCount == 4 ){ Form1->Button1->Visible = true; } }
Hi, ich krieg beim ausführen der funktion IncreaseCount mittels einem Timer eine Access Violation, verstehe aber nicht ganz wieso, der Form pointer wird über den konstruktor der form gesetzt und zeigt auch auf das richtige, die komponenten sind published, also gleichzusetzen mit public.
Edit: Ein geiles datum heute...
-
Hi,
ohne AppMan-Konstruktor und mit Setzen von TimerCount im Code läuft es bei mir. Poste doch bitte
den relevaten Rest.mfg
kpeter
-
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" #define SAFE_DEL(p) { if ( p != NULL ){ delete p; p = NULL; } } class AppMan{ public: AppMan( TForm1 * Form1 ):TimerCount(0),Form(Form1){} void IncreaseCount(); private: short TimerCount; TForm1 * Form; }; void AppMan::IncreaseCount(){ if ( ++TimerCount == 2 ){ Form1->Label2->Visible = true; // Access Violation }else if ( TimerCount == 4 ){ Form1->Button1->Visible = true; } } //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1 = NULL; AppMan * Manager = NULL; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Manager = new AppMan(Form1); Timer1->Enabled = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender) { Manager->IncreaseCount(); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormDestroy(TObject *Sender) { SAFE_DEL(Manager); } //---------------------------------------------------------------------------
-
Hi,
die Klasse AppMan ist bei folgendem Code auch im Header (beide Dateien ungekürzt):
main.h
//--------------------------------------------------------------------------- #ifndef mainH #define mainH //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> #include <ExtCtrls.hpp> //--------------------------------------------------------------------------- #define SAFE_DEL(p) { if ( p != NULL ){ delete p; p = NULL; } } class TForm1; // Vorwärtsdekl. class AppMan{ public: AppMan( TForm1 * Form1 ):TimerCount(0),Form(Form1){} void IncreaseCount(); short TimerCount; // nur zum Test auf public, normal private private: TForm1 * Form; }; class TForm1 : public TForm { __published: TTimer *Timer1; TButton *Button1; TLabel *Label1; TLabel *Label2; void __fastcall FormDestroy(TObject *Sender); void __fastcall Timer1Timer(TObject *Sender); private: public: __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif
main.cpp
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "main.h" #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1 = NULL; AppMan * Manager = NULL; //--------------------------------------------------------------------------- void AppMan::IncreaseCount(){ if ( ++TimerCount == 2 ){ Form1->Label2->Visible = true; // Access Violation ??? }else if ( TimerCount == 4 ){ Form1->Button1->Visible = true; } } //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Manager = new AppMan(Form1); Timer1->Enabled = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormDestroy(TObject *Sender) { SAFE_DEL(Manager); } //--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender) { Manager->IncreaseCount(); Label1->Caption = IntToStr(Manager->TimerCount); }
Der Code läuft sauber durch.
mfg
kpeter
-
nach dem verschieben des
TForm1 *Form1 = NULL;
vor die funktions deklaration hats geklappt.
danke.