Delphi and Kylix

Moderators: pritaeas
Number of threads: 7244
Number of posts: 19051

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Access violation with Registry and ReadInteger. Posted by CLCase on 3 Jan 2009 at 11:36 AM
Hi All,

With the following code I read a value in the reg. database.

procedure TfrmGraph.ReadRegistry;
var
Registry: TRegistry;
begin
try
Registry := TRegistry.Create;
Registry.RootKey := HKEY_CURRENT_USER;
Registry.OpenKey('\SOFTWARE\TCC\Settings', True);
seZAxis.Value := Registry.ReadInteger('ZAxis');
finally
Registry.Free;
end;

If my program runs for the first time the registry of course doesn't contain the key. In this case the key should be created automatically,
but despite of this I get an access violation when the program is run the first time.

The error comes with this line:
seZAxis.Value := Registry.ReadInteger('ZAxis');

I think the reason is that the new created key doesn't contain any information, but how do you normally prevent the program to crash in this case? I thought I could prevent the crash with the try - except, but it makes no difference.

Any ideas?
Report
Re: Access violation with Registry and ReadInteger. Posted by dve83 on 4 Jan 2009 at 1:20 AM
might sound kindof odd, but have you tried assigned the value to a temporary variable.

eg.
ftemp := Registry.ReadInteger('ZAxis');

just to see if it's not the left hand side of the assignment statement that is failing.
Report
Re: Access violation with Registry and ReadInteger. Posted by CLCase on 4 Jan 2009 at 3:40 AM
Yep, but I get the samme error.

The program crashes with an error like "error accessing data for ZAxis".
I use a German version of Delphi so it doesn't make sence to post the compiler message here (as it is in German).

By the way:
the key \SOFTWARE\TCC\Settings' is created before the crash.

Regards
Report
Re: Access violation with Registry and ReadInteger. Posted by dve83 on 5 Jan 2009 at 1:04 AM
k then the only thing that could be wrong is the the object / class / record ZAxis is not initialized / created. OR one of the properties has not been initialized. If ZAxiz is a class / object, make sure that the property you are trying to change, does not refer to a un-initialized variable. Either ZAxiz or ZAxiz.Value might not exist.
Report
Re: Access violation with Registry and ReadInteger. Posted by CLCase on 5 Jan 2009 at 1:45 AM
: k then the only thing that could be wrong is the the object / class
: / record ZAxis is not initialized / created. OR one of the
: properties has not been initialized. If ZAxiz is a class / object,
: make sure that the property you are trying to change, does not refer
: to a un-initialized variable. Either ZAxiz or ZAxiz.Value might not
: exist.

That is possible, but why does my program crash when I use try - except??
Report
Re: Access violation with Registry and ReadInteger. Posted by dve83 on 5 Jan 2009 at 2:10 AM
procedure TfrmGraph.ReadRegistry;
var
  Registry: TRegistry;
begin
 try
   Registry := TRegistry.Create;
   Registry.RootKey := HKEY_CURRENT_USER;
   Registry.OpenKey('\SOFTWARE\TCC\Settings', True);
   try
     seZAxis.Value := Registry.ReadInteger('ZAxis');
   except
     On E:Exception do
     begin
       ShowMessage(e.Message);
     end;
   end;
 finally
   Registry.Free;
 end;


while running in the IDE, you will always see any exceptions that are raised (unless you turn the options off, which I don't recommend), but if you handle them within the try and except, the affects will not be as adverse as if when you hadn't handled them. if you run the EXE standalone (outside the IDE), they ought to not show. try the above code, to get a readable error message. you might want to test seZAxis and its property for validity. (eg. If not assigned(seFAxiz) then SHowMessage.)



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.