Any ideas on how to track down this error would be appreciated.
I have some c code that runs in two or more processes. The first process listens in on a message queue and saves the resulting struct to a database. The remaining processes query one or more serial devices and pass this information through the message queue to the first process to be stored in the database.
It all works great except the following. One of the structs I am using contains a float. This struct gets sent through the queue and decoded correctly however when binding the value using sqlite3_bind_double() the resulting value in the database is 0. Placing a printf() statement around the sqlite3_bind_double() statement causes the code to work and place the correct value in the database.
But even more interesting is if I remove the printf() statement and compile the program with gcc the code works.
Any help would be great. Thanks in advance.
Code:
int
add_inverter_stat(sqlite3 *db_conn, struct inverter_stat const *istat
,int *sqlite3_err)
{
sqlite3_stmt *stmt = NULL;
*sqlite3_err = sqlite3_prepare_v2(db_conn, SQL_INSERT_INVERTER_STAT, -1
,&stmt, NULL);
*sqlite3_err = sqlite3_bind_int(stmt, 1, istat->stat_id);
*sqlite3_err = sqlite3_bind_text(stmt, 2, istat->serial_no, -1, NULL);
*sqlite3_err = sqlite3_bind_int64(stmt, 3, istat->time_taken);
*sqlite3_err = sqlite3_bind_double(stmt, 4, (double)istat->value);
*sqlite3_err = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return 1;
}
That's the sort of thing I would expect from something trying to read a four-byte float as an eight-byte double, with the other four bytes being ... well, whatever. This really shouldn't work, but if you put an explicit cast (double)flt_var as the third parameter, does that help?
Related
I am using the jansson library for a C project.
I have some problem understanding how to use the decref. Shall it be used after each new json_t parameter or not? As I understand jansson will borrow references to make this simpler.
If I run this program and check the values of a_id and a_test they are the same. I expected error or null for a_test.
I tried the same idea but then I added decref for json_acc and json_param but it crashed before I could even read the 1:th value. I was assuming a crash but not until a_test.
This is part of a bigger project but I try to add an example to show the essentials.
API side:
json_t* parObj;
void loadFile(char* path)
{
json_error_t error;
parObj = json_load_file(path, 0, &error);
}
int getAccountId(char* id)
{
json_t* json_acc = json_object_get(parObj, "accounts");
json_t* json_param = json_object_get(json_acc, id);
return json_integer_value(json_param);
}
void cleanJson()
{
json_decref(parObj);
}
Caller side:
loadFile("/home/jacob/accountDump.json");
int a_id = getAccountId("10");
cleanJson();
int a_test = getAccountId("10");
I did misunderstood how it is supposed to work, I assumed that decref would also set the memory to zero.
The API will remove the references and make it a free memory but as long as no one writes there or memset it to zero and the pointer is not set to null I can still read the values from that pointer.
I'm new to OpenWrt. I am using iwinfo lib to scan wifi. It was working fine until I scan multiple time in my code. At first I think it is because of I am doing wrong using this library, so I tried to modified the cli program from iwinfo library and make it scan twice. Here is the main function of the cli code, which will work only once. The print_scanlist function implementation is same it in the cli program.
int main() {
const struct iwinfo_ops *iw = iwinfo_backend("wlan0");
print_scanlist(iw, "wlan0");
print_scanlist(iw, "wlan0");
iwinfo_finish();
return 0;
}
At first I think it was because the hardware does not allowed to scan in short amount of time. But if I run the program second time it still work for the first function. Now I have no idea why is this happenging, does anyone know why?
I`m almost sure one of these options (maybe even both) will work:
int main() {
const struct iwinfo_ops *iw = iwinfo_backend("wlan0");
print_scanlist(iw, "wlan0");
iwinfo_finish();
print_scanlist(iw, "wlan0");
iwinfo_finish();
return 0;
}
int main() {
print_scanlist(iwinfo_backend("wlan0"), "wlan0");
iwinfo_finish();
print_scanlist(iwinfo_backend("wlan0"), "wlan0");
iwinfo_finish();
return 0;
}
It`s important to understand that iw is not a handle of some sort, it is just a pointer to a list of functions stored in the backend library. The list is hard-coded and it cannot be modified at runtime.
Since I am using nl80211 as backend, I dug into the source code of iwinfo. I found that it is using WPA supplicant. In the scan function, it called a function name nl80211_get_scanlist_wpactl and it's purpose is to connect to the WPA supplicant and ask it to scan and get its result.
The steps in iwinfo are
send(sock, "ATTACH", 6, 0);
send(sock, "SCAN", 4, 0);
send(sock, "SCAN_RESULTS", 12, 0);
The problem is iwinfo forgot to call a DETACH therefore the next time you wont able to do anything. So after I add a send(sock, "DETACH", 6, 0), I got it to work. Thanks everybody
I am understanding and implementing the concept of threading in my application. Since now things are going good. But I have few questions still unanswered and they are making me slow now. I would appreciate if anyone replies to even any of them
In Createthread(), can we only take 1 argument? as I have seen in MSDN website and all other examples that I have seen I saw only 1 argument, LPVOID.
The other thing is , what does the return value DWORD WINAPI means as a return value? Can we have only DWORD , int or any other return type. I suppose it has something to do with HANDLE (may be)
I want to use the array of the thread, hence I learn the array to functions, and (as I have understood) threads are itself just a function called by CreateThread() routine, hence I tried to implement that concept there but could not because of the return type DWORD WINAPI was not allowing me to do so?
I have one single thread for saving files, now I want its array so that I can save multiple files at the same time (not exaclty the same starting time, but sort of parallel file saving). How can I do that?
Thanks
Shan
Indeed, you can only take one argument, of type void * (LPVOID).
However, since it can point to anything, it can point to a struct
or object (usually allocated on the heap for lifetime reasons).
WINAPI is not part of the return value, it's the function's calling
convention. The function must return a DWORD or anything that fit
in it. It must NOT return a pointer, because a pointer can't fit a
DWORD in Win64.
I don't understand, please elaborate what you're
trying to do.
Usually for this you need a single thread function,
passed several times to CreateThread() with a different argument
each time. Don't forget to keep the thread handles (which you'll
likely save in an array) until you stop needing them and close them
with CloseHandle().
for the point number three I guess I understood and will try differently. I was using
DWORD WINAPI save_uwpi_file0( LPVOID )
{
while(1)
{
if(release == 1 && flag_oper1 == 1)
{
int w_cnt = 0; FILE *opfile;
char fname[30] = "txt_file0.txt";
//opening file for write
opfile = fopen(fname , "w");
printf("assigning memory for file 1 \n");
ssint *Lmem = (ssint *)malloc( sizeof(ssint)*size_of_memory);
memcpy(Lmem, pInDMA, sizeof(ssint)*size_of_memory);
release = 0;
printf("relseaing for second file saving\n");
for( int nbr = 0; nbr < size_of_memory; nbr++){
fprintf(opfile , "%hi\n", Lmem[nbr] );
}
printf("aligned free 1\n");
free(Lmem);
fclose(opfile);
printf("File saved 1\n\n");
return 1;
} //if statement ends
}
}
and I was using following to make the pointer to (thread) function
DWORD WINAPI (* save_uwpi_file0)(LPVOID);
I guess I should try something like
DWORD (* save_uwpi_file0)(LPVOID);
I will do it and post the result here
I am having some issues with lb_getcursel and what it returns (if it does even return anything)
heres my message handler...
case IDT_TESTLIST1:
if(HIWORD(wParam) == LBN_DBLCLK) {
int ret = 0;
double TimeOut = 60.0;
int Lng = 1;
unsigned char Param[255] = {0};
unsigned char Port1 = port1;
int iCurSel = SendDlgItemMessage(hwnd,IDT_TESTLIST1,LB_GETCURSEL,0.0);
ret = PSB30_Open(Port1,16);
ret = PSB30_SendOrder(Port1,test1[iCurSel].testNumber, &Param[0],&Lng,&TimeOut);
ret = PSB30_Close(Port1);
}
break;
I am using Visual Studio 2010 and whenever i run the program iCurSel doesn't look like it even gets assigned a value, defaults to 0, when i step into the case statement, not all variables are visible in the autos section, when i add a watch to iCurSel i get a CXX0017: Error message.
hwnd is the handle to my main window and is correct
any help would be appreciated
Cheers
i find it funny that none of my variables in the message are showing anything by hovering over them
That's because they don't exist. Your program cannot compile, it has an error. SendDlgItemMessage() takes 5 arguments, you pass 4. The last one got morphed into a floating point value by a typo.
Clearly you'll need to pay attention to compile error messages. And change a setting so this cannot happen again. Tools + Options, Projects and Solution, Build and Run. Change the "On Run, when build or deployment error occurs" setting to "Do not launch".
I am having errors when I try to use pthread_create. I understand that my use of argsRight->thread_id / argsLeft->thread_id and NULL are not correct, but I am unsure how else to make a reference to the thread id. It requires a pointer, but it seems like every way I tried (&, *), the GCC compiler would not accept.
Also, is there any reason it will not accept my use of NULL? I can't see any reason that would be wrong, but GCC says my use of the void function is invalid.
Can anyone shed some light on how to properly set up a call to pthread_create? I have included parts from my method where I am using the pthread_create function.
void pthreads_ms(struct ms_args* args)
{
int left_end = (args->end + args->start) / 2;
int right_start = left_end + 1;
int rc1, rc2;
// Create left side struct
struct ms_args* argsLeft;
argsLeft = malloc(sizeof(args));
argsLeft->thread_id = (2 * args->thread_id + 1);
argsLeft->start = args->start;
argsLeft->end = left_end;
argsLeft->array = args->array;
// Same methodology as above to create the right side
if (args->start != args->end)
{
// Print the thread id number, and start and end places
printf("[%d] start %d end %d", args->thread_id, args->start, args->end);
// Sort Left Side
rc1 = pthread_create(argsLeft->thread_id, NULL, pthreads_ms(argsLeft), argsLeft); //problem line here
//Sort right side
rc2 = pthread_create(argsRight->thread_id, NULL, pthreads_ms(argsRight), argsRight); //problem line here
}
It is not your application, it's pthread_create() will fill thread_id field. So, first of all, struct ms_args's field should be of type pthread_t and you should pass a pointer to that field:
pthread_create(&argsLeft->thread_id, ...
According to pthread_create the proper call should be
rc1 = pthread_create(&(argsLeft->thread_id), NULL, &pthreads_ms, argsLeft);
Same goes for right side.
The definition of pthread_ms() should include a return value
void *pthreads_ms(struct ms_args* args) { ... }
Besides that, your code looks pretty dangerous to me, since it creates recursively two threads for every existing one. Depending on your input, this might build a large tree of threads, which could bring your system to a halt.