StackOverflowException with attribute

G

Guest

Hi,
I have a project based on .Net 1.1 and VS 2003,now I am trying to upgrade it
to .Net 2.0 and VS 2005.The project passes the 'Build Solution',but When I
start Debug, it suddenly comes StackOverflowException and Debug stops.

The main Exception point is the function below:

public System.ComponentModel.PropertyDescriptorCollection
GetStatefulPropertyDescriptors()
{
DataObject.StatefulPropertyAttribute a = new
StatefulPropertyAttribute();
return this.GetProperties(new Attribute[] { a }); //exception point
//class System.Attribute that is base class for customer attributes
}

[AttributeUsage(AttributeTargets.Property, AllowMultiple=false,
Inherited=true)]
public class StatefulPropertyAttribute : Attribute{}

Under .Net 1.1 and VS 2003 there is no problem, no exception for the
function, but why does it come StackOverflowException under .Net 2.0 and How
to solve it?
 
M

Marc Gravell

Well, how is this.GetProperties(...) implemented? Is this an
ICustomTypeDescriptor implementation? If so, what are you calling? It
should normally be something like
TypeDescriptor.GetProperties(GetType()) or (GetType(), attributes)...

Marc
 
N

Nicholas Paldino [.NET/C# MVP]

windsim,

Well, more likely than not you have a recursive call somewhere (or
something that results in an endless loop of calls). Have you set a
breakpoint and looked a the call stack to see where the calls are repeating
in a loop?
 
G

Guest

this.GetProperties(...) is a member function of class
PropertyDescriptorCollection:
public PropertyDescriptorCollection GetProperties(Attribute[] attributes){...}

where

class PropertyDescriptorCollection : IList, IDictionary, ICollection,
IEnumerable
 
A

atlaste

this.GetProperties(...) is a member function of class
PropertyDescriptorCollection:
public PropertyDescriptorCollection GetProperties(Attribute[] attributes){...}

where

class PropertyDescriptorCollection : IList, IDictionary, ICollection,
IEnumerable
--
windsim

Marc Gravell said:
Well, how is this.GetProperties(...) implemented? Is this an
ICustomTypeDescriptor implementation? If so, what are you calling? It
should normally be something like
TypeDescriptor.GetProperties(GetType()) or (GetType(), attributes)...

The overflow exception is caused by the recursion depth that exceeds a
threshold value. If I understand this code correctly you should try
increasing this max recursion depth to solve the problem.

Cheers,
Stefan.
 
G

Guest

Thanks guys!

public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection globalizedProps;
PropertyDescriptorCollection baseProps =
TypeDescriptor.GetProperties(this, attributes, true);
....
}
I can not set break point at above code line and I think the problem is
maybe TypeDescriptor.GetProperties(...)

The info from call stack looks like a endless loop below :
....
[External Code]

SystemFrameworks.dll!SystemFrameworks.DataObject.GlobalizedObject.GetProperties(System.Attribute[] attributes = {Dimensions:[1]}) Line 350 + 0xd bytes C#

SystemFrameworks.dll!SystemFrameworks.DataObject.ModuleProperties.GetStatefulPropertyDescriptors() Line 92 + 0x26 bytes C#

SystemFrameworks.dll!SystemFrameworks.DataObject.ModuleProperties.GetModulePropertyListCurrent(SystemFrameworks.DataObject.ModuleProperties
mp = {SystemFrameworks.DataObject.DTMProperties}) Line 122 + 0x8 bytes C#

SystemFrameworks.dll!SystemFrameworks.DataObject.ModuleProperties.ToString()
Line 350 + 0xb bytes C#

SystemFrameworks.dll!SystemFrameworks.DataObject.ModuleProperties.GetHashCode() Line 313 + 0x7 bytes C#
[External Code]

by the way how to increase this max recursion depth in VS 2005?
--
windsim


Nicholas Paldino said:
windsim,

Well, more likely than not you have a recursive call somewhere (or
something that results in an endless loop of calls). Have you set a
breakpoint and looked a the call stack to see where the calls are repeating
in a loop?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

windsim said:
Hi,
I have a project based on .Net 1.1 and VS 2003,now I am trying to upgrade
it
to .Net 2.0 and VS 2005.The project passes the 'Build Solution',but When I
start Debug, it suddenly comes StackOverflowException and Debug stops.

The main Exception point is the function below:

public System.ComponentModel.PropertyDescriptorCollection
GetStatefulPropertyDescriptors()
{
DataObject.StatefulPropertyAttribute a = new
StatefulPropertyAttribute();
return this.GetProperties(new Attribute[] { a }); //exception point
//class System.Attribute that is base class for customer attributes
}

[AttributeUsage(AttributeTargets.Property, AllowMultiple=false,
Inherited=true)]
public class StatefulPropertyAttribute : Attribute{}

Under .Net 1.1 and VS 2003 there is no problem, no exception for the
function, but why does it come StackOverflowException under .Net 2.0 and
How
to solve it?
 
N

Nicholas Paldino [.NET/C# MVP]

I don't know what atlaste was trying to indicate by saying to raise the
maximum recursion depth, because in the end, if you have a loop that loops
infinitely, it's always going to be larger than whatever the threshold is.

To me, it would seem that the call to GetProperties on the
TypeDescriptor class is calling back into your GetProperities method, and
it's always going to instantiate another call.

You need to find some way to break this loop. What is it that you are
trying to do?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

windsim said:
Thanks guys!

public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection globalizedProps;
PropertyDescriptorCollection baseProps =
TypeDescriptor.GetProperties(this, attributes, true);
...
}
I can not set break point at above code line and I think the problem is
maybe TypeDescriptor.GetProperties(...)

The info from call stack looks like a endless loop below :
...
[External Code]

SystemFrameworks.dll!SystemFrameworks.DataObject.GlobalizedObject.GetProperties(System.Attribute[]
attributes = {Dimensions:[1]}) Line 350 + 0xd bytes C#

SystemFrameworks.dll!SystemFrameworks.DataObject.ModuleProperties.GetStatefulPropertyDescriptors()
Line 92 + 0x26 bytes C#

SystemFrameworks.dll!SystemFrameworks.DataObject.ModuleProperties.GetModulePropertyListCurrent(SystemFrameworks.DataObject.ModuleProperties
mp = {SystemFrameworks.DataObject.DTMProperties}) Line 122 + 0x8 bytes C#

SystemFrameworks.dll!SystemFrameworks.DataObject.ModuleProperties.ToString()
Line 350 + 0xb bytes C#

SystemFrameworks.dll!SystemFrameworks.DataObject.ModuleProperties.GetHashCode()
Line 313 + 0x7 bytes C#
[External Code]

by the way how to increase this max recursion depth in VS 2005?
--
windsim


Nicholas Paldino said:
windsim,

Well, more likely than not you have a recursive call somewhere (or
something that results in an endless loop of calls). Have you set a
breakpoint and looked a the call stack to see where the calls are
repeating
in a loop?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

windsim said:
Hi,
I have a project based on .Net 1.1 and VS 2003,now I am trying to
upgrade
it
to .Net 2.0 and VS 2005.The project passes the 'Build Solution',but
When I
start Debug, it suddenly comes StackOverflowException and Debug stops.

The main Exception point is the function below:

public System.ComponentModel.PropertyDescriptorCollection
GetStatefulPropertyDescriptors()
{
DataObject.StatefulPropertyAttribute a = new
StatefulPropertyAttribute();
return this.GetProperties(new Attribute[] { a }); //exception
point
//class System.Attribute that is base class for customer
attributes
}

[AttributeUsage(AttributeTargets.Property, AllowMultiple=false,
Inherited=true)]
public class StatefulPropertyAttribute : Attribute{}

Under .Net 1.1 and VS 2003 there is no problem, no exception for the
function, but why does it come StackOverflowException under .Net 2.0
and
How
to solve it?
 
G

Guest

One more thing: This is an implementation of the ICustomTypeDescriptor
interface, class name is GlobalizedObject.

Thia whole loop starts when some external code calls the GetHashCode() of my
object of type DTMProperties (inherits from ModuleProperties, inherits from
GlobalizedObject). The stack trace shows below shows that my
ModuleProperties.GetHashCode() method calls ModuleProperties.ToString().

The ToString method calls the GlobalizedObject.GetProperties(...) method in
order to return the name/value pairs of all properties that have a special
attribute.

The code that now executes does the following:

public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection globalizedProps;
PropertyDescriptorCollection baseProps =
TypeDescriptor.GetProperties(this, attributes, true);
....
}

The next thing happening according to the stack trace, is that we call into
some external code (probably the TypeDescriptor.GetProperties(...)), and then
we get back into the GetHashCode. Thus an infinite loop.

So the question is whether the .NET 2.0 implementation of
TypeDescriptor.GetProperties(...) calls the GetHashCode method for some
reason now? And was that not the case in 1.1?

Here is a more readable version of my stack trace:
..... (it just repeats itself from here)
[External Code]
SystemFrameworks.DataObject.GlobalizedObject.GetProperties(System.Attribute[] attributes = {Dimensions:[1]})
SystemFrameworks.DataObject.ModuleProperties.GetStatefulPropertyDescriptors()
SystemFrameworks.DataObject.ModuleProperties.GetModulePropertyListCurrent(SystemFrameworks.DataObject.ModuleProperties
mp = {SystemFrameworks.DataObject.DTMProperties})
SystemFrameworks.DataObject.ModuleProperties.ToString()
SystemFrameworks.DataObject.ModuleProperties.GetHashCode()
[External Code]

SystemFrameworks.DataObject.GlobalizedObject.GetProperties(System.Attribute[]
attributes = {Dimensions:[1]})
SystemFrameworks.DataObject.ModuleProperties.GetStatefulPropertyDescriptors()
SystemFrameworks.DataObject.ModuleProperties.GetModulePropertyListCurrent(SystemFrameworks.DataObject.ModuleProperties
mp = {SystemFrameworks.DataObject.DTMProperties})
SystemFrameworks.DataObject.ModuleProperties.ToString()
SystemFrameworks.DataObject.ModuleProperties.GetHashCode()
[External Code]

--
windsim


windsim said:
Thanks guys!

public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection globalizedProps;
PropertyDescriptorCollection baseProps =
TypeDescriptor.GetProperties(this, attributes, true);
...
}
I can not set break point at above code line and I think the problem is
maybe TypeDescriptor.GetProperties(...)

The info from call stack looks like a endless loop below :
...
[External Code]

SystemFrameworks.dll!SystemFrameworks.DataObject.GlobalizedObject.GetProperties(System.Attribute[] attributes = {Dimensions:[1]}) Line 350 + 0xd bytes C#

SystemFrameworks.dll!SystemFrameworks.DataObject.ModuleProperties.GetStatefulPropertyDescriptors() Line 92 + 0x26 bytes C#

SystemFrameworks.dll!SystemFrameworks.DataObject.ModuleProperties.GetModulePropertyListCurrent(SystemFrameworks.DataObject.ModuleProperties
mp = {SystemFrameworks.DataObject.DTMProperties}) Line 122 + 0x8 bytes C#

SystemFrameworks.dll!SystemFrameworks.DataObject.ModuleProperties.ToString()
Line 350 + 0xb bytes C#

SystemFrameworks.dll!SystemFrameworks.DataObject.ModuleProperties.GetHashCode() Line 313 + 0x7 bytes C#
[External Code]

by the way how to increase this max recursion depth in VS 2005?
--
windsim


Nicholas Paldino said:
windsim,

Well, more likely than not you have a recursive call somewhere (or
something that results in an endless loop of calls). Have you set a
breakpoint and looked a the call stack to see where the calls are repeating
in a loop?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

windsim said:
Hi,
I have a project based on .Net 1.1 and VS 2003,now I am trying to upgrade
it
to .Net 2.0 and VS 2005.The project passes the 'Build Solution',but When I
start Debug, it suddenly comes StackOverflowException and Debug stops.

The main Exception point is the function below:

public System.ComponentModel.PropertyDescriptorCollection
GetStatefulPropertyDescriptors()
{
DataObject.StatefulPropertyAttribute a = new
StatefulPropertyAttribute();
return this.GetProperties(new Attribute[] { a }); //exception point
//class System.Attribute that is base class for customer attributes
}

[AttributeUsage(AttributeTargets.Property, AllowMultiple=false,
Inherited=true)]
public class StatefulPropertyAttribute : Attribute{}

Under .Net 1.1 and VS 2003 there is no problem, no exception for the
function, but why does it come StackOverflowException under .Net 2.0 and
How
to solve it?
 
A

atlaste

I figured that the recursion wasn't infinite (but just deep). Since
semantics of methods (like GetProperties) change that could explain
the behaviour (it worked in 1.1 but not in 2.0). Changing the max
recursion depth not only helps to identify the problem in that
particular case but also helps to solve the special case problem. I
used to have these kind of problems all the time...

However, it seems like it's the GetHashCode that makes the thing
infinite so you were right after all.

I can think of a dozen reasons why they opted for using GetHashCode in
2.0 and not in 1.1. After all, the features of the language evolved. I
would make a new implementation of getHashcode and equals.

Cheers,
Stefan.


I don't know what atlaste was trying to indicate by saying to raise the
maximum recursion depth, because in the end, if you have a loop that loops
infinitely, it's always going to be larger than whatever the threshold is.

To me, it would seem that the call to GetProperties on the
TypeDescriptor class is calling back into your GetProperities method, and
it's always going to instantiate another call.

You need to find some way to break this loop. What is it that you are
trying to do?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Thanks guys!
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection globalizedProps;
PropertyDescriptorCollection baseProps =
TypeDescriptor.GetProperties(this, attributes, true);
...
}
I can not set break point at above code line and I think the problem is
maybe TypeDescriptor.GetProperties(...)
The info from call stack looks like a endless loop below :
...
[External Code]
SystemFrameworks.dll!SystemFrameworks.DataObject.GlobalizedObject.GetProperties(System.Attribute[]
attributes = {Dimensions:[1]}) Line 350 + 0xd bytes C#
SystemFrameworks.dll!SystemFrameworks.DataObject.ModuleProperties.GetStatefulPropertyDescriptors()
Line 92 + 0x26 bytes C#
SystemFrameworks.dll!SystemFrameworks.DataObject.ModuleProperties.GetModulePropertyListCurrent(SystemFrameworks.DataObject.ModuleProperties
mp = {SystemFrameworks.DataObject.DTMProperties}) Line 122 + 0x8 bytes C#
SystemFrameworks.dll!SystemFrameworks.DataObject.ModuleProperties.ToString()
Line 350 + 0xb bytes C#
SystemFrameworks.dll!SystemFrameworks.DataObject.ModuleProperties.GetHashCode()
Line 313 + 0x7 bytes C#
[External Code]
by the way how to increase this max recursion depth in VS 2005?
windsim,
Well, more likely than not you have a recursive call somewhere (or
something that results in an endless loop of calls). Have you set a
breakpoint and looked a the call stack to see where the calls are
repeating
in a loop?
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Hi,
I have a project based on .Net 1.1 and VS 2003,now I am trying to
upgrade
it
to .Net 2.0 and VS 2005.The project passes the 'Build Solution',but
When I
start Debug, it suddenly comes StackOverflowException and Debug stops.
The main Exception point is the function below:
public System.ComponentModel.PropertyDescriptorCollection
GetStatefulPropertyDescriptors()
{
DataObject.StatefulPropertyAttribute a = new
StatefulPropertyAttribute();
return this.GetProperties(new Attribute[] { a }); //exception
point
//class System.Attribute that is base class for customer
attributes
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple=false,
Inherited=true)]
public class StatefulPropertyAttribute : Attribute{}
Under .Net 1.1 and VS 2003 there is no problem, no exception for the
function, but why does it come StackOverflowException under .Net 2.0
and
How
to solve it?
 
M

Marc Gravell

MG> Well, how is this.GetProperties(...) implemented? Is this an
MG> ICustomTypeDescriptor implementation?
---
w> this.GetProperties(...) is a member function of class
w> PropertyDescriptorCollection:
w> public PropertyDescriptorCollection GetProperties(Attribute[]
attributes){...}
---
w> This is an implementation of the ICustomTypeDescriptor
w> interface, class name is GlobalizedObject.
---
Why didn't you say that the first time? Anyways... in the general
case, I would *definitely* be treating this as a huge warning sign;
you shouldn't be coming anywhere *near* blowing the stack just to read
metadata. For reference, I still think there is a loop in here: your
ICustomTypeDescriptor implementation uses the *instance* form of
TypeDescriptor.GetProperties... and the first thing that does is ask
"does this instance implement ICustomTypeDescriptor? If so, call that
instead..." - which is why I posted about using the *Type* form of
TypeDescriptor.GetProperties.

Actually, to avoid this hell, I quite like tke 2.0 approach of
TypeDescriptionProvider; this allows you to delegate metadata
provision to a separate class (helping keep each class targetted to
doing one job) - but more importantly it makes the distinction between
the *instance* and *Type* forms disappear; your CustomTypeDescriptor
subclass can of course choose whether to care about instances or not.

Marc
 

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