none
My program is crashing at get line in release mode, it is working properly in debug mode, i am using VS2017. Please help. RRS feed

  • Question

  • I am getting below errors at getline.

    Debug Assertion Failed!

    Program: ...ion\SpeakerDiarization\bin\x64\Release\SpeakerDiarization.exe
    File: minkernel\crts\ucrt\src\appcrt\lowio\read.cpp
    Line: 381

    Expression: _osfile(fh) & FOPEN

    For information on how your program can cause an assertion
    failure, see the Visual C++ documentation on asserts.

    (Press Retry to debug the application)

    // Some line of code from my program

    std::ifstream is(filename.c_str(), std::ifstream::in);

    string line ;

    if(is.good())

    {

    std::getline(is, line) // crashing here

    }

    Friday, August 16, 2019 11:11 AM

All replies

  • Maybe there is a code before getline that causes a memory corruption.

    For example, check if all of the arrays and vectors are used correctly.

    Friday, August 16, 2019 11:32 AM
  • There is a log before this (std::getline(is, line)) line , which is coming, and one log after this line , which is not coming. Crashing on this line only, and as I said there is no issue in debug mode.
    Friday, August 16, 2019 11:37 AM
  • Hello,

    I've seen a difference between string and std::string before. So this could already help:

    try to define the variable as

    std::string line;


    Or use

    char szline[1024];
    is.getline(szline, 1024);

    http://www.cplusplus.com/reference/istream/istream/getline/


    Regards, Guido


    Friday, August 16, 2019 11:43 AM
  • Tried both the solution, didn't help, same problem.  
    Friday, August 16, 2019 12:03 PM
  • Void X(const std::string &filename) {
    std::ifstream is(filename.c_str(), std::ifstream::in);
    if (!is.good()) {
    cout << "Cannot open config file: " << filename;
    }

    std::string line, key, value;
            int32 line_number = 0;

            cout<<"A"; // this log is coming

        while (std::getline(is, line)) {

                    cout<<"B"; // this is not coming

            line_number++;

    }

    cout<<"C"; // nor this log is coming

    }

    Friday, August 16, 2019 12:10 PM
  • This code has significantly different logic than what you originally showed us.  You need to show us real code which exhibits the problem.

    In this code, f the stream is not good, you display an error message BUT then you try to perform a getline anyway.  Show us some real code.

    Friday, August 16, 2019 12:56 PM
  • Well, I would only use ifstream routines, not istream routines. In Addition, when !good you should cout filename.c_str().

    Try is_open() instead of good().

    There will be an assertion when the file cannot be opened, but normally the assertion would come before cout A. Use open() instead.

    std::ifstream is; is.open(filename.c_str()); if (!is.is_open()) { cout << "Cannot open config file: " << filename.c_str();
    return; } std::string line, key, value; int32 line_number = 0; char szline[1024]; cout<<"A"; // this log is coming while (is.getline(szline, 1024)) { line=szline; cout<<"B"; // does it come now? line_number++; } //is.close();


    Regards, Guido


    Friday, August 16, 2019 1:05 PM
  • This is the original code.

    just i replaced the log and error message.

    Void X(const std::string &filename) {
    std::ifstream is(filename.c_str(), std::ifstream::in);
    if (!is.good()) {
    cout << "Cannot open config file: " << filename; // Here I am using my Err log which will return from //here only, if there is some issue to open the file, and it is not returning from here. because As I said // Log A is coming. but not B and C. I am using DLL to call this function from .cs program.

    }


    std::string line, key, value;
            int32 line_number = 0;

            cout<<"A"; // this log is coming

        while (std::getline(is, line)) {

                    cout<<"B"; // this is not coming

            line_number++;

    }

    cout<<"C"; // nor this log is coming

    }


    Friday, August 16, 2019 1:07 PM
  • This is the original code.

    just i replaced the log and error message.

    Void X(const std::string &filename) {
    std::ifstream is(filename.c_str(), std::ifstream::in);
    if (!is.good()) {
    cout << "Cannot open config file: " << filename; // Here I am using my Err log which will return from //here only, if there is some issue to open the file, and it is not returning from here. because As I said // Log A is coming. but not B and C. I am using DLL to call this function from .cs program.

    }


    std::string line, key, value;
            int32 line_number = 0;

            cout<<"A"; // this log is coming

        while (std::getline(is, line)) {

                    cout<<"B"; // this is not coming

            line_number++;

    }

    cout<<"C"; // nor this log is coming

    }


    Yes I read before that cout A is coming, so the !good is not true. Just like I have written.

    But what about trying the normal ifstream-routines instead of doing nothing (only copying the original code which is not working for you does not solve your problem)?

    Friday, August 16, 2019 1:20 PM
  • Part of the Code

    ================================================================                std::ifstream is;
    is.open(filename.c_str());
    KALDI_LOG << "Sadiq BBB"; 
    if (!is.is_open()) {
    KALDI_ERR << "Cannot open config file: " << filename.c_str();

    }
    KALDI_LOG << "Sadiq C1";
    std::string line, key, value;
    int32 line_number = 0;
    char szline[1024];

    KALDI_LOG << "A"; 

    while (is.getline(szline, 1024)) {
    line = szline;
    KALDI_LOG << "B";

                            line_number++;
    }
    is.close();
    KALDI_LOG << "C4"

    =====================================

    Log from this code, which is coming:

    Sadiq BBB

    Sadiq C1

    A

     Again crashing at getline, and as I said , I am calling this function with the help of DLL(from .cs code)

    This code is working in debug mode  for this project only, and in release mode also, when I am writing a normal test program to test release mode.

    But for this project, it is not working in release mode and crashing at getline.


    Friday, August 16, 2019 1:51 PM
  • This code is working in debug mode  for this project only, and in release mode also, when I am writing a normal test program to test release mode.

    But for this project, it is not working in release mode and crashing at getline.


    So the problem most likely is somewhere else in the code of the project that experiences the release mode crash but the first visible manifestation is the assertion.

    It is not uncommon for release builds to reveal bugs that were not noticed in a debug build.  Debug builds will initialize variables with special values and also allocate larger buffers for debugging routines.  So you could actually have a buffer overrun in debug mode that isn't noticed but that blow up in release mode.  If you search the forums for debug vs. release behaviors you will find many discussions of such issues.

    As Viorel_ suggested earlier, you should carefully review other code for correctness. From what you have said, I believe that focusing attention on getline is unlikely to solve the problem.

    Friday, August 16, 2019 3:29 PM
  • This is the first function of DLL, which i am calling from .cs projects. I tried other methods also to read the file, it is crashing when program tried to read the file every time. No issue to open the file. 

    I tried getline and  below one also.

    char s[10000];

    while (ifile.eof() == 0)
    {

    LOG<<"A"; // coming

    ifile >> s; //Crashing here this time

    LOG<<"B"; // not coming 

              }

     
    Monday, August 19, 2019 8:55 AM
  • Hello,

    you have already found out, that a test code works in Release, only in the actual project, the code does not work.

    Although many people have already suggested, that there is something wrong with your memory management and/or variable initialization, you are still focusing on getline and similar functions to read the file.

    The ifstream variable seams to be corrupt. But it is not shown in your code snippets why. Something is happing with your memory somewhere else, which corrupts the ifstream or line buffer.

    Can you access the whole array area of szline before you call getline? If yes, only the ifstream buffer is corrupt.

    You could also try to increase the memory in the linker project settings.

    Regards, Guido

    Monday, August 19, 2019 9:35 AM
  • char s[1024];
    memset(s, 'A', 1024 * sizeof(char));
    int i;
    for (i= 0; i < 1024; i++)
    cout << s[i] << endl;
    cout << "count" << i;

    this is working, means I can access whole array.

    So ifstream buffer is crashing. 

    Monday, August 19, 2019 12:09 PM
  • Earlier you said "Again crashing at getline, and as I said , I am calling this function with the help of DLL(from .cs code)."

    Maybe there is a problem with your DLL or the manner in which it is being called from managed code.

    Monday, August 19, 2019 1:14 PM