0

Friends,

I have code with the following structs (note the void pointer is there as there are other structs similar to KCKey it often references. Have not shown for brevity).

I place these arrays as Global Variables (I know not best practice, but lets go along with it for now). These global variables are declared as extern in one headers, and accessed in other source code.

The problem is in struct KCKey. When using the "keyprint" variable as const char *, it compiles, runs and works well. When using the "keyprint" variable as QString, I get a SEGFAULT with the following message: "the inferior stopped because it received a signal from the operating system. Signal name SIGSEV.

The code stops in "atomic_base.h".

Why does the code work with const char * , but not QString.

I don't believe this has to do with memory allocation because: 1) it crashes with ONE instance of global variable using QString, and 2) similar code runs well using std::string in an embedded microcontroller within tight memory constraints.

struct KeyCap{
    int map;
    int row;
    int col;
    int layer;
    int keytype;
    void * void_ptr;
};


struct KCKey{
    int keycode;
    QString keyprint;
};

As requested with additional code: example global header file:

Map_A.h

#ifndef MAP_A_H
#define MAP_A_H

#include <QString>
#include "structs.h"

extern     KCMap Map_A;
extern     KCLayer l_aDefault;
extern     KCLayer l_aFN;
extern     KCLayer l_aNUM;

extern     KeyCap Map_A_Default[6][13];
extern     KeyCap Map_A_FN[6][13];
extern     KeyCap Map_A_NUM[6][13];

//~~~DEFAULT LAYER KEYS~~~~~~~~~~
extern     KeyCap AD_00;
extern     KeyCap AD_01;
extern     KeyCap AD_02;
extern     KeyCap AD_03;

Map_A.cpp

#include <QString>
#include "Map_A.h"
#include "keycodes.h"
#include "structs.h"

KCMap Map_A = {l_aDefault, l_aFN, l_aNUM};

KCLayer l_aDefault = {*Map_A_Default[0]};
KCLayer l_aFN = {*Map_A_FN[0]};
KCLayer l_aNUM = {*Map_A_NUM[0]};


KeyCap AD_00 = {zMap_A,0,0,1,no_key,&Key_None};
KeyCap AD_01 = {zMap_A,0,1,1,no_key,&Key_A};
KeyCap AD_02 = {zMap_A,0,2,1,no_key,&Key_B};
KeyCap AD_03 = {zMap_A,0,3,1,no_key,&Key_C};
KeyCap AD_04 = {zMap_A,0,4,1,no_key,&Key_D};

Keycodes.h

#include <QString>
#include "structs.h"
#include "keycodes.h"


KCKey Key_None = {0,""};
KCKey Key_A = {KEY_A,"A"};
KCKey Key_B = {KEY_B,"B"};
KCKey Key_C = {KEY_C,"C"};
KCKey Key_D = {KEY_D,"D"};
KCKey Key_E = {KEY_E,"E"};
KCKey Key_F = {KEY_F,"F"};
KCKey Key_G = {KEY_G,"G"};
KCKey Key_H = {KEY_H,"H"};
KCKey Key_I = {KEY_I,"I"};
KCKey Key_J = {KEY_J,"J"};
KCKey Key_K = {KEY_K,"K"};
NK2013
  • 47
  • 1
  • 5
  • 1
    You talk about global variables, but the code doesn't show any. There's nothing immediately wrong with these struct definitions - the problem must be in how you use them. For further assistance, prepare a [mcve]. – Igor Tandetnik Jan 02 '21 at 00:30
  • 1
    Does this answer your question? [Prevent static initialization order “fiasco”, C++](https://stackoverflow.com/questions/29822181/prevent-static-initialization-order-fiasco-c) – n. m. could be an AI Jan 02 '21 at 00:51
  • What's `KCMap` and `KCLayer`? It's very suspicious that you use `l_aDefault` et al in the initializer of `Map_A` - they themselves are not yet constructed at this point, being defined below. They in turn rely on still more global variables like `Map_A_Default` - where and how are those defined? On an unrelated note, you seem to be saying that `keycodes.h` contains `#include "keycodes.h"` line, including itself; that doesn't look right, the code you show is probably different from the code you actually compile. – Igor Tandetnik Jan 02 '21 at 03:27

1 Answers1

0

I think that the problem is how you are working with void_ptr. I tried a code derived from yours using QString in KCKey structure and it works.

#include <iostream>
#include <QString>

struct KCKey{
    int keycode;
    QString keyprint;
};

KCKey Key_None = {0,"test"};

int main(void)
{
    std::cout << Key_None.keyprint.toStdString() << "\n";
    return 0;
}
dodooft
  • 71
  • 2
  • 4
  • Yes, that was a suspicion of mine too, but also confusing because casting and dereferencing work for ints, const char *, etc. I might just stick to const char *,.. wasted so much of my time – NK2013 Jan 02 '21 at 01:26
  • I agree I don't think that you have a benefit using QString instead of std::string, but you could attach a minimal example to reproduce the behavior If you want more help to find the problem. – dodooft Jan 02 '21 at 01:34
  • thanks for the offer,.. but the code is too large to go through with a fine tooth comb, – NK2013 Jan 02 '21 at 02:08