Linux下c++代码fwrite处出现段错误

2022-11-11 16:10:13 浏览数 (1)

在windows系统下运行下面的代码可以正常运行

但到了linux下,出现段错误

通过gbd调试检测到是fwrite出现的问题(段错误提示在代码下面)

通过打断点检测也确实是fwrite将数据写入流的时候不能写入出现的段错误。

尝试了很多方法都不能解决,求助大佬

#include <GL/glew.h>

#include <GL/freeglut.h>

#include <bits/stdc .h>

#include <stdlib.h>

#include <GL/glut.h>

#include <ctime>

#include <unistd.h>

#include <opencv2/opencv.hpp>

#include <opencv2/core/core.hpp>

#include <opencv2/highgui/highgui.hpp>

using namespace cv;

using namespace std;

Mat I = imread(".https://img.yuanmabao.com/zijie/pic/h1.png");

int width = I.cols;

int height = I.rows;

GLubyte* pixels;

static GLfloat spin1 = 0.0;    

static GLfloat move1 =0.0;      

static GLfloat size1 = 1.0;

int n = 0;

FILE* pPipe = NULL;

long long lSize = 544 * 960 * 4;

int fpipe;

GLuint load_texture(const char* fileName)

{

    GLuint texture_ID;

    GLubyte* pixels = NULL;

    Mat  dst = imread(fileName, -1);

    Mat img;

if (dst.channels() != 4)

    {

cvtColor(dst, img, CV_BGR2BGRA);

    }

if (dst.channels() == 4)

        img = dst;

    // if (img.empty())

    // {

    //  fprintf(stderr, "Can not load image %s", fileName);

    //  return -1;

    // }

int width = img.cols;

int height = img.rows;

int channel = img.channels();

int pixellength = width * height * channel;

    pixels = new GLubyte[pixellength];

memcpy(pixels, img.data, pixellength *sizeof(char));

glGenTextures(1, &texture_ID);

glBindTexture(GL_TEXTURE_2D, texture_ID);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);

glEnable(GL_TEXTURE_2D);

free(pixels);

return texture_ID;

}

GLubyte* SavePNG()

{

    GLubyte * pPixelData = NULL;

    GLint picWidth = 544;

    GLint picHeight = 960;

    pPixelData = new GLubyte[544 * 960 * 4];

if (pPixelData == 0)

return 0;

glReadBuffer(GL_FRONT);

glReadPixels(0, 0, picWidth, picHeight, GL_RGBA, GL_UNSIGNED_BYTE, pPixelData);

return pPixelData;

}

void pipe(GLubyte* data)

{

    cout<<"pipe start"<<endl;

fwrite(data, lSize, 1,  pPipe); //出现段错误,无法向流中写入数据

    cout<<"pipe end"<<endl;

fflush(pPipe);

}

void display()

{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    GLuint image = load_texture(".https://img.yuanmabao.com/zijie/pic/h1.png");

glViewport(0, 0, (GLsizei)width, (GLsizei)height);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(-1, 1, -1, 1, -1, 1);

glEnable(GL_TEXTURE_2D);    

glBindTexture(GL_TEXTURE_2D, image);    

glTranslatef(move1, 0, 0);              

glRotatef(spin1, 0, 0, 1);              

glScalef(size1, size1, 1);          

glBegin(GL_POLYGON);    

glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.5, 0.5);    

glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.5, -0.5);  

glTexCoord2f(1.0f, 1.0f); glVertex2f(0.5, -0.5);    

glTexCoord2f(1.0f, 0.0f); glVertex2f(0.5, 0.5);

glEnd();    

glDisable(GL_TEXTURE_2D);  

glutSwapBuffers();

}

void spinAndSizeDisplay()

{

    n = 1;

if (spin1 < 180)

    {

        spin1 = 1;

if (spin1 > 180)

        {

            spin1 -= 180;

        }

    }

    GLubyte * data = SavePNG();

pipe(data);

free(data);

    cout<<"pipe"<<endl;

if (n > 200)

    {

        // close(fpipe);

pclose(pPipe);

exit(0);

    }

    //spin1 < -180 ? spin1 = 180 : spin1 -= 1;

    //size1 > 2 ? size1 -= 2 : size1 = 0.003;

    //move1 = move1 > 100 ? move1 -= 100 : move1 = 1;

glutPostRedisplay();                

    //sleep(0.5);

}

int main(int argc, char** argv)

{

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);

    string a = "/usr/local/ffmpeg/bin/ffmpeg -f rawvideo -vcodec rawvideo  -video_size 544x960 -pixel_format rgba -itsoffset 1.5 -i - -i .https://img.yuanmabao.com/zijie/pic/db.mp4 -i .https://img.yuanmabao.com/zijie/pic/sc/sc_=.png -filter_complex [0:v]vflip[a];[1:v][a]overlay[b];[b][2:v]overlay -r 25 -t 14.04 -loglevel quiet -y .https://img.yuanmabao.com/zijie/pic/output.mp4";

const char* p = NULL;

    p = a.c_str();

    pPipe = popen(p, "wb");

    // cout << "trans before"<<endl;

    // fpipe = fileno(pPipe);

    // cout << "trans after"<<endl;

glutInitWindowSize(width, height);

glutInitWindowPosition(100, 100);

glutCreateWindow("OpenGL");

glutDisplayFunc(display);

glutIdleFunc(spinAndSizeDisplay);

glutMainLoop();

}

0 人点赞