Weird error: [ArgumentOutOfRangeException: 'count' must be non-negative

I have a site that runs on ASP.NET 3.5, NHibernate 2.2, and Sprint.NET for Injection Dependency. A rather strange error occurs on our test server, as well as almost every time there are many users on the network. After a problem occurs, this error is displayed for each user and each request that they make, until you complete IISRESET. Then everything is all right.

Here's the exception:

'count' must be non-negative. Parameter name: count Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentOutOfRangeException: 'count' must be non-negative. Parameter name: count Source Error: [No relevant source lines] Source File: c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\4bf9aa39\6dcf5fc6\App_Web_z9ifuy6t.6.cs Line: 0 Stack Trace: [ArgumentOutOfRangeException: 'count' must be non-negative. Parameter name: count] System.String.CtorCharCount(Char c, Int32 count) +10082288 Spring.Objects.Factory.Support.AbstractObjectFactory.GetObjectInternal(String name, Type requiredType, Object[] arguments, Boolean suppressConfigure) +3612 Spring.Objects.Factory.Support.AbstractObjectFactory.GetObject(String name) +75 Spring.Objects.Factory.Support.DefaultListableObjectFactory.GetObjectsOfType(Type type, Boolean includePrototypes, Boolean includeFactoryObjects) +365 Spring.Context.Support.AbstractApplicationContext.GetObjectsOfType(Type type, Boolean includePrototypes, Boolean includeFactoryObjects) +136 Spring.Context.Support.AbstractApplicationContext.GetObjectsOfType(Type type) +66 [ActivationException: Activation error occured while trying to get instance of type InfoTextService, key ""] Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:57 Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance() in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:90 OurProjectsNamespace.Infrastructure.ObjectLocator.LocateService() +86 
+4
source share
3 answers

This is indeed a very strange mistake. When you look at the source of AbstractObjectFactory.GetObjectInternal , you will see the following structure:

 [ThreadStatic] private int nestingCount; protected object GetObjectInternal(...) { const int INDENT = 3; bool hasErrors = false; try { nestingCount++; if (log.IsDebugEnabled) { log.Debug("msg" + new String(' ', nestingCount * INDENT)); } // More code: Calls self recursively. } catch { nestingCount--; hasErrors = true; if (log.IsErrorEnabled) { log.Error("msg" + new String(' ', nestingCount * INDENT)); } } finally { if (!hasErrors) { nestingCount--; if (log.IsDebugEnabled) { log.Debug("msg" + new String(' ', nestingCount * INDENT)); } } } } 

The exception you see must be thrown by one of three calls to new String(' ', nestingCount * INDENT) . This particular call to the string constructor string when the given value is negative. Since INDENT is a constant, nestingCount must have a negative value in this case. nestingCount is a stream-static variable. By default, static variables are always initialized to the default value (0 in this case) and cannot be influenced by other threads. Moreover, nestingCount never used outside this method.

Since nestingCount is thread-static and is used only in this method, it is difficult to imagine that the nestingCount script can become negative. Perhaps in the case of an asynchronous exception (ThreadAbort), but even this is difficult for me to imagine. Another option is that the static variable of the stream is changed by someone else using reflection.

But the big question is: how to solve this?

Decision:

There is only one thing that I can think of, and which reconfigures log4net so that debugging information is not logged. If debugging information is rejected, the string(char, int) constructor will probably never be called again, which will hide the problem. Not very beautiful, but possibly effective. This may work because AbstractObjectFactory logs use the log variable, which is initialized as follows:

 this.log = LogManager.GetLogger(this.GetType()); 

You can do this by globally disabling logging of debugging information in log4net, or when you find it too complicated, setting log4net to turn off debugging information for type Spring.Objects.Factory.Support.DefaultListableObjectFactory (an instance that actually raises an exception) .

Good luck.

+3
source

I saw that this error occurs when a database column maps to multiple properties. A typical case is that a foreign key column maps to a property and to a collection. The second or third pair of eyes in the configuration files helps identify them.

One twist on this is that it is common to all users. Do you have a persistent object that is stored in application state?

0
source

In my case, this error occurred after a while during performance testing. It starts normally, and after a while this error appears.

Turns out this is caused by a completely unrelated [ThreadLocal] variable that I used in my code. I replaced it with a method parameter, and now it works fine.

0
source

All Articles