Solved

Delphi and access violation, check for real object implementation

Posted on 2010-11-10
9
927 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:ewangoya
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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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:ewangoya
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FMX enumerated colours 2 84
Mobile Keyboard covers the display of the TMemo 3 78
QRReport  TQrmemo vertical stretching 1 42
Firemonkey DbLookupComboBox equivalent ? 2 38
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

863 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

27 Experts available now in Live!

Get 1:1 Help Now