Memory leak in the sample code to capture image with Opencv with GLIB [closed]

asked 2015-02-21 10:53:24 -0600

venkat.330 gravatar image

I am pretty new to camera sort of drivers and trying to explore the best available options in capturing a IMAGE from a webcam.

I indeed have specs like the APP needs to cross platform & use front cam to capture image.

To start this development process i used openCV to capture image. However, when multiple cameras are connected to the system.

To implement camera functionality here is the sample code i have built.

I see some memory leak in code when capturing continuous image with openCv. Unable to find the cause.

/*
 ============================================================================
 Name        : cameraLibSam.c
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <opencv/highgui.h>
#include <glib.h>
//#include "libuvc/libuvc.h"
#include <stdio.h>
#include <windows.h>
#include <libusb-1.0/libusb.h>
#include <conio.h>

#include <opencv2/core/core_c.h>
#include <opencv2/highgui/highgui_c.h>
#include <opencv/cv.h>

#define SET 1
#define GET 0


GHashTable* cameraTable;

debug_log(char *data,int level){
    printf("%s",data);
}
typedef struct
{
    char cmd;
    int width;
    int height;
    int brightness;
    int contrast;
    int saturation;
    int gain;
    int quality;
    char file[20];
    char device[20];
    char nextcmd[20];
    int delay;
}camera_dev_t;

int process_camera (camera_dev_t *settings);

int printdev(libusb_device *dev, char *devId); //prototype of the function

 //TODO static const char version[] = VERSION;
 int run = 1;
 char temp[256];

char getCameraModel(void);

static CvCapture* capture;

void freeVals(gpointer key, gpointer value,gpointer user_data)
{
    printf("Key : %s --> Value : %s\n",key,value);
    free(key);
    free(value);
}

void printKeys(gpointer key, gpointer value,gpointer user_data)
{
    printf("Key : %s --> Value : %s\n",key,value);
}
int getcameraConfigurationInfo()
{
    GKeyFile *key_file;
    GError *error;
    gint keyCount;
    gsize num_keys;
    gchar **keys;
    gchar *value;

    key_file = g_key_file_new();
    error = NULL;
    if(!g_key_file_load_from_file(key_file,"config.ini",
            G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS,
                                  &error))
    {
         g_debug("%s", error->message);
         return -1;
    }

        if(g_key_file_has_group(key_file,"PROVIEW"))
        {
            keys = g_key_file_get_keys(key_file,"PROVIEW",&num_keys,&error);
            printf("dd NUMKEY %d\n",num_keys);
            int len=0;
                for(keyCount = 0;keyCount < num_keys;keyCount++)
                {
                    value=g_key_file_get_string(key_file,"PROVIEW",keys[keyCount],&error);
                    if(strstr(keys[keyCount],"Priority") != NULL)
                    {
                        len = strlen(value) + 9;
                        char *charVal = (char *)malloc(len);
                        memset(charVal,0,sizeof(charVal));
                        sprintf(charVal,"PROVIEW%s", value);
                        char *charKey = (char *)malloc(strlen(keys[keyCount])+9);
                        sprintf(charKey,"PROVIEW%s", keys[keyCount]);
                        g_hash_table_insert(cameraTable, charKey, charVal);
                        //if(value !=NULL)
                            //free(value);\
                        if (value)
                        //g_strfreev(value);
                    }
                    else
                    {
                        len = strlen(keys[keyCount]) + 9; //adding groupName ref + 1 byte extra for NULL
                        char *charKey = (char *)malloc(len);
                        char *charVal = (char *)malloc(strlen(value)+1);
                        memset(charKey,0,sizeof(charKey));
                        sprintf(charKey,"PROVIEW%s", keys[keyCount]);
                        memset(charVal,0,sizeof(charVal));
                        strcpy(charVal,value);
                        //sprintf(charVal,"PROVIEW%s", keys[keyCount]);
                        g_hash_table_insert(cameraTable, charVal, charKey);
                    }
                }
            g_hash_table_foreach(cameraTable,(GHFunc)  printKeys,NULL);
            g_strfreev(keys);
        }
        else
        {
            printf("Group not found %d\n",0);
            g_key_file_free(key_file);
            return -1;
        }
    g_key_file_free(key_file);
    return 0;
}

int printdev(libusb_device *dev,char *devId) {
    int i = 0;
    struct libusb_device_descriptor desc;
    int r = libusb_get_device_descriptor(dev, &desc);
    if (r < 0) {
        printf("failed to get device descriptor\n");
        return -2;
    }
    if((int)desc.bDeviceClass== 239){
        sprintf(devId,"%04X,%04X",desc.idVendor,desc.idProduct);
        printf("Number ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-11-17 14:09:23.000857

Comments

1

using opencv's c-api is no more a valid option in 2015. if you have to use c, rather avoid opencv at all, and try with low-level c-libs instead.

(it's a bad question here, since such code should not be written at all any more.)

berak gravatar imageberak ( 2015-02-21 11:00:47 -0600 )edit

@berak : I was really not aware of this sorry.. Just tried it as a option...

venkat.330 gravatar imagevenkat.330 ( 2015-02-21 11:03:07 -0600 )edit

imho, the main problem in your code is, that you're creating a new capture object each time you call captureImageFromCam(). instead , have 1 single instance in your main function, and reuse it whenever you need an image from it..

berak gravatar imageberak ( 2015-02-21 11:45:40 -0600 )edit