Using std::string concatenation in combination with modules gives a segmentation fault using gcc

38 Views Asked by At

The following code compiles without error using g++ (GCC) 12.2.1 20230201

But when I run it I get:

Segmentation fault (core dumped)

If I remove the three statements

        name (name),
        mutexName (name + "_mutex"),
        memoryName (name + "_memory")

the error disappears.

Main program: test_main.cpp

import shared_buffer;

namespace sb = shared_buffer;

struct DataRecord {
    int i;
};

int main () {
    sb::Handle <DataRecord, 10> handle ("my_handle");
    return 0;
}

Module: shared_buffer.cpp

export module shared_buffer;

import <string>;
import <iostream>;

namespace st = std;

export namespace shared_buffer {

template <typename Record, int maxNrOfRecords>
struct Content {
    int size = 0;
    Record records [maxNrOfRecords];
};

template <typename Record, int maxNrOfRecords>
class Handle {
public:
    st::string name;
    st::string mutexName ;
    st::string memoryName;
    Content <Record, maxNrOfRecords> content;

    Handle (st::string name):
        // Adding the following 3 lines results in: Segmentation fault (core dumped)
        name (name),
        mutexName (name + "_mutex"),
        memoryName (name + "_memory")
    {
        st::cout << name << '\n';
        st::cout << mutexName << '\n';
        st::cout << memoryName << '\n';
    }
};

}

I compile under Manjaro Linux using script: compile

clear
g++ -std=c++20 -fmodules-ts -xc++-system-header string
g++ -std=c++20 -fmodules-ts -xc++-system-header iostream
g++ -std=c++20 -fmodules-ts shared_buffer.cpp test_main.cpp

Can anyone tell me what's wrong and how to fix?

0

There are 0 best solutions below