Link to home
Start Free TrialLog in
Avatar of Stuart_Johnson
Stuart_Johnson

asked on

Acces Violations

Hi Folks,

We've got a slight problem.  One of our applications has a StringGrid in an MDI form.  Depending on the cell selected, a popup menu is modified (items enabled or disabled).  If the form is closed without making the popup appear, it works fine.  However, if the popup is visible when you click the close button (the X a the top of the form), we get an A/V.

I've removed all the code which dynamically changes the popup (removed the event), but it still does it.

Does anyone have any ideas as to what could be causing this as it's causing us a lot of grief.

Thanks for any info,


Stu
Avatar of Stuart_Johnson
Stuart_Johnson

ASKER

Some more info.

If we put a button on the form and make it close the form (calling Close), we don't get an A/V.  This isn't an option though, it was just a test.

So, why would the button work and not the close (X) button at the top of the form?

Stu
Hi Stu,

It may be that the order of the popup closing and the sending of the close message to the form may be getting confused:

Here are some thoughts:

1. Don't free the form when it is closed (ie: don't set action := caFree). Instead call the Release method

2. Here's a kludgy idea to try as well, do this in the OnClose event:

var
  HaveClosed : Boolean;

...
Init HaveClosed to false somewhere;
...

procedure TMyForm.FormClose(Sender : TObject;
                            var action : ???);
begin
  if HaveClosed then
    action := cafree
  else
    begin
      HaveClosed := True;
      action := caNone;
      PostMessage(Handle, WM_CLOSE);
    end;
end;

Cheers,

Raymond.
Hi Ray,

Thanks for the info.  The app is an MDI App, so I have to use the Action to free the form.  Unless there is another way of freeing the form?

The code you gave worked, but I still get the A/V.

Any other clues at all?  There is now no FormDestroy (I got rid of that).  There is no OnCloseQUery either.  There is code in the FormClose, but it's only setting a few booleans which exist on the apps main form.  Even if I comment all this code out, I still get the same problem.

This is really driving me mad.  I need to get this working in 2 hours.  I have no idea why it's doing it at all.  Granted, the form in the app has been blown out of proportion (it was never ment to do half the things its doing now).

Thanks mate.

Stu
Where is the AV occuring?

Raymond.
I honestly don't know.  I can't find it.  It appears to be after the form is freed, but there is no code in the main application which detects that the form is no longer running.

If I put a break point in the FormClose event, it goes all the way through to the "end" and then I get the A/V.  There is no re-entry code after that, so I don't know what's going on.

Stu
Where is the AV occuring?

Raymond.
ASKER CERTIFIED SOLUTION
Avatar of rwilson032697
rwilson032697

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is that an accidental double post, Ray?

Stu
Hi Ray,

I'm just running it now.  Tracing from the FormClose forward.  I'll let you know what the problem is when I find it.

Cheers

Stu
Stu,

You should be able to let the critter run and get execution stopping in the source when the AV occurs...

Raymond.
OK.  I've found the source of the problem.

If you right-click on the grid, the menu appears.  If you click on the close button, when the menu closes, (get this) the MouseUp event is fired!  How does that work?  Strange..

I'm just trying to work out what's going on.  This is a project I've only just started working on, so I'm not familar with it.

Stu
That is strange! I have noticed in the past that the MouseDown, MouseClick, MouseUp order can occur in that order, when you might expect it to do Down, Up, Click!

Now you have found it I image killing it should be simple :-)

Cheers,

Raymond.
Nope.  Not that simple worst luck.  It's just not going to go away.  I've fixed it SORT OF.  It only does it every now and then.  Even if it does it once, it's once too often.

Do you know of any code I can use to make the menu close up again?  I'm really clutching at straws here, but I don't know what else to try.  I think we're on the right path with trapping the errors.  I've been given a time repreave, so I can spend more time trying to debug it.  I'll keep going down the path you suggested above.

Thanks so much for you help Ray.  I really appreciate it!

Stu
OK.  Here is an interesting thing.  This same code works PERFECTLY in our version 1.4 application which was writtin in Delphi 3 Ent.  From 1.5 onwards it's stuffed (we're now on version 1.9) and 1.5+ has been done in Delphi 5.

Could the be ANYTHING at all in D5 which could cause this sort of thing?  I don't think I'm using any of the updates - would it be worth downloading and installing them?

Stu
The popup menu should close all by itself when you click on the close button for the MDI form.

Take a look at the TPopupMenu.Popup method. It calls TrackPopupMenu, a Win32 API function that handles the display etc of the menu. You could put a breakpoint after this to see it come back when you click on the close button for the form.

Cheers,

Raymond.
There is quite a lot of change between D3 and D5 (why not d6??), though nothing comes to mind which would cause this.

It would certainly be a good idea to get the D5 updates installed (not that it guarantees a fix of course).

Another kludge you could look at is setting a flag in the close event that the mouse up event can look at...

Cheers,

Raymond.


how about checking the forms state on the popups OnPopup event ?
like :

procedure TForm1.myPopupPopUp(Sender: TObject);
begin
  if csDestroying in ComponentState then exit;
  // if you do any procesing here at all
end;

that saved me lots of AVs :)

how do you create the popup  ?
dinamically ? do you create any of it's items dinamically ?
also on the forms Close event try closing the menu
OK, after much rooting around (take that literally if you please), I fixed the problem.

In the FormCloseQuery, I set the grid's OnMouseUp event to nil and the problem went away.  It's now going perfectly.

Thank you so much for you help, Ray.  You solved a lot of headaches.

Stu
Hi Lee,

Thank-you too for your contribution.  The error didn't happen when the popup popped up, but when it was closing back down again.

Stu
Hi Lee,

Thank-you too for your contribution.  The error didn't happen when the popup popped up, but when it was closing back down again.

Stu
Hi Stu,

Thanks for the points! Hopefully the new project won't throw too many of these nasties at you!

Cheers,

Raymond.
No worries, Ray.  Thanks for the help.

Stu
No worries, Ray.  Thanks for the help.

Stu