Solved

Delphi and access violation, check for real object implementation

Posted on 2010-11-10
9
919 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
Comment Utility
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
Comment Utility
try;

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

Expert Comment

by:ewangoya
Comment Utility
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
 

Author Comment

by:ISIGest
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:ISIGest
Comment Utility
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:ewangoya
Comment Utility
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
Comment Utility
Ok;

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

FreeAndNil(myDM);
0
 
LVL 21

Accepted Solution

by:
developmentguru earned 500 total points
Comment Utility
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
Comment Utility
Does this guy knows how to deal accept and assist?  Good.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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 video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now