如何避免堆栈溢出?

编程入门 行业动态 更新时间:2024-10-24 03:21:34
本文介绍了如何避免堆栈溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我编译我用CSHARP codeProvider code,并动态地创建一些类的结果集的实例。比我调用一些方法。如果方法有递归我得到StackOverflowException和我的应用程序将终止。

我如何避免这种情况?

使用系统; 使用System.Runtime.Remoting; 命名空间TestStackOverflow {     类节目     {         类StackOver:MarshalByRefObject的         {             公共无效的run()             {                 跑();             }         }         静态无效的主要(字串[] args)         {         AppDomain中域= AppDomain.CreateDomain(新);         对象句柄句柄= domain.CreateInstance(typeof运算(StackOver).Assembly.FullName的typeof(StackOver).FullName);         如果(处理!= NULL)         {             StackOver堆栈=(StackOver)handle.Unwrap();             stack.Run();         }     } } }

相关报道:   

什么是堆栈溢出?

解决方案

运行时调用运行。这就是无限递归。

类StackOver:MarshalByRefObject的     {         公共无效的run()         {             跑(); //无终止的递归调用         }     }

I compile my code using CSharpCodeProvider, and dynamically create instance of some class in result assembly. Than I call some method. If the method has recursion I get StackOverflowException and my app terminates.

How do I avoid this?

using System; using System.Runtime.Remoting; namespace TestStackOverflow { class Program { class StackOver : MarshalByRefObject { public void Run() { Run(); } } static void Main(string[] args) { AppDomain domain = AppDomain.CreateDomain("new"); ObjectHandle handle = domain.CreateInstance(typeof (StackOver).Assembly.FullName, typeof (StackOver).FullName); if (handle != null) { StackOver stack = (StackOver) handle.Unwrap(); stack.Run(); } } } }

Related:

What is a stack overflow?

解决方案

Run is calling Run. That is the infinite recursion.

class StackOver : MarshalByRefObject { public void Run() { Run(); // Recursive call with no termination } }

更多推荐

如何避免堆栈溢出?

本文发布于:2023-11-09 09:55:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572075.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:堆栈

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!