Re: Seg fault reading data from IOChannel
- From: klaus triendl <klaus triendl eu>
- To: gtkmm-list gnome org
- Subject: Re: Seg fault reading data from IOChannel
- Date: Tue, 03 Feb 2009 12:50:31 +0100
Víctor M. Palacio Tárrega schrieb:
> You are absolutely right. buff remains from another workaround and
> should be eliminated.
>
> About data, should be defined as char foo[<data_lenght>]; where I use
> only 1 or 4 as data_lenght.
> Below are included the calls to read_data() function I'm using.
>
> I'm doing something nasty with data conversion, or is just fine for you?
>
> Thanks a lot,
>
> Code:
> int value;
> char data;
> //.........................................
> ioc_unix.read_data(&ID,1);
> std::cout<<"command received: " << (int)ID << std::endl;
> //Get value
> switch(ID)
> {
> case 0: //speed
> //..........................
> break;
> case 1: //RPM
> if(ioc_unix.read_data((char*)value,4) !=-1){
> //......................................
Yes, you're doing something nasty with conversions, i.e. you convert
'value' with a C-style cast to a char pointer, which is absolutely
nasty, because depending on whether you've initialized 'value' it surely
contains an undefined integer value and is the cause for the
segmentation fault.
What you want to do is to convert the address of the 'value' variable to
a char pointer (note the address-of operator): (char*) &value;
Additionally I see two problematic issues with your code:
1) the size of a variable of type int is not 4 bytes on 64bit systems,
so your hard-coded length of 4 might cause problems
2) I didn't see the code where you implement sending the data, but you
have to consider the endianess (byte-order) of the system your
application is running on. If you send an integer (4 bytes) from a
big-endian system over the network to a little-endian system then the
integer value won't be the same because they interpret the 4 bytes
differently. Better you use functions like htonl(), ntohl(), etc...
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]