堆栈溢出异常
mscorlib.dll 中发生了类型为“System.StackOverflowException”的未处理异常
在 page_load 事件中,我正在调用
if (mySession.Current._isCustomer)
{
Response.Redirect("Products.aspx");
}
mySession 类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ShoppingCartWebApp
{
public class mySession
{
// private constructor
private mySession() {}
// Gets the current session.
public static mySession Current
{
get
{
mySession session =
(mySession)HttpContext.Current.Session["__MySession__"];
if (session == null)
{
session = new mySession();
HttpContext.Current.Session["__MySession__"] = session;
}
return session;
}
}
// **** add your session properties here, e.g like this:
public string _Property1 { get; set; }
public DateTime _date { get; set; }
public String _loginId { get; set; }
public string _firstName { get; set; }
public string _userName { get; set; }
public string _role { get; set; }
public Boolean _isCustomer = false;
public Boolean _isAuth = false;
public Boolean _isGuest = true;
public ShoppingCart _cart = new ShoppingCart();
public ShoppingCart instance
{
get
{
return _cart;
}
set
{
_cart = value;
}
}
public void abandonSession()
{
// _date =
_loginId = null;
_firstName = null;
_cart = null;
_userName = null;
_role = null;
_isCustomer = false;
_isAuth = false;
}
}
}
它给出了 stackoverflow 异常。为什么?
购物车类:
public class ShoppingCart
{
#region ListCart
public List<CartItem> Items { get; private set; }
public static SqlConnection conn = new SqlConnection(connStr.connString);
#endregion
#region CartSession
public ShoppingCart cart;
public ShoppingCart()
{
if (mySession.Current._cart == null)
{
cart = new ShoppingCart();
cart.Items = new List<CartItem>();
if (mySession.Current._isCustomer)
cart.Items = ShoppingCart.loadCart(mySession.Current._loginId);
mySession.Current._cart = cart;
}
else
{
cart = mySession.Current._cart;
}
}
}
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
In page_load event i am calling
if (mySession.Current._isCustomer)
{
Response.Redirect("Products.aspx");
}
mySession class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ShoppingCartWebApp
{
public class mySession
{
// private constructor
private mySession() {}
// Gets the current session.
public static mySession Current
{
get
{
mySession session =
(mySession)HttpContext.Current.Session["__MySession__"];
if (session == null)
{
session = new mySession();
HttpContext.Current.Session["__MySession__"] = session;
}
return session;
}
}
// **** add your session properties here, e.g like this:
public string _Property1 { get; set; }
public DateTime _date { get; set; }
public String _loginId { get; set; }
public string _firstName { get; set; }
public string _userName { get; set; }
public string _role { get; set; }
public Boolean _isCustomer = false;
public Boolean _isAuth = false;
public Boolean _isGuest = true;
public ShoppingCart _cart = new ShoppingCart();
public ShoppingCart instance
{
get
{
return _cart;
}
set
{
_cart = value;
}
}
public void abandonSession()
{
// _date =
_loginId = null;
_firstName = null;
_cart = null;
_userName = null;
_role = null;
_isCustomer = false;
_isAuth = false;
}
}
}
it gives a stackoverflow exception. why?
ShoppingCart Class:
public class ShoppingCart
{
#region ListCart
public List<CartItem> Items { get; private set; }
public static SqlConnection conn = new SqlConnection(connStr.connString);
#endregion
#region CartSession
public ShoppingCart cart;
public ShoppingCart()
{
if (mySession.Current._cart == null)
{
cart = new ShoppingCart();
cart.Items = new List<CartItem>();
if (mySession.Current._isCustomer)
cart.Items = ShoppingCart.loadCart(mySession.Current._loginId);
mySession.Current._cart = cart;
}
else
{
cart = mySession.Current._cart;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这行代码会导致无限循环和堆栈溢出:
它由 mysession 类的每个实例初始化。及其使用其父类。
即使使用单例 mySession 也无法解决问题。
当此代码执行时:
它尝试初始化新的ShoppingCard。购物卡要求 mysession 的单例实例。这行代码尚未执行:
因此要创建会话的新实例并且...
这意味着堆栈溢出!
你可以这样纠正它:
看看我在代码中的评论。
This line of code causes infinite loop and stack overflow :
it is initialized by each instance of mysession class. and its using its parent class.
even using singleton mySession can not solve the problem.
when this code is executing :
it tries to initialize new ShoppingCard. shopping card asks for singleton instance of mysession. this line of code is not executed yet :
so goes to create a new instance of my session and ...
this means stack overflow !
you can correct it like this :
look at my comments in code.
问题的出现是由于
mySession
和ShoppingCart
之间的关系造成的。mySession
的成员变量定义如下:当调用
mySession
的构造函数时,会实例化ShoppingCart
的实例。当ShoppingCart
的构造函数执行时,它会调用mySession.Current
静态属性。因为ShoppingCart
的构造函数是从同一个属性中调用的(请记住,我们仍在原始静态调用中创建mySession
的实例),因此它继续在此递归直到引发 StackOverflowException 为止。要解决此问题,我建议您查看一下
ShoppingCart
类。首先,为什么它需要一个自身的实例作为成员变量?其次,如果ShoppingCart
需要知道mySession
的内容信息,那么你的封装不正确。我建议您将所需的信息传递到ShoppingCart
的构造函数中,以避免回调mySession.Current
。The problem comes because of the relationship between
mySession
andShoppingCart
.mySession
has a member variable defined like so:When the constructor of
mySession
is called, an instance ofShoppingCart
is instantiated. When the constructor ofShoppingCart
executes, it calls themySession.Current
static property. Because the constructor ofShoppingCart
was called from within this same property (remember, we are still creating an instance ofmySession
in the original static call), it continues to recurse in this way until aStackOverflowException
is raised.To fix this, I suggest you take a look at your
ShoppingCart
class. Firstly, why does it need an instance of itself as a member variable? Secondly, ifShoppingCart
needs to know information about the contents ofmySession
, your encapsulation is not correct. I suggest you pass the information needed into the constructor ofShoppingCart
to avoid making a call back tomySession.Current
.鉴于您更新的问题,我认为如果您正确遵循 .Net 命名准则(如我对您的问题的评论中所述),您应该能够轻松找出问题所在。我怀疑您的调用代码是相似的,并且不遵循指南会掩盖实际发生的情况与您认为正在发生的情况。
作为第一步,我建议进行此清理;它可能会清楚地表明您在哪里导致溢出。
Given your updated question, I think if you properly followed .Net naming guidelines (as outlined in my comment on your question), you should be able to easily figure out where the problem is. I suspect your calling code is similar, and not following the guidelines is obscuring what is actually happening from what you think is happening.
As a first step I would recommend doing this cleanup; it will likely make it clear where you're causing the overflow.