A few days ago, in one of the projects I was working on, an Object reference not set to an instance of an object error was issued, and the line where the error was issued was line 61 in the logs. When I checked line 61, there was no possibility of error at all, only a prototype class was created on line 61. 

A friend of mine named "Mustafa Shoja", who really knows Sharp very well, told me, "I read in the compiler lesson that sometimes it is possible that the line it shows is not correct and shows the error line with a few lines of difference." At first I did not believe and said that I do not accept this at all to see with my own eyes.

 In the stack, I saw several people asking the same question and saying that the error line in the Stacktrace shows the wrong line. I went to think first: |

Then we ran the project together in Debug mode and it showed the error line correctly. We did not expect at all. Then I said run the project in Release mode to see if it gives the same result.

This time we ran the project and saw that the error line was displayed incorrectly. I, who was totally angry. Then we solved the problem and it was over. 

But when I got home I said I had to find the reason. I simulated the project example and asked the question in the stackoverflow. In Debug mode it showed the error line correctly but in Release mode the error line was wrong. My questions were as follows:

I have a User class:

public class User
{
    public string Name { get; set; }
    public string Family { get; set; }
    public string Type { get; set; }
}

And a DoSomething method that simulated a real project example:

public static void Main()
{
    try
    {
        List<User> users = new List<User>();
        users.Add(new User
        {
            Family = "Zamani",
            Name = "Farhad",
            Type = "ADT"
        });
        DoSomething(users);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.StackTrace);
    }
}
public static void DoSomething(List<User> users)
{
    var adult = users.Where(a =>  // line 34
                                a.Name.ToUpperInvariant() == "Farhad".ToUpperInvariant()
                             && a.Family.ToUpperInvariant() == "Zaman".ToUpperInvariant()
                             ).FirstOrDefault();

    (string name, string type) = GetPassengerInfo(GetType(adult.Type), "Farhad");//line 39

}
private static (string name, string type) GetPassengerInfo(string type, string name)
{
    return (name, type);
}
static string GetType(string type)
{
    string result = string.Empty;
    switch (type)
    {
        case "ADT":
            result = "Adult";
            break;
    }
    return result;
}

When I run the project in Debug mode, the error line is displayed correctly and line 39 is displayed. But when I go to Release project, the error line shows 34. 

This is because in Release mode and when we do the publishing, the optimizer starts working and deletes the unused code and there is a topic called Method inlining which is very important and makes a lot of changes in this mode and for this reason. In Release, it is possible that the line that issued the error is different from the source code of your code.

Related articles:

:)

Powered by Froala Editor

Comments

Users Comments
  • RJ Smith

    I've noticed this same issue and a lot of the time the error line number is perfectly offset by comments in the code but not every time. It's almost as if some comments are not being recognized as comment and other are. I haven't had a chance to try and solve this further, but using this logic has helped me numerous times find the actually line that is throwing the exception.

    Posted at Monday, February 14, 2022
  • TIMO

    The incorrect line numbers are because the dotnet optimizing JIT compiler does not generate accurate machine code to IL offset debugging data for call sites. See also: https://github.com/dotnet/runtime/issues/96473#issuecomment-1890383639

    Posted at Saturday, January 13, 2024