Board index » delphi » Delphi1: GPF everywhere!

Delphi1: GPF everywhere!

David Preece <dmpre...@dreams.co.uk> wrote

Quote
> Hi.

> I'm just on the sharp bit of the Delphi learning curve (already know C++
> and VB - it seemed to be a logical move) and have hit a hitch. I have an
> MDI form that creates children with TMDIChild.Create(self) to let the
child
> objects form, but keep a pointer to the parent. The child menus auto
merge
> OK with the parent, and the child window appears to be handling the
> messages from the above menus.

> However, in one of the child window handlers I am trying to call a
> procedure in the parent form by casting the 'parent' pointer to
TMDIParent
> (C++ habit, do I need to do this?) with:

Yes, you have to do the cast just like in C++. The syntax
TMDIParent(Parent)
does no error checking, BTW. The syntax (Parent as TDMIParent) will raise
an exception immediately if the type cast is invalid. But if you know
the cast is valid, the first syntax is quite OK and faster.

Quote
>   procedure TMDIChild.Something1Click(Sender: TObject);
>   begin
>     TMDIParent(Parent).Procedure;
>   end;

> [needless to say TMDIParent and TMDIChild are the forms in question]

> ..which calls the procedure fine, but GPF's if I try to use a member
> variable of TMDIParent within the procedure. The GPF doesn't happen if I
> call the procedure from a button on the TMDIParent form.

These GPF's certainly sound strange. I can't see why they should happen.
But this seems like a case where a global variable is "allowed".
var
  GlobalMDIParent: TMDIParent;
There can be only one MDI parent form in an application, and it
is a truly global object. So on FormCreate of the MDI parent,
put e.g. GlobalMDIParent := Self, and use GlobalMDIParent.Procedure.

Quote
> Whilst on the subject, am I right in thinking that when I declare a
> variable of type TMyType (and .Create it), only a reference to it gets
put
> on the stack and the object itself gets put in the heap?

You're quite right. Object Pascal never allocates objects on the stack.
This is one of the minor "valid" bashing subjects against Pascal
from C++ folks. It slows down the use of local/temporary objects a little.
But it also simplifies parameter passing and reduces the risk for
additional GPF's caused by late night programming, a habit that
will be around for a while, I think  :)

Best wishes,
Matthias Bolliger

 

Re:Delphi1: GPF everywhere!


Quote
Matthias Bolliger wrote:

> David Preece <dmpre...@dreams.co.uk> wrote
> > However, in one of the child window handlers I am trying to call a
> > procedure in the parent form by casting the 'parent' pointer to
> TMDIParent
> > (C++ habit, do I need to do this?) with:

> Yes, you have to do the cast just like in C++. The syntax
> TMDIParent(Parent)
> does no error checking, BTW. The syntax (Parent as TDMIParent) will raise
> an exception immediately if the type cast is invalid. But if you know
> the cast is valid, the first syntax is quite OK and faster.

if you want to make sure that you are not about to cause GPF, good thing
is to do this:

if parent is TMDIParent then
begin
  TMDIParent(parent).procedure;
end;

"is" is similar to i.e. in JAVA "instanceof", which tells you, whether
the pointer "parent" points to the class derived from TMDIParent (and
so, whether it is legal to call TMDIParent functions)

--
It's not a fault of Windows NT, it's a fault of Windows 95
----------------------------------------------------------
DSpa...@rotocast.com, d...@cyclelogic.com

Re:Delphi1: GPF everywhere!


Hi.

I'm just on the sharp bit of the Delphi learning curve (already know C++
and VB - it seemed to be a logical move) and have hit a hitch. I have an
MDI form that creates children with TMDIChild.Create(self) to let the child
objects form, but keep a pointer to the parent. The child menus auto merge
OK with the parent, and the child window appears to be handling the
messages from the above menus.

However, in one of the child window handlers I am trying to call a
procedure in the parent form by casting the 'parent' pointer to TMDIParent
(C++ habit, do I need to do this?) with:

  procedure TMDIChild.Something1Click(Sender: TObject);
  begin
    TMDIParent(Parent).Procedure;
  end;

[needless to say TMDIParent and TMDIChild are the forms in question]

..which calls the procedure fine, but GPF's if I try to use a member
variable of TMDIParent within the procedure. The GPF doesn't happen if I
call the procedure from a button on the TMDIParent form.

What's going on? I really don't want to have to use VB ever again, but I
must rid myself of these GPF's.

Whilst on the subject, am I right in thinking that when I declare a
variable of type TMyType (and .Create it), only a reference to it gets put
on the stack and the object itself gets put in the heap?

Cheers,

Dave  :)

---------------------------------
David Preece,  dmpre...@tcp.co.uk
---------------------------------

Re:Delphi1: GPF everywhere!


Quote
In article <32d4f204.10413...@news.tcp.co.uk>, dmpre...@dreams.co.uk (David Preece) wrote:
>However, in one of the child window handlers I am trying to call a
>procedure in the parent form by casting the 'parent' pointer to TMDIParent
>(C++ habit, do I need to do this?) with:

>  procedure TMDIChild.Something1Click(Sender: TObject);
>  begin
>    TMDIParent(Parent).Procedure;
>  end;
[snip]
>...which calls the procedure fine, but GPF's if I try to use a member
>variable of TMDIParent within the procedure. The GPF doesn't happen if I
>call the procedure from a button on the TMDIParent form.

Not really answering your question (it is not obvious to my why your code
above is not working), but...

You may want to try sending your parent form a message rather than directly
calling one of its procedures.  Depending on what you are trying to do, it may
make the design cleaner.

Oh ... I believe the more accepted way of casting a delphi object would be to
say "(Parent as TMDIParent).Procedure" which should raise an exception
immediately if you are doing an unsafe type cast.

Brian

Re:Delphi1: GPF everywhere!


On Mon, 13 Jan 97 20:55:51 GMT, bcwri...@ucsd.edu (Brian Wright)
wrote:

Quote
>You may want to try sending your parent form a message rather than directly
>calling one of its procedures.  Depending on what you are trying to do, it may
>make the design cleaner.

Ok, I'm really a newbie here...  Can you give a quick example of
sending the parent a message.  I am working with an MDI child to
parent problem, and am pretty lost here.  Or if this is covered in the
docs, just wack me in the head.

Chuck Gadd
Director of Software Development, {*word*104} FX Communications.
e-mail:-cg...@{*word*104}-fx.com  http://www.{*word*104}-fx.com
Remove the - in front of my email address to send me e-mail.
This prevents automailers from filling my mailbox!
*** I boycott businesses that send me unsolicited email adverti{*word*224}ts ***

Other Threads