Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Delphi and access violation, check for real object implementation

Posted on 2010-11-10
9
Medium Priority
?
976 Views
Last Modified: 2012-05-10
Hi,
I've this problem:
in my application I've some data module with some fields and methods;
one field is an implementation of and object.

FmyField: TmyCustomClassDataModule;

This is released automatically by Delphi when the application shutdown but if I check this field with this:

if FmyField <> nil then
  // Do Somethink

this check return true and if I access to field I got obviously an access violation.

My question is:
How I can check if this field (or variable) is real implemented?
0
Comment
Question by:ISIGest
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 34100792
To check that you can use it in a Try-Except statement and see if it succeed to assign a value meaning that it is not freed.

Try
Just use FmyField by assigning any value, such as:
FmyField := another variable with same datatype
Except
   Showmessage('not real');
end;
0
 
LVL 14

Expert Comment

by:systan
ID: 34101637
try;

if assigned(FmyField)  then ShowMessage('field is active') else ShowMessage('field not active');
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34102793
How is this field destroyed when application shuts down when its part of a datamodule. Should'nt the datamodule be destroying it. How do you create it?

Use Assigned(FmyField) to check if valid
as Systan suggested
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ISIGest
ID: 34104010
This is destroyed by application becouse it's create with application owner.
Application.CreateForm(myDM, TmyDMClass);

When I shutdown the application this is destroyed automatically but if I check this field from other form or datamodule it is implemented but our child (components) are really destroyed.
0
 

Author Comment

by:ISIGest
ID: 34104024
This field when destroyed is not NIL.  This is the problem.
And if I try to access to it checking with "Assigned(myField or myVariable)" the assign function return TRUE.
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34105388
Please help me understand.
I really don't understand your question. You say it is destroyed when you shutdown your application. If the application is shutdown, when are you testing if it is NIL, isn't application already closed

Is the datamodule created automatically when the application starts or do you call
Application.CreateForm(myDM, TmyDMClass) somewhere in the code yourself in which case it depends on when you are calling, what happens when it is being created, is it actually created or there is an exception which is quietly handled
0
 
LVL 14

Expert Comment

by:systan
ID: 34108683
Ok;

During the other form terminated, try to issue this command;

FreeAndNil(myDM);
0
 
LVL 21

Accepted Solution

by:
developmentguru earned 2000 total points
ID: 34123306
You can track down HOW the object is becoming invalid by assigning the OnDestroy event of the data module (if that is what has been freed).  If it is a form that has been freed you can still use this technique.  Once the event has been created for the OnDestroy, place a breakpoint there.  When the program stops on that breakpoint then you can use the call stack to find out how it was called.  This will show you the cause, if the object is being destroyed prematurely.

Another source of this type of error is memory overwrite.  You can create a breakpoint that will break when the memory location of your variable has changed.  If there is a memory overwrite then this will show you where it is happening.  If it is a memory overwrite then the object you originally used likely still exists, but the variable will be pointing to a nonsense memory address.  Trying to dereference this pointer will cause an access violation.  To create this type of breakpoint use the menus View | Debug Windows | Breakpoints, then right click on the breakpoints window and right click and select Add Data Breakpoint.  Specify your variable.  If this breakpoint never happens then you do not have a memory overwrite.  To use this I would stop the program after the object has been created and assigned, and assign the breakpoint then.

Let me know if you need more.
0
 
LVL 14

Expert Comment

by:systan
ID: 34128033
Does this guy knows how to deal accept and assist?  Good.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question