Solved

Delphi and access violation, check for real object implementation

Posted on 2010-11-10
9
954 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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