0

Below I have a line of code that calls SearchAlbums(text). By removing this line I no longer get StackOverflowException so I believe this is the line that gives me the trouble.

    public ObservableCollection<AlbumView> Albums = new ObservableCollection<AlbumView>();

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        if (e.Parameter is string text)
        {
            // User Search
            MainPage.Instance.SetHeaderText(GetSearchHeader(text, MainPage.Instance.IsMinimal));
            History.Push(text);
            SearchArtists(text);
            SearchAlbums(text);
            SearchSongs(text);
            SearchPlaylists(text);
        }
        else
        {
            // Back to Search Page
            MainPage.Instance.SetHeaderText(GetSearchHeader(History.Pop(), MainPage.Instance.IsMinimal));
        }
    }

    public void SearchAlbums(string text)
    {
        Albums.Clear();
        foreach (var group in MusicLibraryPage.AllSongs.Where((m) => IsTargetAlbum(m, text)).GroupBy((m) => m.Album))
        {
            Music music = group.ElementAt(0);
            Albums.Add(new AlbumView(music.Album, music.Artist, group.OrderBy((m) => m.Name).ThenBy((m) => m.Artist)));
        }
    }

Therefore, I set a breakpoint in this function and I was actually able to run this function without an exception. The StackOverflowException occurred after OnNavigatedTo has been executed.

I think the constructor of AlbumView might have something to do with it:

    public AlbumView(string name, string artist, IEnumerable<Music> songs)
    {
        Name = name;
        Artist = artist;
        Songs = new ObservableCollection<Music>(songs);
        FindThumbnail();
    }
    public async void FindThumbnail()
    {
        foreach (var music in Songs)
            if ((Cover = await Helper.GetThumbnailAsync(music, false)) != null)
                break;
        if (Cover == null) Cover = Helper.DefaultAlbumCover;
    }

But I also use this constructor elsewhere in another page, and that page displayed perfectly.

The first piece of code posted above is in this page. And the exception occurs when there is a match album.

I don't know how to fix it. I can only guess it might be the issue of async operation. Thanks in advance!

Seaky Lone
  • 992
  • 1
  • 10
  • 29
  • 1
    `StackOverflowException` should contain a stacktrace which will explain the call tree. I suspect that `Albums.Add` somehow indirectly leads to invocation of `OnNavigatedTo` – fenixil Oct 09 '19 at 01:31
  • @fenixil I am also hoping that VS could show me the stacktrace but I wasn't able to find it. It just displays a new page that says `StackOverflowException`. Do you know where I can find it? `Albums` is just a `ObservableCollection`. – Seaky Lone Oct 09 '19 at 01:33
  • There are a lot of useful answers in stackoverflow, just a little of googling and you'll get what you need. Please try [this](https://stackoverflow.com/a/5003882/11843739) answer. Or just put a breakpoint, and step through your code. – fenixil Oct 09 '19 at 02:53
  • @fenixil I have tried breakpoint as explained in my question but it doesn't seem helpful. Thanks for the stacktrace. I will try it later. – Seaky Lone Oct 09 '19 at 03:10
  • @fenixil That answer doesn't work for me and the call stack is not showing any functions. `OnNavigatedTo` is only called once. – Seaky Lone Oct 09 '19 at 04:19
  • Just to verify, did you look at CallStack window in VS? Please make sure you read comments to the answer too as them might contain useful details. Learning things is way more important than just resolving 1 particular issue, so please make sure you know what to do when you'll get stackoverflow next time. – fenixil Oct 09 '19 at 14:30
  • @fenixil I did read comments. And CallStack doesn't show any functions. – Seaky Lone Oct 09 '19 at 15:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/200626/discussion-between-seaky-lone-and-fenixil). – Seaky Lone Oct 10 '19 at 02:15

1 Answers1

1

I tested your code. Your problem is not in code-behind, but on the control.

For instances that have an asynchronous, use Binding instead of x:Bind for the binding of the instance. Because Binding is a runtime binding, and x:Bind is a compile-time binding.

change your code to this:

SearchPage.xaml

...
<controls:Carousel.ItemTemplate>
    <DataTemplate x:DataType="data:AlbumView">
        <local:GridAlbumControl DataContext="{Binding}" />
    </DataTemplate>
</controls:Carousel.ItemTemplate>
...

Best regards.

Richard Zhang
  • 7,523
  • 1
  • 7
  • 13
  • Excuse me, I just found another `StackOverflowException`, could you please test it? It occurs when I enter the `SearchPage` and then close the app. My code has also been updated. – Seaky Lone Oct 09 '19 at 09:23
  • Sorry, I can't reproduce this question. If the StackOverflowException is displayed as before, please try to comment out some of the controls that may be reported in xaml and then locate the cause of the problem. – Richard Zhang Oct 09 '19 at 09:28
  • Maybe because you didn't get any search result or you are not using my latest code? In my test, I found out that as long as there is any search result for any of the Artists/Albums/Songs/Playlists, `StackOverflowException` would occur on exit when you are on the `SearchPage`. I tried commenting out every control of the 4 `StackPanel`s that holds those data but it still occurred. So it doesn't seem to be an XAML issue? – Seaky Lone Oct 09 '19 at 11:42
  • @SeakyLone please make sure you learnt how to troubleshoot `StackOverflowException` so that you don't need to post a question on each occurrence ;) – fenixil Oct 09 '19 at 14:31
  • @fenixil I don't think this is a c# related question just as this answer suggests. My call stack doesn't have anything c# related. I just checked that my debugging settings by writing a recursive function. It correctly gives me the callstack with a bunch of that function. But my problem is it doesn't report a c# problem. So I don't know what else I can look at. – Seaky Lone Oct 10 '19 at 01:30
  • You can look at other answers in that thread for example :) Dump contains everything. That is a good exercise if stackoverflow happens not in your code ;) Also you can uncheck [Enable Just My Code](https://learn.microsoft.com/en-us/visualstudio/debugger/just-my-code?view=vs-2019) and check `Enable .NET Framework sources stepping`. – fenixil Oct 10 '19 at 01:42