Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Access violation with Registry and ReadInteger.

CLCaseCLCase Posts: 11Member
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('SOFTWARETCCSettings', 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?

Comments

  • dve83dve83 Posts: 30Member
    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.
  • CLCaseCLCase Posts: 11Member
    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 SOFTWARETCCSettings' is created before the crash.

    Regards
  • dve83dve83 Posts: 30Member
    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.
  • CLCaseCLCase Posts: 11Member
    : 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??
  • dve83dve83 Posts: 30Member
    [code]procedure TfrmGraph.ReadRegistry;
    var
    Registry: TRegistry;
    begin
    try
    Registry := TRegistry.Create;
    Registry.RootKey := HKEY_CURRENT_USER;
    Registry.OpenKey('SOFTWARETCCSettings', True);
    try
    seZAxis.Value := Registry.ReadInteger('ZAxis');
    except
    On E:Exception do
    begin
    ShowMessage(e.Message);
    end;
    end;
    finally
    Registry.Free;
    end;[/code]

    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.)
Sign In or Register to comment.