0

I had an app that worked well in Debug. Then I switched to Release and built the solution. I got a prompt asking for things like "continue to debug" etc, and chose the one that deactivate "Just My Code" (note: I'm new in this).

I launched my .exe and my app gave me an exception in a place my code worked just fine these past 2 weeks.

The code in question:

private void DgDeliveryOrders_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
        foreach (string piece in e.AddedItems)
        {
            Database.Pieces.Add(piece);
        }
        foreach (string piece in e.RemovedItems)
        {
            Database.Pieces.Remove(piece);
        }
        FillDetailsDataGrid();
}

Database.Pieces is a model property in my library project:

public static class Database
{
    public static List<string> Pieces { get; set; }
}

I chose the static route because I used it in several places and it was easier (I don't know if it's the best practice, but it worked in Debug).

But then, each time I clicked on a item in my DataGrid, I got an exception: Object reference not set to an instance of an object. Plus something telling me things about optimized code. I switched back to Debug, it worked, no more errors. I switched again to Release, the same error is back. Breakpoints were acting funny (sometimes it stopped, sometimes not), and although I had the exception message, my ex was null.

After following some topics here, I checked that my solution was in Release for my 2 projets. I deactivated "Optimize" in the projets properties too. What did it change? Breakpoints not working at all.

So I checked again "Optimize", reactivated Just my Code and... my code isn't working in Debug anymore. Nor do the breakpoints.

I tried to reboot Visual Studio, but nothing changed.

At this point, I don't know what to do, it's the first time I do a release, what did I do wrong?

Thank you for reading.

UPDATE

The code problem was resolved in the comments, as for the VS problem, it solved itself after several reboot of the software/computer.

Shilok
  • 81
  • 1
  • 10
  • what code sets `Pieces` to a non-null list? I'd guess the problem is *there* (in particular: it not happening - `Database.Pieces.Add(...)` and `Database.Pieces.Remove(...)` will always fail if `Database.Pieces` is `null`); side note: static lists like this are often bad news – Marc Gravell Jul 30 '19 at 14:42
  • @MarcGravell In the same file this method is, the list is initialized in Window_Loaded with "Database.Pieces = new List();" But this code worked fine in Debug, it's the first time I get an error on it. – Shilok Jul 30 '19 at 14:48
  • 1
    k; there are some really subtle timing things that can happen between debug and release in some cases - maybe just do that init *at a different time*? heck: `public static List Pieces { get; } = new List();` (if you're using a recent compiler) – Marc Gravell Jul 30 '19 at 14:59
  • That actually works just fine! Thank you! For the other problem, I think I found my answer here: https://stackoverflow.com/questions/34028341/vs-2015-update-1-claiming-i-am-debugging-a-release-build – Shilok Jul 30 '19 at 15:07
  • Just change from release to debug mode – Catarina Ferreira Jul 30 '19 at 15:10
  • @CatarinaFerreira I already am in debug mode, that's the problem xD – Shilok Jul 30 '19 at 15:20
  • Have you tried Clean the solution and recompile again? – Catarina Ferreira Jul 30 '19 at 15:22
  • @CatarinaFerreira Yup, it didn't change anything. – Shilok Jul 30 '19 at 15:26
  • 1
    @MarcGravell I checked my git to see if I changed something I forgot about. And yes, the code problem was totally my fault: my list initialization was in my constructor and I moved it to Window_Loaded before the release. Never thought it could do an error! – Shilok Jul 30 '19 at 15:29

0 Answers0