473,406 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Singleton lockouts and cleanup

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?
Jul 21 '05 #1
2 1245
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.

Jul 21 '05 #2
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.

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dave Benjamin | last post by:
Hey all, I was just reflecting upon the old code block debate, and the thought occurred to me that Python really does have a form of code blocks in its implementation of generators. It was a...
7
by: Ethan | last post by:
Hi, I have a class defined as a "Singleton" (Design Pattern). The codes are attached below. My questions are: 1. Does it has mem leak? If no, when did the destructor called? If yes, how can I...
10
by: ferdinand.stefanus | last post by:
Hi Could someone tell me what's the difference between these two singleton implementations: First implementation: class Singleton { public:
6
by: Tom | last post by:
Hi there. I wonder how a singleton could be (auto)disposed. For instance : class Foo : IDisposable { private static readonly _instance = new Foo(); private Foo()
7
by: Atul Malaviya | last post by:
>From a design/usability perspective. When will one use a singleton pattern and when a class with purely static members? What are the pros and cons? I have inherited a code base which is full...
2
by: Chris Murphy via DotNetMonster.com | last post by:
Hey guys, I've been hitting a brick wall with a problem I've come accross in developing an application. Background: The application uses one primary class that I'm trying to implement with the...
2
by: Marty McDonald | last post by:
public static object GetInstance(Type type, object args) { object o = _hashObjects; if (o == null) { lock (_hashObjects) { //Still null? Need to check again because someone may //have added...
7
by: Adrian | last post by:
Hi, I have a large unmanaged static C++ library which I've wrapped using a small C++/CLR DLL. This is called from a C# client application. The static library has a singleton, however it appears...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.