简体   繁体   中英

An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll during calling recursive function

// code execute infinite and giving me error as unhanded exception of type 'System.StackOverflowException////

//default declaration //

Hashtable ht = new Hashtable(); // for store pagerank during recursion i have used hash table
DBML.Dbml_UploadDataContext objdbcontext = new DBML.Dbml_UploadDataContext(TDBConvertion.Properties.Settings.Default.MY_DBConnectionString); // this is DataContext of my DBML  and all tables have primary key and foreign key in it.

//method for calling recursive function //

 public string calculatepagerank()
        {
            double damp = 0.85;
           double output=0;

                objdbcontext.Connection.Open();
                string stroutput = "";

               foreach (var item in objdbcontext.Engine_Directories) // for all ids i want to calculate pr
                    {

                        output = pagerank(item.ID, objdbcontext.Engine_Links_Inlinks.Where(w => w.LinkID == item.ID).ToList(), objdbcontext.Engine_linkcounts.Where(x => x.ID == item.ID).FirstOrDefault().OutLinks.Value,damp);
                        stroutput = stroutput + ",Iteration::" + i + ",ID=" + item.ID + ",pagerank=" + output.ToString() + "\n";
                    }
                    ht.Clear();
                objdbcontext.Connection.Close();
            return stroutput;
        }

/// recursive function //

        public double pagerank(Int64 intId,List<DBML.Engine_Links_Inlink> listInlinksID,Int64 intoutlinkcount,double dumpingfactor)
        {

            double count = 0;
            double o = 0;

            foreach(var item in listInlinksID)
            {
                Int64 outlinkcount=1;
                if( objdbcontext.Engine_linkcounts.Where(x => x.ID == item.InlinksID).FirstOrDefault()!=null)
                {
                    Int64 outc = objdbcontext.Engine_linkcounts.Where(x => x.ID == item.InlinksID).FirstOrDefault().OutLinks.Value;
                    outlinkcount=( outc !=0)?outc:1 ;
                }

                if (!ht.ContainsKey(item.InlinksID))
                {
                    o = pagerank(item.InlinksID.Value,objdbcontext.Engine_Links_Inlinks.Where(w => w.LinkID == item.InlinksID).ToList(), outlinkcount, dumpingfactor);

                    ht.Add(item.InlinksID.Value, o);
                    count = count + o;
                }
                else
                {
                    count = count + Convert.ToDouble(ht[item.InlinksID].ToString());
                }

            }          
            return dumpingfactor + dumpingfactor * (count/intoutlinkcount);
        }

Well .NET have not infinity-size stack. By default it's 1MB only. Execute this code in new thread with larger stack size(second argument for "Thread" constructor), or use loops instead of recursion.

Could you possibly refactor the following piece of code?

if (!ht.ContainsKey(item.InlinksID))
{
   o = pagerank(item.InlinksID.Value,objdbcontext.Engine_Links_Inlinks.Where(w => w.LinkID == item.InlinksID).ToList(), outlinkcount, dumpingfactor);

   ht.Add(item.InlinksID.Value, o);
   count = count + o;
}

Instead of recursively calling pagerank, why don't you do take the code and add use coroutines ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM