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?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

? what are the callback functions ?
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.
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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.

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.

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.
Do you set the reference each time after you created an instance of your form (and delete it before destroying)?
sartwjAuthor Commented:
I make no explicit reference to Form1 when created or when closed beyond the following code:

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

Form1 self freed and released on close.
procedure Form1.FormClose();
  Action := caFree;
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.
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?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
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.
I hate this global form variables... #¦o/
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();
if not Assigned(NewForm) then
NewForm := TNewForm.Create(Application);;
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.