Page 1 of 1

Memory corruption error when accessing files?

Posted: 23. May 2014, 11:35
by PacMan
Hello!

I'm running Ubuntu 12.04 on VirtualBox 4.3.12 on Mac OS X 10.9 as host. In the last few days I tried to solve some chemistry mechanisms with an extension to the open-source software Cantera 2.0.0. In my particular case, there is an input.txt file, which can be modified to define the boundary conditions. The software solves some equations and writes two files. The first one is there to store all the results of the calculation, the second one serves as the basis for the next run.

Whereas the calculation runs fine, I sometimes (depends on the entries in the input file) get an error when the program tries to access the files:

Code: Select all

*** glibc detected *** ./flamelet: malloc(): memory corruption: 0x0000000000e67360 ***
The numbers at the end differ from case to case.

I took a look at the code of this "flamelet" executable and localized the error. It might help you to have a look also, so I insert the important part of the code:

Code: Select all

    //-------Scalar Dissipation Rate calculation
    double a = sqrt(abs(solution(1,4))/ gas.density());
    double dissRate = 2.0*a/M_PI*exp(-2.0*pow(boost::math::erfc_inv(2.0*Z_st),2));

    cout << "Scalar Dissipation Rate: " << dissRate << " 1/s"<< endl;

    //-------Construct Filename
    ostringstream strs;
    strs << dissRate;
    std::string str_dissRate = strs.str();
    string strfile = "canteraTables/canteraTable_" + str_dissRate + ".csv";

    char * filename = new char[strfile.length()];
    strcpy(filename,strfile.c_str());

    // Print the flamelet table to be used in OPENFOAM
    ofstream myfile;
    myfile.open(filename);
    output_Excel(myfile, filename, names, solution);
 
    string title;
    string id;
    string desc;
    cout << "Do you want to write the initial guess file? press 1 for yes or 0 to no";
    cin >> log_num;

    if(log_num==1){
    remove ("solution.xml"); 
    flame.save(title= "solution.xml",id="id_01", desc="case_1");}
The error occurs at the lines "myfile.open(filename)" or "flame.save(title= "solution.xml",id="id_01", desc="case_1")", depending on which one is executed first (I changed the order as a test). I tried to run the code with the same boundary conditions on another system with ubuntu running as OS and it worked fine. Thats why I assume that there is a VirtualBox problem managing the memory.

Can someone please help me with my current problem? Many thanks in advance!

Re: Memory corruption error when accessing files?

Posted: 23. May 2014, 13:03
by socratis
Where is the file that you want to open or save located at? Is it in a shared folder maybe?

Re: Memory corruption error when accessing files?

Posted: 23. May 2014, 13:18
by PacMan
socratis wrote:Where is the file that you want to open or save located at? Is it in a shared folder maybe?
At first I have to say that I'm not exactly sure what this myfile.open function does, because I'm not an expert in C++. To answer your question, the solution file is located in the same folder as the flamelet executable. No shared folders...

In addition I have to mention that everything works fine sometimes, depending on the entries in the input file. Unfortunately I can't figure out why.

Re: Memory corruption error when accessing files?

Posted: 23. May 2014, 13:25
by socratis
If there are no VirtualBox features involved (such as shared folders), and it works sometimes, I would look for the problem in your code and/or your setup. Maybe the memory of your guest is indeed too low?

Re: Memory corruption error when accessing files?

Posted: 23. May 2014, 16:22
by PacMan
socratis wrote:Maybe the memory of your guest is indeed too low?
My system has 8 GB of memory, the VM usually uses 2 GB. I also tried with 4 GB, changed nothing.

Re: Memory corruption error when accessing files?

Posted: 23. May 2014, 18:11
by smithlar
"filename" should have room for the trailing nul byte of a Std C string,
like this:

Code: Select all

char * filename = new char[strfile.length() + 1];

Re: Memory corruption error when accessing files?

Posted: 24. May 2014, 11:33
by PacMan
smithlar wrote:"filename" should have room for the trailing nul byte of a Std C string,
like this:

Code: Select all

char * filename = new char[strfile.length() + 1];
Seems to be working fine, thank you very very much! :wink: