堆栈溢出异常

发布于 2024-10-31 11:25:02 字数 2390 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

友欢 2024-11-07 11:25:02

这行代码会导致无限循环和堆栈溢出:

if (mySession.Current._isCustomer)
                cart.Items = ShoppingCart.loadCart(mySession.Current._loginId);

它由 mysession 类的每个实例初始化。及其使用其父类。

即使使用单例 mySession 也无法解决问题。

当此代码执行时:

session = new mySession();

它尝试初始化新的ShoppingCard。购物卡要求 mysession 的单例实例。这行代码尚未执行:

HttpContext.Current.Session["__MySession__"] = session;

因此要创建会话的新实例并且...

这意味着堆栈溢出!

你可以这样纠正它:

public static mySession Current
{
  get
  {
    mySession session =
      (mySession)HttpContext.Current.Session["__MySession__"];
    if (session == null)
    {
      session = new mySession();
      HttpContext.Current.Session["__MySession__"] = session;
      session._cart = new ShoppingCart(); //initialize your shoppoing car after adding variable to session !
    }
    return session;
  }
}

public ShoppingCart _cart;// = new ShoppingCart(); remove initialization

看看我在代码中的评论。

This line of code causes infinite loop and stack overflow :

if (mySession.Current._isCustomer)
                cart.Items = ShoppingCart.loadCart(mySession.Current._loginId);

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 :

session = new mySession();

it tries to initialize new ShoppingCard. shopping card asks for singleton instance of mysession. this line of code is not executed yet :

HttpContext.Current.Session["__MySession__"] = session;

so goes to create a new instance of my session and ...

this means stack overflow !

you can correct it like this :

public static mySession Current
{
  get
  {
    mySession session =
      (mySession)HttpContext.Current.Session["__MySession__"];
    if (session == null)
    {
      session = new mySession();
      HttpContext.Current.Session["__MySession__"] = session;
      session._cart = new ShoppingCart(); //initialize your shoppoing car after adding variable to session !
    }
    return session;
  }
}

public ShoppingCart _cart;// = new ShoppingCart(); remove initialization

look at my comments in code.

她如夕阳 2024-11-07 11:25:02

问题的出现是由于 mySessionShoppingCart 之间的关系造成的。

mySession 的成员变量定义如下:

public ShoppingCart _cart = new ShoppingCart();

当调用 mySession 的构造函数时,会实例化 ShoppingCart 的实例。当 ShoppingCart 的构造函数执行时,它会调用 mySession.Current 静态属性。因为 ShoppingCart 的构造函数是从同一个属性中调用的(请记住,我们仍在原始静态调用中创建 mySession 的实例),因此它继续在此递归直到引发 StackOverflowException 为止。

要解决此问题,我建议您查看一下 ShoppingCart 类。首先,为什么它需要一个自身的实例作为成员变量?其次,如果ShoppingCart需要知道mySession的内容信息,那么你的封装不正确。我建议您将所需的信息传递到 ShoppingCart 的构造函数中,以避免回调 mySession.Current

The problem comes because of the relationship between mySession and ShoppingCart.

mySession has a member variable defined like so:

public ShoppingCart _cart = new ShoppingCart();

When the constructor of mySession is called, an instance of ShoppingCart is instantiated. When the constructor of ShoppingCart executes, it calls the mySession.Current static property. Because the constructor of ShoppingCart was called from within this same property (remember, we are still creating an instance of mySession in the original static call), it continues to recurse in this way until a StackOverflowException 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, if ShoppingCart needs to know information about the contents of mySession, your encapsulation is not correct. I suggest you pass the information needed into the constructor of ShoppingCart to avoid making a call back to mySession.Current.

剩一世无双 2024-11-07 11:25:02

鉴于您更新的问题,我认为如果您正确遵循 .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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文