Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delphi and access violation, check for real object implementation

Posted on 2010-11-10
9
Medium Priority
?
972 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
Independent Software Vendors: 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

704 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