martinling / libserialport

Unofficial personal repository for libserialport - see git://sigrok.org/libserialport for official repo
http://sigrok.org/wiki/Libserialport
GNU Lesser General Public License v3.0
65 stars 34 forks source link

Segmentation fault errors with port enumeration #7

Closed kezl closed 9 years ago

kezl commented 9 years ago

Hello,

Using the code below I am getting a segfault error and I am starting to think my allocation is wrong somehow but I can't see where, am I missing something? My operating system is debian jessie and I am using a copy of the library built on the same machine. I have tried calling the resultant executable as normal and root user and get the same error so I don't think it is permissions related.

Best regards, kezl

/*
gcc `pkg-config  --cflags glib-2.0` -g -Wall -O3 -I/path-to/libserialport/ -o test2.o -c test2.c
gcc test2.o linux.o linux_termios.o serialport.o `pkg-config --libs glib-2.0` -ludev -o test2
*/

#include <glib.h>
#include <stdio.h>
#include <malloc.h>

#include <glib/gi18n.h>
#include <unistd.h>
#include <libudev.h>

#include "libserialport.h"

// Debug
// export LIBSERIALPORT_DEBUG=1
// ./test2

int main() {

    struct sp_port **ports = NULL;    

    //struct sp_port *port = NULL;
    int result = SP_OK;

    if (!(ports = malloc(sizeof(struct sp_port **)))) {
        printf("Port list malloc failed\n");
    } else {
        printf("Port list malloc succeeded\n");

    }

    result = sp_list_ports(&ports);
    //result = sp_list_ports((struct sp_port ***)&ports);

    if(result == SP_OK ) {
        printf("Operation completed successfully. \n");
    } else if(result == SP_ERR_ARG) {
        printf("Invalid arguments were passed to the function.\n");
    } else if(result == SP_ERR_FAIL) {
        printf("A system error occured while executing the operation.");
    } else if(result == SP_ERR_MEM ) {
        printf("A memory allocation failed while executing the operation.");
    } else if(result == SP_ERR_SUPP) {
        printf("The requested operation is not supported by this system or device. ");
    }

    //~ int i = 0;
    //~ while (ports[i] != NULL) {
        //~ port = ports[i];
        //~ const char* name = sp_get_port_name(port);
        //~ printf("%s\n", name);
        //~ i++;
    //~ }

    sp_free_port_list(ports);
    return 0;
}
martinling commented 9 years ago

Your malloc call is unnecessary. Having declared struct sp_port **ports, you just pass &ports to sp_list_ports() and it will set ports to point to the array it allocates.

This version of your code runs fine for me:

/* gcc test2.c -lserialport -o test2 */

#include <stdio.h>
#include <libserialport.h>

int main() {

    struct sp_port **ports = NULL;
    struct sp_port *port = NULL;

    int result = SP_OK;

    result = sp_list_ports(&ports);

    if(result == SP_OK ) { 
        printf("Operation completed successfully. \n");
    } else if(result == SP_ERR_ARG) {
        printf("Invalid arguments were passed to the function.\n");
    } else if(result == SP_ERR_FAIL) {
        printf("A system error occured while executing the operation.");
    } else if(result == SP_ERR_MEM ) { 
        printf("A memory allocation failed while executing the operation.");
    } else if(result == SP_ERR_SUPP) {
        printf("The requested operation is not supported by this system or device. ");
    }   

    int i = 0;
    while (ports[i] != NULL) {
        port = ports[i];
        const char* name = sp_get_port_name(port);
        printf("%s\n", name);
        i++;
    }   

    sp_free_port_list(ports);
    return 0;
}                    
kezl commented 9 years ago

On 16/03/15 10:14, Martin Ling wrote:

Closed #7 https://github.com/martinling/libserialport/issues/7.

— Reply to this email directly or view it on GitHub https://github.com/martinling/libserialport/issues/7#event-255290991.

Still no joy for me, using your example after a clean install I can build but I get the following error when the program is run: ./test2: error while loading shared libraries: libserialport.so.0: cannot open shared object file: No such file or directory

It seems odd that it looks for a shared library.

When I tried building with with libserialport.a in the same directory: gcc -I/path/to/source/libserialport/ -o test3 test3.c ./libserialport.a it worked as expected so I will do it that way.

Best regards, kezl

martinling commented 9 years ago

The way you have been building using files directly from the libserialport tree is very odd.

The normal way to use the library would be to run 'make install' in the libserialport source directory, which will install the headers and library in /usr/local.

You can then build your own program elsewhere, including <libserialport.h> and building with -lserialport. The result will be a dynamically linked executable which finds and uses the installed copy of the library when it runs.

If you want a static executable, i.e. one that does not depend on the presence of a shared library, then just build with the -static flag and the necessary parts of the library will be incorporated directly into your executable.

kezl commented 9 years ago

The libserialport.so.0 error comes after I run make install using the usual method as per the readme and then build the example you gave, so building against the installed copy doesn't seem to work for me.

If I build a static executable from components in the same directory it works , so I am starting to think maybe something is corrupt on my path so I will look there, thankyou for your help.

martinling commented 9 years ago

You may just need to run sudo ldconfig to update things after having installed the library.

kezl commented 9 years ago

On 16/03/15 12:55, Martin Ling wrote:

|sudo ldconfig| Yes, that fixed it :) thank-you again!