FLTK SIGSEGV Segmentation Fault when calling fl_li

2019-09-20 05:04发布

问题:

Calling fl_line() in my program immediately returns an SIGSEGV exception after drawing 1 line on the screen. I've checked vectors offset, nothing out of bounds:

Here's how it looks like when it crashes

Here's the call stack

Why is fl_line() called with parameters but not Fl_graphics_driver::line() ?

Here's my program, it was to draw a simple polygon out of 4 points:

#include <iostream>

#include <FL/Fl.H>
#include <FL/Fl_draw.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Widget.H>
#include <initializer_list>
#include <vector>
#include <functional>
//#include <cmath>
//#include <math.h>

struct Point {
int x,y;
Point(int xx, int yy) : x(xx), y(yy) { }
};


class Shape: public Fl_Widget{
public:

Shape(int x, int y, int w, int h) : Fl_Widget(x,y,w,h) {}

Point point(int idx) const {
return (points[idx]);
}
unsigned int points_size() const {
return points.size();}

void draw() override{
draw_lines();
}
void line(int x, int y, int x1, int y1) const {fl_line(x,y,x1,y1);}
void add(Point p){ points.push_back(p); }

protected:
virtual void draw_lines() const{}

private:
std::vector<Point> points;
};


class ClosedPolyline: public Shape {
public:
/*ClosedPolyline(std::initializer_list<Point> pp) {
if (pp.size() > 0) {
for (Point p: pp)
add(p);
}
}
*/
ClosedPolyline(Point a1, Point a2, Point a3, Point a4) : Shape(a1.x,a1.y,a3.x-a1.x,a2.y-a1.x) {
    add(a1); add(a2); add(a3); add(a4);
}

protected:

void draw_lines() const override{

for (unsigned int i=1; i<points_size(); ++i){
    std::cout << point(i-1).x << " " << point(i-1).y << " to " << point(i).x << " " << point(i).y << std::endl;
}
/*
for (unsigned int i=1; i<points_size(); ++i){
fl_line(point(i-1).x, point(i-1).y, point(i).x, point(i).y);
}
*/

line(point(0).x, point(0).y, point(1).x, point(1).y);
line(point(1).x, point(1).y, point(2).x, point(2).y);
line(point(2).x, point(2).y, point(3).x, point(3).y);
line(point(1).x, point(1).y, point(2).x, point(2).y);
}
};



class MyWindow: public Fl_Window {
public:
MyWindow(int x, int y, int w, int h, const char* title = 0)
: Fl_Window(x, y, w, h, title) {}
void Attach(Shape &s) {
shapes.push_back(&s);
//shapes.push_back(s);
//draw();
}
//void draw_shapes(){draw();}
protected:
void draw() override{
for(Shape * s: shapes) {
s->draw();
//s.draw();
}
}

private:
std::vector<Shape*> shapes;
};

/*
class Circle: public Shape {
public:
Circle(Point p, double r): radius(r) {
add(Point{p.x - r, p.y - r});
}

protected:
void draw_line() {
fl_arc(point(0).x, point(0).y, radius + radius,radius + radius, 0, 360);
}

private:
double radius;
};

void Function(T f, double r1, double r2,
Point xy, int count = 100, double xscale = 25, double yscale = 25) {
double dist = (r2-r1)/count;
double r = r1;
for (int i = 0; i<count; ++i) {
add(Point(xy.x+int(r*xscale), xy.y-int(f(r)*yscale)));
r += dist;
}
}
};

class CosFunction {
public:
double (double x) {
return cos(x * M_PI / 180);
}
};
*/

typedef double Fct(double);

int main() {
MyWindow win(100, 100, 600, 400, "C++ Test task");

ClosedPolyline p{Point{100, 100}, Point{100, 200}, Point{500, 100}, Point{500, 200}};
/*
Function<std::function<double(double)>> f1 {[] (double x) -> double { return x * x; }, -100, 100, Point {300, 300}, 100, 20, 5};
Function<Fct> f2 {sin, -360, 360, Point{300, 300}, 200, 1, 25};
Function<CosFunction> f3{CosFunction(), -360, 360, Point{300, 300}, 200, 1, 25};
Circle c1{Point{300, 50}, 30};
*/

win.Attach(p);
/*
win.Attach(f1);
win.Attach(f2);
win.Attach(f3);
win.Attach(c1);
*/

win.end();

win.show();
return (Fl::run());
}

回答1:

The colour map has not been set. Setting the colour initializes the colour map. A good place to put it would be in the MyWindow constructor.

MyWindow(int x, int y, int w, int h, const char* title = 0)
   : Fl_Window(x, y, w, h, title) {

  fl_color(FL_FOREGROUND_COLOR);
}

A list of colours can be found in FL/Enumerations.H



标签: c++ c++11 fltk