Zugriffsverletzung bei Komponentenentwicklung
-
Hallo!
Ich möchte eine Komponenten entwickeln, die, wenn ich sie aktiviere, alle Komponenten auf einem Formular in einem neuen Stil zeichnet. Doch wenn ich im Entwicklungsmodus die Komponente aktiviere (ich habe hierfür eine neue Eigenschaft eingetragen), kommt es zu einer Zugriffsverletzung in einem Modul. Kann mir vielleicht jemand bei dem Problem behilflich sein?
Hier ist der Code:
void __fastcall TKomponente::SetActive(bool Value) { if (!(Value == FActive)) { FActive = Value; if (FActive) InitItems(FForm, true, true); else InitItems(FForm,false,true); } } //---------------------------------------------------------------------------- void __fastcall TKomponente::InitItems(TScrollingWinControl* wForm, bool Enable, bool Update) { int i; TComponent *Comp; for (i=0; i<=wForm->ComponentCount-1; i++) //der Fehler tritt hier auf { Comp = wForm->Components[i]; } }Der Header sieht wie folgt aus:
/--------------------------------------------------------------------------- #ifndef KomponenteH #define KomponenteH //--------------------------------------------------------------------------- #include <SysUtils.hpp> #include <Controls.hpp> #include <Classes.hpp> #include <Forms.hpp> //--------------------------------------------------------------------------- class PACKAGE TKomponente : public TComponent { private: bool FActive; TScrollingWinControl *FForm; TFont *FFont; void __fastcall SetActive(bool Value); void __fastcall SetForm(TScrollingWinControl *Value); void __fastcall InitItems(TScrollingWinControl* wForm, bool Enable, bool Update); protected: virtual void __fastcall Loaded(); public: __fastcall TKomponente(TComponent* Owner); virtual __fastcall ~TKomponente(); __property TScrollingWinControl *Form = {read = FForm, write = SetForm}; __published: __property bool Active = {read = FActive, write = SetActive}; __property TFont *Font = {read = FFont, write = FFont}; };Danke!
Gruß
Thomas
-
Hallo,
Dazu müsste man schon sehen wie deine Komponente eingesetzt wird. Ein Zugriffsverletzung an dieser Stelle wird wohl durch einen ungültigen Pointer (wForm) verursacht.
Ist vor dem Aufruf von SetActive auch FForm schon gesetzt (d.h. SetForm aufgerufen)?
-
Hallo!
Ja! SetForm ist auch schon gesetzt. Hier ist der Code dazu:
void __fastcall TKomponente::SetForm(TScrollingWinControl *Value) { bool Hold; if (Value != FForm) { Hold = Active; Active = false; FForm = Value; if (Hold) Active = true; } }Alles funktioniert, d.h. ich kann Active ändern, solange ich die Zeilen in InitItems deaktiviere.
Gruß
-
Ich kann nicht sehen, dass Du irgendwo überprüfst, ob FForm auch eine gültige Instanz ist.
Also irgendwas in der Art:void __fastcall TKomponente::InitItems(TScrollingWinControl* wForm, bool Enable, bool Update) { int i; TComponent *Comp; if (wForm) for (i=0; i<=wForm->ComponentCount-1; i++) //der Fehler tritt hier auf { Comp = wForm->Components[i]; } }Weshalb übergibst Du eigentlich der privaten Methode InitItems die Referenz auf FForm und verwendest nicht direkt Form?
Was mir noch zu dem Thema einfällt, ist, dass Du die Eigenschaft ComponentState (glaube die heißt so) abprüfen kannst. Daraus kann
man sehen, ob die Komponente bereits vollständig geladen ist. Hilfreich könnte in dem Zusammenhang u.U. auch ein Überschreiben
der Loaded()-Methode sein.Gruß,
Alexander