access violation when accessing public class variable from within class, but not from outside class

access violation when accessing public class variable from within class, but not from outside class

Post by netmuzi » Sun, 22 Apr 2007 03:44:52


I'm writing a component that will allow the user to "snap" a window to
the side of an MDI window, which will create a panel that will
represent the auto-hide window. The problem is, no matter what I do
to try and store a pointer to the form in my TDockingPanel class, I
get an access violation when attempting to access it from within the
class. However, when I access the pointer from outside the class, I
get no access violation, and everything is correct! Here's the code:

class PACKAGE TDockingPanel : public TPaintBox
{
private:
TNotifyEvent FOnMouseEnter;
TNotifyEvent FOnMouseLeave;
AnsiString FCaption;
bool MouseIsOver;
bool Down;
TTimer *Timer;
AnsiString FPanelForm;
void __fastcall CustomPaint(TObject *Sender);
void __fastcall ClickEvent(TObject *Sender);
void __fastcall TimerEvent(TObject *Sender);
void __fastcall MouseEnter(TObject *Sender);
void __fastcall MouseLeave(TObject *Sender);
void __fastcall WriteCaption(AnsiString c);
void __fastcall GrowRect(TRect &R, int pixels);
HFONT __fastcall CreateVerticalFont();
void __fastcall CenterText(AnsiString text);
void __fastcall RevealForm();
void __fastcall HideForm();
protected:
void __fastcall WndProc(TMessage &Message);
public:
__fastcall TDockingPanel(TComponent* Owner);
TForm *f; // the form associated with this TDockingPanel
__published:
__property AnsiString Caption = {read = FCaption, write =
WriteCaption};
__property TNotifyEvent OnMouseEnter = {read = FOnMouseEnter,
write = FOnMouseEnter};
__property TNotifyEvent OnMouseLeave = {read = FOnMouseLeave,
write = FOnMouseLeave};
};

// an example member function which gives an access violation:

void __fastcall TDockingPanel::RevealForm()
{
if (f)
{
ShowMessage(f->Caption);
}
}

// a simplified example which does NOT give an access violation

void __fastcall TForm2::Start1Click(TObject *Sender)
{
DockingPanel1->f = Form1;
ShowMessage(DockingPanel1->f->Caption);
}

How could this be? I'm using BCB 5. The component is included in
package dclusr50. It doesn't make much sense. By the way,
TDockingPanel::RevealForm() is generated on a MouseEnter event, which
I trigger AFTER the form's address has been assigned to f. CodeGuard
says I have an access overrun in Start1Click. I don't know what this
means or how to fix it. I've tried making f a property of
TDockingPanel, but that didn't work either.

TIA.