-1

My program throws this exception: System.StackOverflowException: 'Exception_WasThrown'

Details:

System.StackOverflowException HResult=0x800703E9 Message=Exception_WasThrown

I'm assembling an object to print a report, when I bring 37999 data I have to group them and get the requested information, but in the middle of the way the unformed error above pops. Here is the code of the method where the error happens.

private List<RelFilmesSeries> RetornaListaFilmes(List<RelFilmesSeries> dados, List<RelFilmesSe`enter code here`ries> filmes, bool boolOnePaises)
    {
        {
            Utils Utils = new Utils();
            RelFilmesSeries filme = new RelFilmesSeries();
            GroupRightCountries GroupRightCountries = new GroupRightCountries();
            List<GroupRightCountries> ListGroupRightCountries = new List<GroupRightCountries>();
            List<string> Direitos = new List<string>();
            var result = "";
            foreach (var item in dados)
            {
                if (dados.Count != 0)
                {
                    filme = new RelFilmesSeries();
                    filme.titulo_original = item.titulo_ingles;
                    filme.direcao = item.direcao;
                    filme.producao = item.producao != null ? item.producao.Substring(0, Math.Min(item.producao.Length, 150)) + "..." : "";
                    filme.elenco = item.elenco != null ? item.elenco.Substring(0, Math.Min(item.elenco.Length, 150)) + "..." : "";
                    filme.generos_nomes = item.generos_nomes;
                    filme.pais_producao = item.pais_producao;
                    filme.sinopse_ingles = item.sinopse_ingles;
                    //filme.status = RemoveStringsIguais(dados[0].status.Split(','));
                    filme.caminho_poster_url = Utils.ConvertImageURLToBase64(dados[0].caminho_poster_url);
                    filme.titulo_original = item.titulo_original;


                    var dadosDireito = dados
                             .Where(x => string.IsNullOrEmpty(x.titulo_original)
                             || x.titulo_original == item.titulo_original).ToList();

                    if (boolOnePaises)
                    {
                        //Pega os direitos relacionados ao filme
                        for (int i = 0; i < dadosDireito.Count; i++)
                        {
                            GroupRightCountries = new GroupRightCountries();
                            if (dadosDireito[i].titulo_original == item.titulo_original && !Direitos.Contains(dadosDireito[i].direito))
                            {
                                GroupRightCountries.Direito = dadosDireito[i].direito;
                                Direitos.Add(dadosDireito[i].direito);
                            }

                            if (GroupRightCountries.Direito != null)
                            {
                                filme.GroupRightCountries.Add(GroupRightCountries);
                            }
                        }
                    }
                    else if (!boolOnePaises)
                    {
                        //Pega os direitos relacionados ao filme
                        for (int i = 0; i < dadosDireito.Count; i++)
                        {
                            GroupRightCountries = new GroupRightCountries();

                            if (filme.GroupRightCountries.Count > 0)
                            {
                                if (filme.GroupRightCountries.Where(x => x.Direito== dadosDireito[i].direito).ToList().Count == 0)
                                {
                                    if (filme.GroupRightCountries.Count == 0)
                                    {
                                        if (dadosDireito[i].titulo_original == item.titulo_original)
                                        {
                                            GroupRightCountries.Direito = dadosDireito[i].direito;

                                            //Pega os paises que são relacionados ao direito
                                            for (int j = 0; j < dadosDireito.Count; j++)
                                            {
                                                if (dadosDireito[j].direito == dadosDireito[i].direito)
                                                {
                                                    if (GroupRightCountries.Direito == null)
                                                    {
                                                        GroupRightCountries.Paises.Add(dadosDireito[j].Pais_regiao);
                                                    }
                                                    else
                                                    {
                                                        if (!GroupRightCountries.Paises.Contains(dadosDireito[j].Pais_regiao))
                                                        {
                                                            GroupRightCountries.Paises.Add(dadosDireito[j].Pais_regiao);
                                                        }

                                                    }
                                                }
                                            }
                                        }
                                        if (GroupRightCountries.Direito != null)
                                        {
                                            filme.GroupRightCountries.Add(GroupRightCountries);
                                        }
                                    }
                                    else if (filme.GroupRightCountries.Where(x => x.Direito == dadosDireito[i].direito).ToList().Count == 0)
                                    {
                                        if (dadosDireito[i].titulo_original == item.titulo_original)
                                        {
                                            GroupRightCountries.Direito = dadosDireito[i].direito;

                                            //Pega os paises que são relacionados ao direito
                                            for (int j = 0; j < dadosDireito.Count; j++)
                                            {
                                                if (dadosDireito[j].direito == dadosDireito[i].direito)
                                                {
                                                    if (GroupRightCountries.Direito == null)
                                                    {
                                                        GroupRightCountries.Paises.Add(dadosDireito[j].Pais_regiao);
                                                    }
                                                    else
                                                    {
                                                        if (!GroupRightCountries.Paises.Contains(dadosDireito[j].Pais_regiao))
                                                        {
                                                            GroupRightCountries.Paises.Add(dadosDireito[j].Pais_regiao);
                                                        }

                                                    }
                                                }
                                            }
                                        }

                                        if (GroupRightCountries.Direito != null)
                                        {
                                            filme.GroupRightCountries.Add(GroupRightCountries);
                                        }
                                    }

                                }


                            }
                            else
                            {
                                if (filme.GroupRightCountries.Count == 0)
                                {
                                    if (dadosDireito[i].titulo_original == item.titulo_original)
                                    {
                                        GroupRightCountries.Direito = dadosDireito[i].direito;

                                        //Pega os paises que são relacionados ao direito
                                        for (int j = 0; j < dadosDireito.Count; j++)
                                        {
                                            if (dadosDireito[j].direito == dadosDireito[i].direito)
                                            {
                                                if (GroupRightCountries.Direito == null)
                                                {
                                                    GroupRightCountries.Paises.Add(dadosDireito[j].Pais_regiao);
                                                }
                                                else
                                                {
                                                    if (!GroupRightCountries.Paises.Contains(dadosDireito[j].Pais_regiao))
                                                    {
                                                        GroupRightCountries.Paises.Add(dadosDireito[j].Pais_regiao);
                                                    }

                                                }
                                            }
                                        }
                                    }
                                    if (GroupRightCountries.Direito != null)
                                    {
                                        filme.GroupRightCountries.Add(GroupRightCountries);
                                    }
                                }
                                else if (filme.GroupRightCountries.Where(x => x.Direito == dadosDireito[i].direito).ToList().Count == 0)
                                {
                                    if (dadosDireito[i].direito == item.direito && dadosDireito[i].titulo_original == item.titulo_original)
                                    {
                                        GroupRightCountries.Direito = dadosDireito[i].direito;

                                        //Pega os paises que são relacionados ao direito
                                        for (int j = 0; j < dadosDireito.Count; j++)
                                        {
                                            if (dadosDireito[j].direito == dadosDireito[i].direito)
                                            {
                                                if (GroupRightCountries.Direito == null)
                                                {
                                                    GroupRightCountries.Paises.Add(dadosDireito[j].Pais_regiao);
                                                }
                                                else
                                                {
                                                    if (!GroupRightCountries.Paises.Contains(dadosDireito[j].Pais_regiao))
                                                    {
                                                        GroupRightCountries.Paises.Add(dadosDireito[j].Pais_regiao);
                                                    }

                                                }
                                            }
                                        }
                                    }

                                    if (GroupRightCountries.Direito != null)
                                    {
                                        filme.GroupRightCountries.Add(GroupRightCountries);
                                    }
                                }

                            }

                        }
                    }

                    //filme.direito = boolOnePaises == false
                    //    ? ConcatenaDireitos(dados, "", dados[0].titulo_original, dados[0].direito)
                    //    : ConcatenaDireitos(dados, "", dados[0].titulo_original);

                    Direitos = new List<string>();
                    filmes.Add(filme);
                    dados.RemoveAll(x => x.titulo_original == item.titulo_original);

                    if (dados.Count != 0)
                    {
                        return RetornaListaFilmes(dados, filmes, boolOnePaises);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            //Fase 2 concatena os dados de direito com os paises;
            List<RelFilmesSeries> ListResultFilmes = new List<RelFilmesSeries>();
            foreach (var item in filmes)
            {
                result = "";
                filme = new RelFilmesSeries();
                filme.titulo_original = item.titulo_ingles;
                filme.direcao = item.direcao;
                filme.producao = item.producao;
                filme.elenco = item.elenco;
                filme.generos_nomes = item.generos_nomes;
                filme.pais_producao = item.pais_producao;
                filme.sinopse_ingles = item.sinopse_ingles;
                //filme.status = RemoveStringsIguais(dados[0].status.Split(','));
                filme.caminho_poster_url = item.caminho_poster_url; //Utils.ConvertImageURLToBase64(dados[0].caminho_poster_url);
                filme.titulo_original = item.titulo_original;

                if (boolOnePaises)
                {
                    //Pega os direitos relacionados ao filme e concatena
                    for (int i = 0; i < item.GroupRightCountries.Count; i++)
                    {
                        if (result == "")
                        {
                            result = item.GroupRightCountries[i].Direito;
                        }
                        else
                        {
                            result += ", " + item.GroupRightCountries[i].Direito;
                        }
                    }
                    filme.direito = result;
                }
                else if (!boolOnePaises)
                {
                    //Pega os direitos e os paises relacionados ao filme e concatena
                    for (int i = 0; i < item.GroupRightCountries.Count; i++)
                    {
                        if (result == "")
                        {
                            result = item.GroupRightCountries[i].Direito + ": [ ";

                            for (int j = 0; j < item.GroupRightCountries[i].Paises.Count; j++)
                            {
                                if (j < item.GroupRightCountries[i].Paises.Count - 1)
                                {
                                    result += item.GroupRightCountries[i].Paises[j] + ", ";
                                }
                                else
                                {
                                    result += item.GroupRightCountries[i].Paises[j] + " ] ";
                                }
                            }
                        }
                        else
                        {
                            result += item.GroupRightCountries[i].Direito + ": [ ";

                            for (int j = 0; j < item.GroupRightCountries[i].Paises.Count; j++)
                            {
                                if (j < item.GroupRightCountries[i].Paises.Count - 1)
                                {
                                    result += item.GroupRightCountries[i].Paises[j] + ", ";
                                }
                                else
                                {
                                    result += item.GroupRightCountries[i].Paises[j] + " ] ";
                                }
                            }
                        }
                    }
                    filme.direito += result;
                }

                ListResultFilmes.Add(filme);
            }

            return ListResultFilmes;
        }
        catch (Exception e)
        {
            throw e;
        }
    }
eglease
  • 2,445
  • 11
  • 18
  • 28
  • Do you know which part of this method is throwing the exception? There's a lot of code here, so a smaller sample might help. – Chris Long Oct 04 '21 at 20:59
  • You cannot expect us to diagnose pages and pages of code, you need to trim this down to a [mre] – DavidG Oct 04 '21 at 20:59
  • 1
    But a StackOverflowException is probably happening because you are recursively calling `RetornaListaFilmes` – DavidG Oct 04 '21 at 21:01
  • Your `RetornaListaFilmes` calls itself. Start there. – LarsTech Oct 04 '21 at 21:01
  • The error is happening in this first key { of the method. I use recursion to be able to remove data that has already been extracted from the object – peterson benhame Oct 04 '21 at 21:17

2 Answers2

0

You have some recursion in your code:

if (dados.Count != 0)
{
    return RetornaListaFilmes(dados, filmes, boolOnePaises);
}

The StackOverflowException would suggest that this recursion is happening endlessly. Probably you are recursing without removing any items from the list, so each call recurses in the same way until the exception occurs. (EDIT rereading your question and code, it could simply be that you're trying to recurse 37999 times, once for each item in the list...)

It's not clear from your question what your intent is, but from the shape of your code it seems likely that you should be either using recursion or looping through your list, not both.

(There are a number of other issues with your code - for example, the check if (dados.Count != 0) is redundant, as you will only reach that line if there is an item in the list to enumerate.)

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • there are 37999 movie data, with movie names that repeat several times and each repeated name has a different right. and each right has its license. so I have to extract the name of the movie to get the right and get the license for the right. I was using more recursion, I removed and left only this one in the code above, then I removed linq queries. but the error still persists, I'll try to remove this recursion and see if the foreach doesn't overflow error. – peterson benhame Oct 04 '21 at 21:36
0

I managed to solve the problem, removed the recursion and added an if condition:

if (filmes.Where(x => x.titulo_original == item.titulo_original).ToList().Count == 0)
{
}
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • Recursion can be difficult to follow/debug. I'd recommend avoiding it when you know exactly how many iterations are needed and the operations being applied in each iteration do not affect the next iteration. – Andrew H Oct 04 '21 at 22:13