本文介绍了如何避免堆栈溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编译我用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 } }
更多推荐
如何避免堆栈溢出?
发布评论