Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

I'm new to this forum. I hope you can help me.

I'm using the Enterprise edition of VS 2005. When I debug my project an exception is raised - 'Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack'. I can't find anything about this on-line. I suspect this message is hiding the true exception. Does anyone know what this message means

Regards,

Andy Ham



Answer this question

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

  • neo1

    Jim,

    Check under the following:

    Debug->Attach to Process

    In the attach to process dialog hit the "Select..." button and check the boxes for both Native and managed code. Now you will attach in mixed mode for debugging.

    Thanks,

    Ian



  • Andrea Ramacciotti

    Dear Ian,

    I have similar problem which showing these unable.. message in the debugging in VS 2005 Team Suite (released).
    I had my attached debug module in both Native & Managed code mode, the module I attached to debug is in debug mode ( its the windows service app).
    Not having any error (yet!) but unable to look at values of any objects as it's showing this message.
    - Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

    Hope you can help me.

    Thank you
    Punprom Kasemsant


  • me here

    Func-eval (property evaluation) is evil.

    What the error means is that the thread is currently stopped in either (a) optimized code or (b) in native code (really, anywhere outside of code that was not directly generated from IL ).

    For a) switching to debug-mode will help. However, if you've stopped in a 3rd-party dll (or the FX), that code may still be optimized even if your project's code is debug. Hitting Break at when a System.Diagnostics.Debug.Assert fires is an example of this. The code that implements Debug.Assert is in a separate library (system.dll) which is optimized even if the rest of your solution is non-optimized.

    In this case, the intersting thing is to get a callstack of where the thread is. The real problem may be that the thread shouldn't even be stopped there in the first place.

    For b) func-eval can only be initiated in code that was jitted from IL. So you can't initiate a func-eval of a managed method if your thread is outside of managed code. There are further restrictions too (some listed here)

    More gory technical details are here:

    http://blogs.msdn.com/jmstall/archive/2005/11/15/funceval_rules.aspx



  • new2C#

    Ian,

    I am expressing the same issue. I can provide a sample code block that shows the issue. Before I provide the code block, I would like to try your suggestion of debugging in mixed mode (both managed and native) so that I can see all of the current call stack; however, I can figure out where to set the mixed mode debugging. Where in VS2005 do I set this option

    Thanks,
    Jim.


  • reverie

    I have received the "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack" but only when I am trying to perform a redirect from within a try catch block. If I move the redirect outside the block it works fine. I created a bool value that was set in the try catch block and then performed an if statement against the bool to perform the redirect. Not my first choice for coding but it solved my problem and allowed me to me on. Good luck.
  • JimSuplizio

    I believe the answer to your problems are explained here


  • Tuur P

    Hello,

    I am having a similar problem except I am attempting to debug a SQL server 2005 c# assembly remotely.

    If I try to set the attach process parameters to use anything but the default "Automatic: T-SQL code, Managed code", then I cannot issue a "Detach All" from within Visual Studio as this is grayed out. I then have to issue a "Terminate All" which forces SQL server 2005 to hang up and I have to restart the SQL server services. In addition, my break points are ignored.

    The problems come with trying to access an SQL data reader. Typical code is as follows:

    using (SqlConnection c = new SqlConnection("context connection=true"))

    {

    SqlCommand cmd = new SqlCommand();

    cmd.Connection = c;

    cmd.CommandText = "SELECT TestID, TestName, TestTypeID from inserted";

    if (c.State != ConnectionState.Open)

    c.Open();

    SqlDataReader r = cmd.ExecuteReader();

    int TestID = 0;

    int TestTypeID = 0;

    string TestName = "Control";

    if (r.HasRows)

    {

    r.Read();

    TestID = int.Parse(r["TestID"].ToString());

    TestTypeID = int.Parse(r["TestTypeID"].ToString());

    //*********************************

    //TODO: ItemName is specific to each table, so make sure it's ok

    TestName = r["TestName"].ToString();

    }

    r.Close();

    }

    On trying to assign to the variables, from the reader, the values of the items of the reader, r, are replaced with the "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack" and the debugger exits at that point.

    Any ideas

    Thanks

    William



  • Ziva_Y

    Andy,

    No problem, glad that I could help. Just come back to these forums if you have any more development issues.

    Thanks,

    Ian



  • EvilSilver

    Hi Ian,

    Thanks for your help. I can confirm that I was using debug mode anyway, but I'll take your second suggestion into accout, the next time I get this error.

    Fortunately, after removing a try-catch block and adding a Trace I managed to get the code working again. I'm still not sure what the cause of the original problem was, but at least I can continue now. Thanks again!

    Regards,

    Andy


  • Ionut Bizau

    Seems to ba a relatively common thing. I am still attempting to figure it out. I have a Dataset that I am using to populate a .CSV file and provide the ability for the end user to 'download' the resulting file. I am populating the dataset and populating a DataTable with it. I am then creating the Folder and File with the extension '.CSV'. I then populate the file row by row using a string. The file is in the correct location, format (.CSV) and properly populated at this point.

    I then call a function that I have attempted to allow the user to download the file. I have attempted this in several different fashions using the Response object. All attempts are downloaded without the .CSV extension. I have pasted some of the code to preform this function below. I do trap an error that states "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack". I have 'Googled' this error and modified my VS Options>Debugging>Just-in-Time to all the Debug in mixed mode (Managed and Native). The error occurs when I step through the code at the Response.End
    I have attempted this with the HtmlTextWriter, StringWriter, StreamWriter and StringBuilder all with the same result.

    Can someone assist


    string strFileNameCSV = "C:\\ExportReports\\Export_CSV_" + ReportForm1.strUserID + ".csv";
    //FileInfo fiBPM_Export = new FileInfo(strFileNameCSV);

    //Clear Headers
    Response.Clear();
    Response.ClearHeaders();

    // initialize the http content-disposition header to indicate a file attachment with the filename
    Response.AppendHeader("Content-Disposition", "Attachment; Filename=\"" + strFileNameCSV + "\".csv");

    ////// transfer the file byte-by-byte to the response object
    //FileInfo fileToDownload = new FileInfo(strFileNameCSV);
    //Response.Flush();
    //Response.WriteFile(fileToDownload.FullName);
    Response.WriteFile(strFileNameCSV);
    //Response.TransmitFile(strFileNameCSV);
    //Response.Redirect(strFileNameCSV);
    Response.End();

     


  • Jay Mann

    Ian,

    A small correction........it is Tools > Attach to Process instead of Debug->Attach to Process

    Thanks

    Rajesh Medackel, India



  • Larry_Santos

    Andy,

    There are two things that I would check. First, make sure that you are working with a debug build and not a release build of your project. And second, make sure that you are debugging in mixed mode (both managed and native) so that you can see all of the current call stack.

    Thanks,

    Ian



  • KrisFB_APPS

    I have a similar problem...here is the code for a 'csv' download. Can someone help

    Response.Clear();

    Response.ClearHeaders();

    //set the conttent type of the file to be downloaded (IF NEEDED)

    Response.ContentType = "application/vnd.ms-excel";

    // initialize the http content-disposition header to indicate a file attachment with the filename

    Response.AppendHeader("Content-Disposition", "Attachment; Filename=\"" + strFileNameCSV + "\"");

    //// transfer the file byte-by-byte to the response object

    FileInfo fileToDownload = new FileInfo(strFileNameCSV);

    Response.Flush();

    Response.WriteFile(fileToDownload.FullName);

    Response.End();


  • Oliver Nguyen

    Thanks!!

    I was having the same problem here.. and now it works..

    once again. thanks.



  • Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack