Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Got a weird access violation

Status
Not open for further replies.

BobbaFet

Programmer
Feb 25, 2001
903
NL
I am working on my own MSN client/bot and when someone contacts me or i contact them I make a new TabSheet that contains 2 labels, a listbox and a button.

Code that I use:
Code:
// Make a new tabsheet //
procedure TForm1.MakeNewTabSheetForNewUser(MSNUser: TMSNUser);
var mySheet:   TTabSheet;
var myLabel1, myLabel2:   TLabel;
var myListBox: TListBox;
var myButton: TButton;
var MakeANewPage: Boolean;
var i: integer;
begin
MakeANewPage := True;
for i := 0 to PageControl1.PageCount - 1 do
        begin
        if PageControl1.Pages[i].Name = RemoveAtAndDotFromEmail(MSNUser.Passport) then
                begin
                MakeANewPage := False;
                Break;
                end;
        end;

if MakeANewPage then
        begin
        mySheet   := TTabSheet.Create(PageControl1);
        myLabel1  := TLabel.Create(mySheet);
        myLabel2  := TLabel.Create(mySheet);
        myListBox := TListBox.Create(mySheet);
        myButton  := TButton.Create(mySheet);

        with mySheet do
                begin
                Parent      := PageControl1;
                PageControl := PageControl1;
                Name        := RemoveAtAndDotFromEmail(MSNUser.Passport);
                Caption     := MSNUser.Displayname;
                Visible     := True;
                TabVisible  := True;
                OnShow      := thisTabSheetShow;
                end;

        with myLabel1 do
                begin
                Parent  := mySheet;
                Caption := MSNUser.Displayname + '''s email: ';
                Top     := lbTemplate.Top;
                Left    := lbTemplate.Left;
                Visible := True;
                Name    := 'label' + IntToStr(PageControl1.PageCount);
                end;

        with myLabel2 do
                begin
                Parent  := mySheet;
                Caption := MSNUser.Passport;
                Name    := 'lb' + RemoveAtAndDotFromEmail(MSNUser.Passport);
                Top     := lbTemplateEmail.Top;
                Left    := lbTemplateEmail.Left;
                Visible := True;
                end;

        with myListBox do
                begin
                Parent  := mySheet;
                Visible := True;
                Name    := 'lib' + RemoveAtAndDotFromEmail(MSNUser.Passport);
                Left    := libTemplate.Left;
                Top     := libTemplate.Top;
                Width   := libTemplate.Width;
                Height  := libTemplate.Height;
                end;

        with myButton do
                begin
                Parent  := mySheet;
                Visible := True;
                OnClick := thisButton;
                Caption := 'X';
                Name    := 'bt' + RemoveAtAndDotFromEmail(MSNUser.Passport);
                Top     := btTemplate.Top;
                Left    := btTemplate.Left;
                Height  := btTemplate.Height;
                Width   := btTemplate.Width;
                end;

        PageControl1.ActivePage := mySheet;
        end;
end;

The button on here is to free the tabsheet. The code that I use to free the tabsheet:

Code:
procedure TForm1.thisButton(Sender: TObject);
var i: integer;
var thisTabsheet: TTabSheet;
begin
thisTabsheet := ((Sender as TButton).Parent as TTabSheet);

for i := thisTabsheet.ControlCount - 1 downto 0 do
        thisTabsheet.Controls[i].Free;

thisTabsheet.Free;
end;

When I click the button I get an Access Violation and I don't understand why that is. Anyone got any ideas?

[bobafett] BobbaFet [bobafett]
Code:
if not Programming = 'Severe Migraine' then
                       ShowMessage('Eureka!');
 
I tried setting up an application with most of your code, and was getting an "Abstract Error" when the thisButton method finished. This is because you cannot free an object within one of it's events.

I solved this by making the following changes.

When creating the TButton, use a nil owner:
Code:
myButton := TButton.Create([b]nil[/b]);

In the thisButton event, set the button's Parent property to nil, prior to freeing it.

Code:
[navy][i]// thisTabsheet := ((Sender as TButton).Parent as TTabSheet);[/i][/navy]
thisButton := TButton(Sender);
thisTabsheet := TTabSheet(thisButton.Parent);
thisButton.Parent := [b]nil[/b];
 
Problem solved, I fixed it by posting a custom message into the windows queu and have my app respond to that.

Made my own custom identifier:
Code:
[b]const CW_HURRYTHEHELLUP = WM_USER + WM_APP + 666;[/b]
type
  TForm1 = class(TForm)

Defined a procedure that would respond to it:
Code:
  { private declarations }
  procedure FreeTabsheet(var msg: TMessage); message CW_HURRYTHEHELLUP;

Code:
procedure TForm1.FreeTabsheet(var Msg: TMessage);
begin
TTabSheet(msg.WParam).Free;
end;

Call to activate the procedure:
Code:
procedure TForm1.thisButton(Sender: TObject);
begin
PostMessage(Self.Handle, CW_SCHIETNOUMAAROP, integer((Sender as TButton).Parent), 0);
end;

This allows me to have the TButton free itself!

[bobafett] BobbaFet [bobafett]
Code:
if not Programming = 'Severe Migraine' then
                       ShowMessage('Eureka!');
 
A code block tends to grab attention when browsing ...> Forums> Programmers > Development Tools > CodeGear... and rightly so because it is the most important part of any coding project.

Roo
Delphi Rules!
 
?Did I do something wrong? I don't get your comment roo0047?

Anyway, the last code-block should say:
Code:
procedure TForm1.thisButton(Sender: TObject);
begin
PostMessage(Self.Handle, CW_HURRYTHEHELLUP, integer((Sender as TButton).Parent), 0);
end;

Forgot to rename the last one.

[bobafett] BobbaFet [bobafett]
Code:
if not Programming = 'Severe Migraine' then
                       ShowMessage('Eureka!');
 
Unless he means your er colourful CONSTANTS might offend some people (not me I hasten to add)[smile]

Steve: N.M.N.F.
If something is popular, it must be wrong: Mark Twain
 
Could be, even though it is just as arbitrary as the next number ;-)

[bobafett] BobbaFet [bobafett]
Code:
if not Programming = 'Severe Migraine' then
                       ShowMessage('Eureka!');
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top