Singleton lockouts and cleanup


M

Marty McDonald

public static object GetInstance(Type type, object[] args)
{
object o = _hashObjects[type];
if (o == null)
{
lock (_hashObjects)
{
//Still null? Need to check again because someone may
//have added it right before the lock.
o = _hashObjects[type];
if (o == null) //Now we know to create an instance.
{
try
{
//create object
o = Activator.CreateInstance(type,args);
//store it in the hashtable
_hashObjects[type] = o;
}
catch (Exception e)
{
throw new Exception("Factory could not create singleton object for type
" +

type.ToString() + ". " + e.Message);
}
}//end if
}//end lock
}//end if
return o;
}//end GetInstance method.

When this is executed with a SqlConnection as the type to instantiate, we
have a lockout (program stops responding). We know that the "lock"
statement is doing this. We're not locking the TYPE SqlConnection - we're
only locking a reference to the hashtable.

* Why does locking the hashtable reference cause a lockout?
* When we get this to work properly, when does an instance die?
 
Ad

Advertisements

T

Tian Min Huang

Hello,

Thanks for your post. As I understand, the problem you are facing is that
your application deadlocks at the code "lock (_hashObjects)". Please
correct me if there is any misunderstanding. I think more information is
needed before moving forward:

1. Is your application a multi-threaded program?

2. When it lockout, please "Break All" to check how many threads are there?
You can check the thread by selecting the menu "Debug" -> "Windows" ->
"Threads".

3. Please check the call stack when it lockout, and tell me the content of
call stack and the status of all the threads.

I look forward to your response.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Ad

Advertisements

T

Tian Min Huang

Hi,

I haven't heard back from you yet and I'm just writing in to see if you
have had an opportunity to collect the information. If you could get back
to me at your earliest convenience, we will be able to go ahead. If there
was some part of my post that you didn't understand, please feel free to
post here. I look forward to hearing from you.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top