Callback Functions & Object References

I have callback functions that use methods that reference a TForm object(Form1.Memo1).  When Form1 is the mainform the program works but when Form1 becomes a subform (created at run-time) I get the EAccessViolation error when the method used in the callback tries to reference Form1.

How can I re-reference Form1 when Form1 is no longer the mainform?





sartwjAsked:
Who is Participating?
 
vladikaConnect With a Mentor Commented:
But why do you define Form1 as LOCAL VARIABLE???
You global variable Form1 still don't initialized. And your functions refer to your global Form1 variable, isn't it?
0
 
kretzschmarCommented:
? what are the callback functions ?
0
 
sartwjAuthor Commented:
The callback functions poll com1 to receive information from GPS units.

The callback functions usually parse doubles or integers to methods for display or further processing.
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
karooCommented:
listening...
0
 
edeyCommented:
a call back function is one that gets passed as a func parameter & is in turn "called back".  An example would be:

The EnumWindows function enumerates all top-level windows on the screen by passing the handle of each window, in turn, to an application-defined callback function. EnumWindows continues until the last top-level window is enumerated or the callback function returns FALSE.

BOOL EnumWindows(

    WNDENUMPROC  lpEnumFunc,      // address of callback function
    LPARAM  lParam       // application-defined value
   );

where lpEnumFunc is the address of a function you declare.

sartwi:
I could be wrong, but I would think you'd have problems accessing a "non-main" form from the callback function because it's called from outside your prog.'s process.

GL
Mike
0
 
sartwjAuthor Commented:
The callbacks I'm using refer to C++ routines (referenced cdecl from Delphi5) in a DLL.  The callbacks work extremely well when the object references within the methods used by the callbacks are on the mainform.  Somehow the objects become de-referenced when the form containing the methods become subforms.

I think I have to use a SafeRef call within the method pointed to in the callback to obtain the Form1 object reference.  

I know nothing about SafeRef or if there are easier ways to reference Form1 in the method that the callback uses.
0
 
ptmcompCommented:
Do you set the reference each time after you created an instance of your form (and delete it before destroying)?
0
 
sartwjAuthor Commented:
I make no explicit reference to Form1 when created or when closed beyond the following code:

unit Main
.....
procedure Tmainform.createForm1;
var
  Form1 : TForm;
begin
  Form1 := TForm.Create(self);
  Form1.show;


Form1 self freed and released on close.
procedure Form1.FormClose();
  Action := caFree;
  Release;
0
 
vladikaCommented:
As I understant you C++ function calls Delphi function which refer to Form1 variable.

When Form1 is main form the variable Form1 is initialized during program startup:
  Application.CreateForm(TForm1, Form1); // Form1 refer to instance of TForm1 class

I think when you create TForm1 instance by hand in run-time you do not initialize Form1 variable properly.

Please, write how you create Form1 as subform.
0
 
MadshiCommented:
Could you please post us the callback type declaration and the call how you install that callback calls? And the definition of the installation function? And the callback handler itself?

I think without that we can only guess what's wrong...

Regards, Madshi.
0
 
sburckCommented:
I'll wager a guess myself - try creating your form using Application.CreateForm() rather than via Tform.Create.  This makes the application the owner of the form.
0
 
ptmcompCommented:
I hate this global form variables... #¦o/
0
 
sartwjAuthor Commented:
vladika you are right!  Thank you!

I have been declaring forms as local variables out of habit and didn't give it a second thought.   By keeping Form1 global - problem solved!

I now create new forms this way:
(forms I want only 1 copy of)

unit Main;
....
procedure CreateNewFormClick();
begin
if not Assigned(NewForm) then
begin
NewForm := TNewForm.Create(Application);
NewForm.show;
 end;
end;
0
All Courses

From novice to tech pro — start learning today.