Thread: Segmentation Fault

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What does the Image class look like? It looks like VideoDevice has an Image member, right?

  2. #17
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    Yes, the VideoDevice has an Image member, here is it:
    Image.h
    Code:
    #ifndef IMAGE_H
    #define IMAGE_H
    
    #include <stdio.h>
    #include <jpeglib.h>
    
    class Image
    {
    public:
        Image(unsigned char* data, int width, int height, int depth = 24);
        ~Image();
        
        void convertToGray();
        void save(const char* filename, int format = Image::JPEG, int jpegQuality = 80);
        
        unsigned char* data;
    
        int width;
        int height;
        int depth;
        int size;
        
        const static short int JPEG = 0;
        const static short int PNM = 1;
    protected:
    private:
    };
    
    #endif // IMAGE_H
    Image.cpp
    Code:
    #include "Image.h"
    
    
    Image::Image(unsigned char* data, int width, int height, int depth)
    {
        this->data   = data;
        this->width  = width;
        this->height = height;
        this->depth  = depth;
        this->size   = width * height;
    }
    
    Image::~Image()
    {
        //dtor
    }
    
    void Image::convertToGray()
    {
        //TODO - fix seg fault in VideoDevice before
    }
    
    void Image::save(const char* filename, int format, int jpegQuality)
    {
        FILE* file = fopen(filename, "w");
    
        if (format == Image::PNM)
        {
            fprintf(file, "P6\n&#37;d %d\n255\n", width, height);
            fwrite(data, width * height * 3, 1, file);
        }
        else if (format == Image::JPEG)
        {
            unsigned int   lineSize = width * 3;
            unsigned char* line;
            struct jpeg_compress_struct compressInfo;
            struct jpeg_error_mgr       error;
    
            compressInfo.err = jpeg_std_error(&error);
    
            jpeg_create_compress(&compressInfo);
            jpeg_stdio_dest(&compressInfo, file);
    
            compressInfo.image_width  = width;
            compressInfo.image_height = height;
            compressInfo.input_components = 3;
            compressInfo.in_color_space   = JCS_RGB;
    
            jpeg_set_defaults(&compressInfo);
            jpeg_set_quality(&compressInfo, jpegQuality, true);
            jpeg_start_compress(&compressInfo, true);
    
            line = data;
    
            for (int i = 1;i <= height;i++)
            {
                jpeg_write_scanlines(&compressInfo, &line, 1);
                line = data + (lineSize * i);
            }
    
            jpeg_finish_compress(&compressInfo);
            jpeg_destroy_compress(&compressInfo);
        }
        fclose(file);
    }

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Ehh... that doesn't help much. The destructor doesn't do anything.

    The only thing I can think of is that you are overwriting memory or something, maybe with mbuffer?

  4. #19
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    My god, im so frustrated, is there any chance that the one who's messing up with my app is the Netbeans generated makefile? If i compile my application by hand it works... I'm so angry &#172;&#172;.

  5. #20
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Scarvenger View Post
    The Capture.cpp.cc:33 is at the red line... Yes, all goes down when i delete the VideoDevice object, and when there´s nothing at the destructor the program still crashes...
    Memory corruption.

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yes, very likely memory corruption.

    Output the pointer value directly after creating the VideoDevice, and again directly before deleting it, and see if the values are the same.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Very strange. Electric Fence is supposed to diagnose the instant you step outside the bounds of allocated memory, yet it still falls over in free()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #23
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Only for heap overflows. But this could be a stack overflow overwriting the pointer itself. (Hmm ... but then it should overwrite the return address, too ...)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #24
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    The address of VideoDevice before creating and before deleting it is the same.. I saw that if i call a method

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM