0

This is the structure I'm using for maintaining a queue

struct image_data_struct {                                                      
    uint32_t frame_id, no_of_files, tile_id, start_x, start_y, width, height;   
    float *tile_image_buffer;                                                   
    image_data_struct():frame_id(), no_of_files(), tile_id(), start_x(), start_y(), width(), height(), tile_image_buffer(NULL) {
        tile_image_buffer = (float*)malloc(300*300*3*sizeof(float));            
    }                                                                           
    ~image_data_struct(){                                                       
        if(tile_image_buffer != NULL){                                          
            free(tile_image_buffer);                                            
            tile_image_buffer = NULL;                                           
        }                                                                       
    }                                                                           
};
image_data_struct tile_img;                                                     
                                                                                
std::queue<image_data_struct> imgQ;                                             
                                                                                
int main()                                                                      
{                                                                               
    float test_img[300*300*3];                                                  
    memset(test_img,2,300*300*3*sizeof(float));                                 
    memcpy(tile_img.tile_image_buffer,&test_img,sizeof(test_img));              
    imgQ.push(tile_img);                                                        
                                                                                
    printf("\nSize of q: %d \n",imgQ.size());                                   
    printf("\nimg addr %p \n",imgQ.front().tile_image_buffer);                  
    imgQ.pop();                                                                 
    return 0;                                                                   
} 

I'm using a destructor for freeing the buffer. so I'm getting segmentation fault when the structure goes out of scope, i.e while calling free

Dharma B
  • 21
  • 9
  • 2
    Since this is C++, `malloc` doesn't really belong here. Consider `new[]` or, even better, `std::vector`. Also use `nullptr` in C++ code, where `NULL` is a C thing. – tadman Dec 03 '20 at 05:39
  • Where does it crash? Precisely at the end of `main()`, or at the `pop()` operation? It's possible you're breaking something with `memcpy()` but you'd have to use a debugger to check that pointer doesn't get damaged somehow. – tadman Dec 03 '20 at 05:42
  • 4
    There is a violation of the rule of 3/5/0. The push creates a copy, so you have two objects with the same `tile_image_buffer`. The pop frees the memory, then the global cleanup of `tile_img` will try to free that already freed block of memory. – 1201ProgramAlarm Dec 03 '20 at 05:48
  • `memset` won't set the values of your array to 2 (assuming that's what you're trying to do) it'll set each byte to 2 instead resulting in a value of `9.55147e-38`, use `std::fill` instead. You should also generally use `std::copy` rather than `memcpy`. Also `test_img` is a megabyte which is rather large for a stack allocation (and will probably crash on Windows) – Alan Birtles Dec 03 '20 at 06:40
  • I have managed to overcome segmentation fault by replacing the pointer `float *tile_image_buffer;` with an array of known size `float tile_image_buffer[300*300*3];` so that malloc won't be required. – Dharma B Dec 03 '20 at 06:51
  • Depending on where you allocate your `image_data_struct` elements you may run into stack overflows with that approach, you'd be better off using `std::vector` which is always allocated on the heap – Alan Birtles Dec 03 '20 at 07:50
  • Re: "How to use `malloc` ... in C++" -- don't. – Pete Becker Dec 03 '20 at 16:01

0 Answers0