0

I'm trying to write a simple fingerprint scanning program in c++ using libfprint, however it intermittently segfaults when run. Valgrind says the error is in the call to fp_enroll_finger which is consistent with my debugging, however beyond that I have absolutely no idea what is causing this error. Some of the times the program is run its fine, but some times it seems to consistently segfault for a period of time whenever the program is run? Heres the code:

#include <iostream>

extern "C"
{
    #include <libfprint/fprint.h>
}

using namespace std;

fp_dev * fpdev;
fp_print_data ** fpdata;

bool createDevice();

int main(int argc, char **argv)
{
    int r = fp_init();
    if(r != 0)
    {
        return r;
    }

    while(createDevice())
    {
        cout << "Scan right index finger" << endl;
        int enrollStatus = fp_enroll_finger(fpdev, fpdata);

        if(enrollStatus != 1)
        {
            cout << "Bad scan" << endl;
            fp_dev_close(fpdev);
        }
        else
        {
            cout << "Good scan" << endl;
            fp_print_data_save(fpdata[0], RIGHT_INDEX);
            break;
        }
    }

    if(fpdev != NULL)
    {
        fp_dev_close(fpdev);
    }

    fp_exit();
    return 0;
}

bool createDevice()
{
    fp_dscv_dev ** listOfDiscoveredDevs;
    fp_dscv_dev * discoveredDevice;
    listOfDiscoveredDevs = fp_discover_devs();
    discoveredDevice = listOfDiscoveredDevs[0];

    if(discoveredDevice != NULL)
    {
        cout << "Device found" << endl;
        fpdev = fp_dev_open(discoveredDevice);
    }
    else
    {
        cout << "No device found" << endl;
        return false;
    }

    fp_dscv_devs_free(listOfDiscoveredDevs);
    return true;
}
  • Does fpdata need data allocated to it? Also, after you close fpdev, set it to NULL. Otherwise, you will end up closing it twice when `fp_enroll_finger` does not return 1. – jeffmagill Apr 19 '13 at 23:41

1 Answers1

2

You need to define fpdev and fpdata as:

fp_dev * fpdev;
fp_print_data * fpdata;

And use them as:

fp_enroll_finger(&fpdev, &fpdata);

Also don't forget to free fpdata when you no longer need it with fp_print_data_free

fp_dev * fpdev;
fp_print_data ** fpdata;

Will create 2 uninitialised pointers pointing to random memory location and leading to segfault once fp_enroll_finger will attempt to acces that location. Checking fp_enroll_finger return value can be useful as well.

alexrider
  • 4,449
  • 1
  • 17
  • 27
  • The reason the above is needed is a pointer (your solution) does not allocate any space in memory for anything. This solution allocates memory and then passes the address of this memory (creating a pointer) to the function for it to use. – Michael Dorgan Apr 19 '13 at 23:41