When i pass the "nullptr" as constructor argument, i get "Guru Meditation Error: Core 1 panic'ed (LoadProhibited)." erro

eng.tavakoli110
Posts: 2
Joined: Thu Feb 03, 2022 2:29 pm

When i pass the "nullptr" as constructor argument, i get "Guru Meditation Error: Core 1 panic'ed (LoadProhibited)." erro

Postby eng.tavakoli110 » Thu Feb 03, 2022 2:41 pm

I want to write a CSV parser library. I have a class that holds the document itself which is called CSV with a method that finds and returns the desire row of document :

Code: Select all

class CSV
{
private:
    /* data */
    char* const m_buffer{nullptr};
    const uint16_t m_number_of_columns;

public:
    CSV(size_t size, uint16_t number_of_column);
    ~CSV();
    //This function finds a row through its number.
    CSV_Row row_get (uint16_t row_number){
        char *buf = find_start_of_row(row_number); //If row_number don't be valid returns "nullptr"
        Serial.println("Debug:1");
        //The error will occur here, when send nullptr to CSV_Row constructore.
        if (nullptr == buf) return (CSV_Row(nullptr));
        Serial.println("Debug:2");
        return CSV_Row(this, buf);
    };

    friend class CSV_Row;
};
Also i have a CSV_Row class that holds a row of document :

Code: Select all

class CSV_Row
{
private:
    /* data */
    CSV* m_csv;
    const char**m_cells{nullptr};
    char* m_location{nullptr};

public:
    CSV_Row(CSV* csv, char* const location = nullptr)
    {
        Serial.println("Debug:3");
        if (nullptr != csv)
        {
            m_csv = csv;
            m_location = location;
            m_cells = new const char*[m_csv->m_number_of_columns];
            Serial.println("Debug:4");
        }
    };
    ~CSV_Row();

};
And i use above classes as :

Code: Select all

#define VALID
CSV my_csv(1000, 4);
void setup (){
/*I initialize the my_csv here, its content is :
col1,col2,col3,col4
ce11,ce12,ce13,ce14
ce21,ce22,ce23,ce24
*/

#ifdef VALID
CSV_Row my_row = my_csv.row_get(1);  //Use method with valid argument
#else
CSV_Row my_row = my_csv.row_get(6);   //Use method with invalid argument
#endif
Serial.println("Debug:5");
}

void loop()
{
}
When i use row_get() method with valid argument the program runs without any error and serial monitor shows :
Debug:1
Debug:2
Debug:3
Debug:4
Debug:5
and when i use those method with invalid argument the program will crash and serial monitor show :
Debug:1
Debug:3
Debug:5
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
I remove additional data
What is wrong with my code?

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: When i pass the "nullptr" as constructor argument, i get "Guru Meditation Error: Core 1 panic'ed (LoadProhibited)."

Postby ESP_Sprite » Fri Feb 04, 2022 5:10 am

Code: Select all

if (nullptr == buf) return (CSV_Row(nullptr));
will result in a call to

Code: Select all

CSV_Row(nullptr, nullptr)
You probably want

Code: Select all

if (nullptr == buf) return (CSV_Row(this, nullptr));

Craige Hales
Posts: 94
Joined: Tue Sep 07, 2021 12:07 pm

Re: When i pass the "nullptr" as constructor argument, i get "Guru Meditation Error: Core 1 panic'ed (LoadProhibited)."

Postby Craige Hales » Sat Feb 05, 2022 1:37 am

m_csv is uninitialized for that code path; you should at least set it to null. Something shortly after the return may be trying to use it.
Is there a destructor? or a copy constructor? Do they try to free the memory m_csv points to?
Craige

eng.tavakoli110
Posts: 2
Joined: Thu Feb 03, 2022 2:29 pm

Re: When i pass the "nullptr" as constructor argument, i get "Guru Meditation Error: Core 1 panic'ed (LoadProhibited)."

Postby eng.tavakoli110 » Sat Feb 05, 2022 12:21 pm

Craige Hales wrote:
Sat Feb 05, 2022 1:37 am
m_csv is uninitialized for that code path; you should at least set it to null. Something shortly after the return may be trying to use it.
Is there a destructor? or a copy constructor? Do they try to free the memory m_csv points to?
Thanks, Yes i had a problem in destructor. I'm newbie in c++, so in checking and reviewing i did not see destructor.

Who is online

Users browsing this forum: No registered users and 101 guests