views:

173

answers:

2

Im not sure if i have explaned this the best i can but, here we go...

I have 2 Custom components on a form, Which are link together at design time through the IDE. Whenever i call a procedure from on of the Component i get the Access violation,

Access violation at address 0049A614 in module 'Project2.exe'. Read of address 00000034.

This is a small section of my code

TMyClient = class(TClientSocket)
{...}
end;

and...

TPresence = class(TComponent) 
 private
  ftheClient: TMyClient 
 public 
  procedure SetStatus(status: string);

 published
  property UserName : string read fUserName write fUserName;
  property theClient: TMyClient read ftheClient write ftheClient;
 end;   



procedure TPresence.SetStatus(status: string);
begin
 try
*****   if theClient = nil then
     Exception.Create('theClient  is Nil');
 except
   on e:Exception do
   MessageDlg(e.classname+', '+e.message, mtWarning, [mbOK], 0);
 end;
  {...}
end;

0049A614 is at the *, and the IDE stops here.

I Have also tried to do the assign at run time with

Presence1.theClient := MyClient1;

with no luck

using procedures from Presence1 or MyClient1 that do not rely on each other work fine.

Delphi 7

Follow Up: from mghie comments, i rethought about it.

I removed the TPresence Component from the form (which caused some strange IDE errors, that might have had something to do with it) and created it design time, assigning everything that was needed. Now it works, but putting the TPresence Component back on the from brings the error back.

Thankyou for your help guys, i should be able to work this one out now, if i can't ill reopen another question :)

+8  A: 

You seem to be thinking that the exception is raised because the client field of Presence1 is not set - if you do however get the exception "Read of address 00000034" it means that the Self pointer in the SetStatus() call is nil. That would indicate that you call SetStatus() on an unassigned TPresence reference. It is not really possible to tell the reason for that from the snippet you posted, but it should get you started debugging.

I would still advise you to write a proper setter method for all component references in your own custom components - first because you have a better hook when debugging such problems (you can set a breakpoint there), and second because you should always call TComponent.FreeNotification() on such linked components to be able to track their destruction and set the internal reference to nil.

mghie
A: 

We probably need more of your code. It is possible you are not correctly creating an instance of TPresence which would give you the error you are experiencing. Try to give us a simple as possible code snippet that causes your error.

Toby Allen