TechTricks
Technical answers from the trenches 
 
 
 
 

     
   
TDataModule Instability Problems
 
   
 Posted: 28 March 2001
 
   
 
 Applies to Delphi 2 and later
 
   
 
Audience: Everyone
 
       
   

Question: I'm creating a data module dynamically. My system becomes unstable at the end of the process. It locks up, hangs, or otherwise gets weird. What's going on and how do I fix it?

Answer: See if you're doing something like this:

   with tDataModule1.create( self ) do
   try
      doSomething();
   finally
      release;
   end;

If so, your problem stems from the fact that tDataModule doesn't have a release method. This code calls the release method of the form that runs it.

The solution is to call your data module's free method instead. For example:

   with tDataModule1.create( self ) do
   try
      doSomething();
   finally
      free;
   end;

This illustrates two dangers:

  • Assuming the ancestry of an object (the classes it inherits from).

    TDataModule descends from tComponent, not tCustomForm. When in doubt, use the Hierarchy link of the help topic for the object you're working with.

  • Writing general, as opposed to specific code.

    WITH is a great construct for reducing the amount of code and typing you need, however, you must use it with care. When you force the compiler to make choices, verify that those choices are the ones you really want.

When in doubt, be specific. For example, the following code would have triggered a syntax error in the Delphi IDE:

   var
      dmoData : tDataModule1;
   begin
      dmoData := tDataModule1.create( self );
      try
         doSomething();
      finally
         dmoData.release;
      end;
   end;

While this might seem like more work, it would have prevented the problem in the first place. Consider it an investment in quality. :-)

For information on why you should use release for forms, please see the online Help. Keep in mind, however, that you free objects and release forms.

 

       

Top

Feedback About Paradox Delphi Assorted Web Stuff
 
 
Copyright © 2000-2004, techtricks.com; All Rights Reserved.
Acknowledgements, Disclaimers, Terms and Conditions.
Article last updated on 31 May 2003

 

Other Sites: Paradox, Delphi, Perl, Web Stuff, and More


 

[- End -]