Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x18 in tid 8522

Hello everyone! So I have this bug that's really strange to me and unsolvable.
So when I debug my c++ application android it is terminated right after
'updateData' function is called, actually whenever a variable is accessed from its object and used. Like this:

1
2
3
4
5
6
7
8
9
template<typename T>
LSOGLData {
template<typename>
friend class LSOGLVertices;
std::vector<LSOGLVertices<T>> _vertices;
void updateData(LSOGLUpdatePacketCollection<T> &collection) {
    if (getDefaultVertexCount() > 0) {} //terminates app
}
};


and that function is called from Vertices which is called from Vertex.
Vertex::setValue -> pointer Vertices::passData -> pointer Data::updateData
1
2
3
4
5
6
7
8
9
class Vertex {
    Vertices *_owner;
};
class Vertices {
    Data *_owner;
};
class Data {
    void updateData(UpdateData);
};


So Vertex has the pointer to its owner (Vertices).
Vertices class has the pointer to its owner (Data).
And when the value is changed from Vertex, the update is passed through Vertices to Data function updateData. Which when called from that way terminates app when it accesses its values.

The error is:
A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x18 in tid 8813 (le.myproject.opengl), pid 8813 (le.myproject.opengl)


I would be really grateful if someone could help me with this.
Last edited on
Can you write a minimal compilable program that demonstrates the problem that you are having?

Based on the code snippets that you have provided, I can't figure out how your template class LSOGLData<T> is related to the Vertex, Vertices or Data classes. You also have references in the text to Vertex::setValue and Vertices::passData which don't appear in the code. Also, you mention that the app terminates after a call to getDefaultVertexCount(), but don't show that code either.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>

using namespace std;

//UPDATE PACKETS
struct UpdatePacket {
    std::vector<int> _values;
    int _index = 0; //INDEX OF VALUE UPDATE.
    int _vertex_id = 0; //TH IS ACTUALLY INDEX IN CONTAINER OF ITS OWNER VERTICES.
    int _vertices_id = 0; //THIS IS ACTUALLY INDEX IN CONTAINER OF ITS OWNER DATA.
};

enum UpdatePacketState {
    kLSOGLIncrease, kLSOGLDecrease, kLSOGLAlter
};
struct UpdatePacketCollection {
    UpdatePacketState state = kLSOGLAlter;
    std::vector<UpdatePacket> _update_packets;
}
//UPDATE PACKETS

struct Vertex {
    
    std::vector<int> _values;
    
    Vertices *_owner;
    int _id_inownercontainer;
    
    public:
    
    void setValue(int index, int value) {
        _values[index] = value;
        
        //VALUE IS CHANGED, THEN APPLY UPDATE TO DATA.
        UpdatePacket update_packet;
        update_packet.index = index;
        update_packet._vertex_id = _id_inownercontainer;
        update_packet._vertices_id = _owner->_id_inownercontainer;
        
        UpdatePacketCollection update_packet_collection;
        update_packet_collection.state = kLSOGLAlter;
        _update_packets.push_back(update_packet);
        
        _owner->passData(update_packet_collection);
        //
    }
    
};

struct Vertices {
    
    friend class Vertex;
    
    std::vector<Vertex> _vertices;
    
    Data *_owner;
    int _id_inownercontainer;
    
    void passData(UpdatePacketCollection &update_packet_collection) {
        _owner->updateData(update_packet_collection);
    }
    
};

struct Data {
    
  friend class Vertices;
    
  std::vector<Vertices> _vertices;
  std::vector<int> _combined_data;
  
  int _default_vertex_count;
  
  createCombinedData() {
      //uses all vertices and mixes them with my algorithm.
  }
  
  //updates specific data in combined data when setValue is called.
  void updateData(UpdatePacketCollection &update_packet_collection) {
      //CALCULATES VALUE'S INDEX IN COMBINED DATA AND UPDATES IT.
      ///and its here when i finally get the update data and when i try to use one of variables from object
      ///like this 
      for (int i = 0; i < _default_vertex_count/*this*/; i++) {
          //IT MAKES APP TERMINATE.
      }
  }
  
  std::vector<int> getData() {
      return _combined_data;
  }
  
};

int main()
{
    
    Vertex v1, v2;
    v1._values.push_back(1);
    v1._values.push_back(0);
    v1._values.push_back(0);
    v2._values.push_back(2);
    v2._values.push_back(0);
    v2._values.push_back(0);
    
    Vertices vcs1, vcs2;
    vcs1._vertices.push_back(v1);
    vcs1._vertices.end()._owner = vcs1;
    vcs1._vertices.end()._id_inownercontainer = vcs1.size()-1;
    vcs1._vertices.push_back(v2);
    vcs1._vertices.end()._owner = vcs1;
    vcs1._vertices.end()._id_inownercontainer = vcs1.size()-1;
    
    Data data;
    data.push_back...vcs1..vcs2
    ...
    
    //WHEN I CALL SET VALUE.
    //data.getVertices(0).getVertex(0).setValue(0, 1);
    //IT CRASHES.
    
    return 0;
}
Last edited on
Can you write a minimal compilable program that demonstrates the problem that you are having?


I tried to compile what you posted (the little gear symbol links to an on-line compiler), and your code had over 30 errors and 2 warnings.

Please post a compilable program. I suspect line 114 in your post will be problematic. What are you trying to do there?

Is there anything represented by the ellipses in line 115 that could be causing a problem? We need the minimal program that still shows the erroneous behavior.

Why don't you have a member function in Vertices that does lines 106 - 108 in a single statement rather than having the user do it manually?
Because I wrote a mini model of my 1394 line code.
I will try to write a code which will simulate the problem.
Last edited on
Putarda wrote:
So when I debug my c++ application android it is terminated right after
'updateData' function is called, actually whenever a variable is accessed from its object and used.


In the debugger, what are values of the variables being used when it crashes? It should always be possible to work backwards from a crash to discover what went wrong. Are you sure the variables are initialised with a sensible value? I know that sounds like an elementary question, but I am just saying ....... you wouldn't be the first, young or old, to have that happen :+)
I tested it. It's type and value and everything was fine, except when you interact with that variable and that is really weird. I can create local variables but I can't use variables from its object.
Last edited on
All those raw _owner; pointers with no obvious initialisation look like poor design.

> fault addr 0x18
This is going to be the result of NULL->someMember, where 0x18 is the offset of that member within one of your structs/classes.


salem c should I reorganize my classes? Since my simple model is working fine but has different organization will that change something if I implement it in my real code? And what do you mean by poor design. What can I do to make it a good design?
This is simple working code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

#include <iostream>
#include <vector>

//UPDATE PACKETS
struct UpdatePacket {
    std::vector<int> _values;
    int _index = 0;
    int _vertex_id = 0;
    int _vertices_id = 0;
};

enum UpdatePacketState {
    kLSOGLIncrease, kLSOGLDecrease, kLSOGLAlter
};
struct UpdatePacketCollection {
    UpdatePacketState state = kLSOGLAlter;
    std::vector<UpdatePacket> _update_packets;
};
//UPDATE PACKETS

struct Vertices;

struct Data {
    
  friend class Vertices;
    
  std::vector<Vertices> _vertices;
  std::vector<int> _combined_data;
  
  int _default_vertex_count;
  
  void createCombinedData() {
      //uses all vertices and mixes them with my algorithm.
  }
  
  //updates specific data in combined data when setValue is called.
  void updateData(UpdatePacketCollection &update_packet_collection) {
      //CALCULATES VALUE'S INDEX IN COMBINED DATA AND UPDATES IT.
      ///and its here when i finally get the update data and when i try to use one of variables from object
      ///like this 
      for (int i = 0; i < _default_vertex_count/*this*/; i++) {
          //IT MAKES APP TERMINATE.
      }
  }
  
  std::vector<int> getData() {
      return _combined_data;
  }
  
};

struct Vertex;

struct Vertices {
    
    friend class Vertex;
    
    std::vector<Vertex> _vertices;
    
    Data *_owner;
    int _id_inownercontainer;
    
    void passData(UpdatePacketCollection &update_packet_collection) {
        _owner->updateData(update_packet_collection);
    }
    
};

struct Vertex {
    
    std::vector<int> _values;
    
    Vertices *_owner;
    int _id_inownercontainer;
    
    void setValue(int index, int value) {
        _values[index] = value;
        
        //VALUE IS CHANGED, THEN APPLY UPDATE TO DATA.
        UpdatePacket update_packet;
        update_packet._index = index;
        update_packet._vertex_id = _id_inownercontainer;
        update_packet._vertices_id = _owner->_id_inownercontainer;
        
        UpdatePacketCollection update_packet_collection;
        update_packet_collection.state = kLSOGLAlter;
        update_packet_collection._update_packets.push_back(update_packet);
        
        _owner->passData(update_packet_collection);
        //
    }
    
};

int main()
{
    
    Vertex vtx1, vtx2, vtx3;
    vtx1._values.push_back(1);//x
    vtx1._values.push_back(0);//y
    vtx1._values.push_back(0);//z
    
    vtx2._values.push_back(2);
    vtx2._values.push_back(0);
    vtx2._values.push_back(0);
    
    vtx3._values.push_back(2);
    vtx3._values.push_back(1);
    vtx3._values.push_back(0);
    
    Vertices vtcs_position, vtcs_color;
    vtcs_position._vertices.push_back(vtx1);
    vtcs_position._vertices.end()->_owner = &vtcs_position;
    vtcs_position._vertices.end()->_id_inownercontainer = vtcs_position._vertices.size()-1;
    vtcs_position._vertices.push_back(vtx2);
    vtcs_position._vertices.end()->_owner = &vtcs_position;
    vtcs_position._vertices.end()->_id_inownercontainer = vtcs_position._vertices.size()-1;
    vtcs_position._vertices.push_back(vtx3);
    vtcs_position._vertices.end()->_owner = &vtcs_position;
    vtcs_position._vertices.end()->_id_inownercontainer = vtcs_position._vertices.size()-1;
    
    vtcs_color._vertices.push_back(vtx1);
    vtcs_color._vertices.end()->_owner = &vtcs_color;
    vtcs_color._vertices.end()->_id_inownercontainer = vtcs_color._vertices.size()-1;
    vtcs_color._vertices.push_back(vtx2);
    vtcs_color._vertices.end()->_owner = &vtcs_color;
    vtcs_color._vertices.end()->_id_inownercontainer = vtcs_color._vertices.size()-1;
    vtcs_color._vertices.push_back(vtx3);
    vtcs_color._vertices.end()->_owner = &vtcs_color;
    vtcs_color._vertices.end()->_id_inownercontainer = vtcs_color._vertices.size()-1;
    
    Data data;
    data._vertices.push_back(vtcs_position);
    data._vertices.end()->_owner = &data;
    data._vertices.end()->_id_inownercontainer = data._vertices.size()-1;
    data._vertices.push_back(vtcs_color);
    data._vertices.end()->_owner = &data;
    data._vertices.end()->_id_inownercontainer = data._vertices.size()-1;
    
    data._vertices[0]._vertices[0].setValue(1, 1);//THIS MODEL WORKS.
    
    std::cout << data._vertices[0]._vertices[0]._values[1];
    
    return 0;
}

Last edited on
Edit: I didn't see the previous 2 posts

It's type and value and everything was fine, except when you interact with that variable and that is really weird


What happens there, exactly? Why can't you use that variable? I mean, it had a sensible value, but terminated anyway?

Does the for loop on line 83 iterate the whole container? But maybe you have a reason to use a variable there. If it is doing the whole thing, try replacing it with a range for with auto forwarding reference:

1
2
3
for (auto&& item :  _vertices)  {
   // do something with item
}


Doing this means one doesn't have to rely on getting the type right, or the size.

Just guessing, see if it makes any difference.

Did you have any warnings when you compiled? Do you have a high level of warnings turned on?
Last edited on
2019-02-04 22:02:11.718 24747-25290/system_process I/ActivityManager: Force stopping com.example.filip.opengl appid=10086 user=0: from pid 17233

I found this in logcat.
https://pastebin.com/brpWhmEr

2019-02-04 22:02:13.811 17265-17265/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x18
2019-02-04 22:02:13.811 17265-17265/? A/DEBUG: Cause: null pointer dereference
2019-02-04 22:02:13.812 17265-17265/? A/DEBUG: eax 00000000 ebx d3dbcb24 ecx 00000000 edx fff2ee08
2019-02-04 22:02:13.812 17265-17265/? A/DEBUG: edi fff2ef18 esi e65ec580
2019-02-04 22:02:13.812 17265-17265/? A/DEBUG: ebp fff2ed88 esp fff2ed80 eip d3d856cf
2019-02-04 22:02:14.264 17265-17265/? A/DEBUG: backtrace:
2019-02-04 22:02:14.264 17265-17265/? A/DEBUG: #00 pc 000156cf /data/app/com.example.filip.opengl-BfnGmFaL2B7eM06dmS4B_Q==/lib/x86/libnative-lib.so (lsogl::LSOGLData<float>::getDefaultVertexCount()+15)

How do I fix this?
Last edited on
How do I fix this?


So you have a null pointer: In the debugger, trace backwards through the code to see why it is a nullptr. There will be a wrong value somewhere, or something not initialised. Or maybe a logical error, like putting data into a wrong container, then wondering why the real one has nothing in it.

The main error that I can see is the many lines like
vtcs_position._vertices.end()->_owner = &vtcs_position;
you are not allowed to modify what end() points it: it points at empty space after the end of the vector.
You probably want .back() instead.
vtcs_position._vertices.back()._owner = &vtcs_position;

(it isn't all, though)
Last edited on
Thank you all for helping me out, but I think I found a bug but I'm not sure.
So in my original code I made a code which simulates the problem:
1
2
data.getVertices(0).getVertex(0).getOwner()->getOwner()->test();
//      &               &           *           *           * 

I checked all pointers and they are ok and initialized, but when I call a pointer function through this: reference->reference->pointer->pointer->function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
        //LSOGL DATA
        template<typename N>
        LSOGLVertices<T> &getVertices(N index) {
            return _vertices_collection.at(index);
        }
        //LSOGL VERTICES
        template<typename N>
        LSOGLVertex<T> &getVertex(N index) {
            return _vertices.at(static_cast<unsigned long>(index));
        }
        //LSOGL VERTEX
        LSOGLVertices<T> *getOwner() {
            return _owner;
        }
        //LSOGL VERTICES
        LSOGLData<T> *getOwner() {
            return _owner;
        }
        //LSOGL DATA
        void test() {
            if (_default_vertex_count == 0) {}
        }
Last edited on
(it isn't all, though)

here's another error It's still ther eafter fixing all end()-> to back()., and I think it's what caused your original segfault.

You create a Vertices called "vtcs_position" at line 112, which is uninitialized (by the way, this is already a major red flag: you should always initialize your objects. Write constructors for all those classes and use them)

You place the address of that, uninitialized, vtcs_position in the _owner pointers of each vertex in its own vtcs_position._vertices

Then you place a *copy* of vtcs_position inside data._vertices and populate the _owner pointer of that copy

The original vtcs_position that's sitting in main function still has an uninitialized _owner containing garbage, and so data._vertices[0]._vertices[0]._owner is still pointing at main's vtcs_position, and data._vertices[0]._vertices[0]._owner._owner is garbage

So inside setValue, you call _owner->passData, that "_owner" points at main's vtcs_position, then in that passData you call _owner->updateData, that _owner is a garbage pointer. And then in updateData, that _owner is now the "this" pointer and your loop attempts to read "_default_vertex_count" (which is also uninitialized btw) off a garbage "this" , at which point it crashes.

This explains your "fault addr 0x18": it your garbage value happened to be zero, and sizeof(vector) on your system is 12 bytes, as appropriate for 32-bit targets, then _default_vertex_count is at address 0x18.

Last edited on
Cubbi. At first glance, I didn't quite understand your answer but the moment I realized my mind was blown.
I never imagined something like this would happen and this is eye-boggling, I thought mechanism would work perfectly but I was wrong and inexperienced.
I thought this bug would end me.
Thank you very much you saved me and gave me a lesson about pointers.
Topic archived. No new replies allowed.