3

In a thread, a file is being opened, closed and disposed continuosly. Does this cause an issue ?

Here is the code

 StreamWriter file1 = new StreamWriter(filepath4, true);
                        for (int i = 0; i < ChannelValueForTcp; i++)
                        {
                            file1.WriteLine(data[i]);
                        }
                                file1.WriteLine(data[data.Length-1]);
                                file1.WriteLine(data[data.Length - 2]);
                                file1.Close();
                                file1.Dispose();

enter image description here

Please help I am stuck. (This comes up randomly we are trying to run the code for 8 hours continuously.)

Edit:

No no other thread works or does anything associated with this file. It is being used only here. There are other threads running, which are giving the same error but randomly after 45 minutes - 5 hours of testing.

Here is the c code. Please download it

[DllImport("ConsoleApplication2.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern int main_c();

 public string[] tcp(string peer, int port)
        {

            int i = main_c();//the c code writes to a file called akash.txt and returns = 0 if it is successful. Then I read the file and do some functions on it. 

 if (i == 0)
            {
                StreamReader objReader = new StreamReader("akash.txt");
13
  • 2
    Do you have any unsafe code? Commented Jul 6, 2013 at 11:08
  • 2
    The StreamWriter implements IDisposable, so you want to wrap it in a using block as opposed to calling Close and Dispose yourself, although as @JonSkeet has pointed out, this isn't going to cause your error, it's just good practice. Commented Jul 6, 2013 at 11:15
  • Does this error always occurs on this exact code line? do you have other threads in the background that performs something related? Commented Jul 6, 2013 at 11:24
  • No. The error does not come here everytime. But out of 8-10 times that error has come, 2 times it was here. Commented Jul 6, 2013 at 11:30
  • 2
    You should probably show the C# declarations of the functions in the dll, along with the C declarations of the same, so that people here more versed in those things can help you figure out what happens. I'm 99.9% positive it's the unsafe code that is the culprit here. Commented Jul 6, 2013 at 11:33

1 Answer 1

2

Random crashes and FatalExecutionEngineError exceptions are normally associated with stack or heap corruption which can remain hidden until further down in your code. Ensure that you have marshalled all your C++ functions correctly using the right calling convention, parameter types and return types.

Microsoft specifies the probable cause of the message is:

The CLR has been fatally corrupted. This is most often caused by data corruption, which can be caused by a number of problems, such as calls to malformed platform invoke functions and passing invalid data to the CLR.

Judging from the code you've supplied, your declaration looks correct so it could be another function that you've marshalled that is causing an issue.

Ensure that your C++ code is stable and not the cause of the problem. I think that it might be associated with the deletion or filling of the 'res' buffer.

You could be compiling the DLL with a flag which sets the calling convention to something apart from __cdecl. You can verify this by right-clicking on the project > Properties > C/C++ > Advanced > Calling Convention.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.