Hello.

1. I have a piece of code that is killing me:

I want to create a MDI application.
I have a main form that automatically creates a new child form called 
'Project Manager'. It can't be close, only minimized. It is 
automatically destroyed by the application when it terminates.
I use this 'Project manager' to create other (one or more) child forms 
called Grids. I should be able to close those forms (caFree) at any time 
or I can leave them open and close the application to close them for me 
when it finish. The question is: why I can't pass *nil *as parameter 
when I create these forms?


procedure TFormManager.Start;
begin
  with TFormGrid.Create(*nil*) DO                             <<< if I 
pass _Application_ or _Self_, it works!
     begin
        ...some code here;
     end;
end;

procedure TFormGrid.FormClose(Sender: TObject; var Action: TCloseAction);
begin 
  Action:= caFree;
end;

I think until here everything is right. Right?
When I close the whole application and one or more of these Grid forms 
are open, it crashes in this procedure:


procedure TMainForm.FormResize(Sender: TObject);       { <- this is 
supposed to keep the child forms at the same width with the main form 
when the user resize the interface }
VAR i: Integer;
begin                                                           
 for I:= MDIChildCount-1 DownTo 0 DO
  begin
   if (MDIChildren[i] is TFrmGrid) then
   if (MDIChildren[i] as TFrmGrid).HandleAllocated
   then
     begin
      if  ((MDIChildren[i] as TFrmGrid).WindowState= wsNormal)       
<<<< *CRASH HERE*. Exception class C0000005 with message 'access 
violation at 0x0055bc6b: read of address 0x00000000'.
      then
        begin
         (MDIChildren[i] as TFrmGrid).Left:= 0;
         (MDIChildren[i] as TFrmGrid).Width:= MainForm.ClientWidth-4;
        end;
     end;
   end;
end; 


I suppose is something with those 'dangling' pointers, so my next 
question is:


2. Why Delphi does only a 'Free' on a object/form when it automatically 
destroy it instead a FreeAndNil?

The application works very well with Self or Application instead of Nil 
but the documentation says that Application is not recommended because 
it is slow and Self should be used OLNY when the  Create belongs to the 
main form (and in my case I don't create those Grids in the main form). 
So the only answer is NIL.
Why does not work?


Thanks.
__________________________________________________
Delphi-Talk mailing list -> Delphi-Talk@elists.org
http://www.elists.org/mailman/listinfo/delphi-talk

Reply via email to