Discussion:
Problems with registry after long time?
(too old to reply)
Sonnich Jensen
2011-11-29 11:43:57 UTC
Permalink
Hi all

I once wrote about that, and someone mentioned it could be a thread
problem. Please explain.

Anyway, I have now found the exact line causing the problem:

Result := False;
EnterCriticalSection(HS_Registry_op);
reg := TRegistry.Create;
reg.LazyWrite := False;
try
reg.RootKey := HKEY_CURRENT_USER;
// open registry key and create it, if do not exist
if Reg.OpenKey(key + '\ProdData', True) then // *** <---- fails
here, returns false, key not created.
begin

This sub is not able to open and create the key after 2-3 days of
work. Yes it happens in a thread, there are ~11 of them but only this
one does it. Restarting the thread makes it work again.

Any ideas why this happens?

Sonnich
Jamie
2011-11-29 12:47:10 UTC
Permalink
Post by Sonnich Jensen
Hi all
I once wrote about that, and someone mentioned it could be a thread
problem. Please explain.
Result := False;
EnterCriticalSection(HS_Registry_op);
reg := TRegistry.Create;
reg.LazyWrite := False;
try
reg.RootKey := HKEY_CURRENT_USER;
// open registry key and create it, if do not exist
if Reg.OpenKey(key + '\ProdData', True) then // *** <---- fails
here, returns false, key not created.
begin
This sub is not able to open and create the key after 2-3 days of
work. Yes it happens in a thread, there are ~11 of them but only this
one does it. Restarting the thread makes it work again.
Any ideas why this happens?
Sonnich
Is it possible that it may still be open from the last run through? Or
maybe you are just not closing the Key when done and maybe re-opening
too many times?

I think your problem maybe in the rest of the code in how it is
handing the registry and thread control access.

Jamie
Sonnich Jensen
2011-11-30 10:16:17 UTC
Permalink
On Nov 29, 2:47 pm, Jamie
Post by Jamie
Post by Sonnich Jensen
Hi all
I once wrote about that, and someone mentioned it could be a thread
problem. Please explain.
  Result := False;
  EnterCriticalSection(HS_Registry_op);
  reg := TRegistry.Create;
  reg.LazyWrite := False;
  try
    reg.RootKey := HKEY_CURRENT_USER;
    // open registry key and create it, if do not exist
    if Reg.OpenKey(key + '\ProdData', True) then // *** <---- fails
here, returns false, key not created.
    begin
This sub is not able to open and create the key after 2-3 days of
work. Yes it happens in a thread, there are ~11 of them but only this
one does it. Restarting the thread makes it work again.
Any ideas why this happens?
Sonnich
Is it possible that it may still be open from the last run through? Or
maybe you are just not closing the Key when done and maybe re-opening
too many times?
   I think your problem maybe in the rest of the code in how it is
handing the registry and thread control access.
Jamie
The CloseKey is used. As for the rest, there might be something.

Sonnich
Jamie
2011-11-30 13:03:19 UTC
Permalink
On Nov 29, 2:47 pm, Jamie
Post by Jamie
Post by Sonnich Jensen
Hi all
I once wrote about that, and someone mentioned it could be a thread
problem. Please explain.
Result := False;
EnterCriticalSection(HS_Registry_op);
reg := TRegistry.Create;
reg.LazyWrite := False;
try
reg.RootKey := HKEY_CURRENT_USER;
// open registry key and create it, if do not exist
if Reg.OpenKey(key + '\ProdData', True) then // *** <---- fails
here, returns false, key not created.
begin
This sub is not able to open and create the key after 2-3 days of
work. Yes it happens in a thread, there are ~11 of them but only this
one does it. Restarting the thread makes it work again.
Any ideas why this happens?
Sonnich
Is it possible that it may still be open from the last run through? Or
maybe you are just not closing the Key when done and maybe re-opening
too many times?
I think your problem maybe in the rest of the code in how it is
handing the registry and thread control access.
Jamie
The CloseKey is used. As for the rest, there might be something.
Sonnich
also, check to make sure you are freeing the Reg object along with that,
and at the very end of the code pile.

I've found that many times people create objects in a function/Thread
or what ever and perform a simple exit in the middle of the code some
where. THe object has not been released and still eating up resources.

That would most likely account for the long delay before you see a
problem, depending on how often this code is executed.

Also, be careful on the use of Try, finally and Except. Make sure you
are actually accounting for errors inside the code and not simply
allowing the function to exit with out handling the rest of it, due to a
fault.

All faults that exit the function in that manner should prompt a error
message to a log or screen to let them know something serious is
happening, don't use it as a common programming function to simply exist
code if for say, a pointer just happens to be invalid or something.
otherwise, that is very bad programming.


Jamie
Skybuck Flying
2011-12-01 01:45:59 UTC
Permalink
Maybe the registry code uses vcl stuff, if so it would need to be called
from a synchronize method.

Perhaps other threads trying to open that key and cause some kind of
conflict.

Perhaps multiple threads reading from the same memory cell which are right
next to each other, could cause race condition.

I am not sure if manipulating the registry is "thread safe"... I would guess
so, but not sure ;)

Perhaps the thread has a memory leak, or maybe a stack overflow, who knows
;)

Bye,
Skybuck.

Loading...