Issues with LB_GETCURSEL - c

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".

Related

Gurobi model Heap has been corrupted

I am using Gurobi (9.0.3) C API to build a LP model and change the constraints to get different objective value. However, sometimes when I run the code, there is error about "triggered the breakpoint" and "heap has been corrupted" where I marked below. I noticed that the heap will be corrupted if memory is wrongly assigned, but I am not sure what to change with the model.
There are three functions in my code, first I will use constructLPmodel() to build original model and get the result using getSol_LP(). After that, updateLP() will be called several (100+) times to change constraints and get the result.
GRBenv *LPenv = NULL;
GRBmodel *LPmodel = NULL;
double LPobjectiveValue;
double constructLPmodel()
{
GRBloadenv(&LPenv, "LP.log");
GRBnewmodel(LPenv, &LPmodel, "LP", 0, NULL, NULL, NULL, NULL, NULL); //breakpoint sometimes lies here
// create variables and constraints
getSol_LP();
return LPobjectiveValue;
}
double updateLP()
{
int error = 0;
error = GRBupdatemodel(LPmodel); // breakpoint sometimes lies here
if (error) printf("Error in updating model\n");
getSol_LP();
return LPobjectiveValue;
}
void getSol_LP()
{
int error = 0;
double objVal;
GRBoptimize(LPmodel); // breakpoint sometimes lies here
GRBwrite(LPmodel, "LP.lp"); // breakpoint sometimes lies here
GRBwrite(LPmodel, "LP.sol");
error = GRBgetdblattr(LPmodel, GRB_DBL_ATTR_OBJVAL, &objVal);
if (error) goto QUIT;
LPobjectiveValue = objVal;
}
This sounds like a hardware issue. Maybe something is wrong with your memory. Have you tried reproducing this on another machine? Do you have a stack trace of the error, showing where it happens?
Without a (minimal) reproducible example, there is not much we can do to help you.

Monitor flashing when running a Windows SendInput API

Well, I certainly should go to python since I did several functions of this type, keyboard event and mouse event, but decide to try to learn the windows api.
My goal is to know when button 1 of the mouse is pressed.
I created this file in a very beginner way, it returns in mouseData only 0.
The curious thing is that whenever I run it, it flashes my monitor at short intervals in blinks, but between 1 second with it off. Very strange that, execution is not viable.
Could someone help me understand and try to execute to see if it is only here.
Code:
int main()
{
DWORD mouseData = 0;
MOUSEINPUT tagMouse;
tagMouse.dx = 0;
tagMouse.dy = 0;
tagMouse.mouseData = mouseData;
tagMouse.dwFlags = MOUSEEVENTF_XDOWN;
tagMouse.dwExtraInfo = 0;
INPUT tagInput;
tagInput.type = INPUT_MOUSE;
tagInput.mi = tagMouse;
while (true) {
if (GetAsyncKeyState(VK_DELETE)) break;
SendInput(1, &tagInput, sizeof(INPUT));
printf("KEYWORD: %d\n", mouseData);
Sleep(500);
}
system("pause");
return 0;
}
I can reproduce your reported 'symptoms' - and the effect is really brutal!
Now, while I cannot offer a full explanation, I can offer a fix! You have an uninitialized field in your tagMouse structure (the time member, which is a time-stamp used by the system). Setting this to zero (which tells the system to generate its own time-stamp) fixes the problem. So, just add this line to your other initializer statements:
//...
tagMouse.dwExtraInfo = 0;
tagMouse.time = 0; // Adding this line fixes it!
//...
Note: I, too, would appreciate a fuller explanation; however, an uninitialized field, to me, smells like undefined behaviour! I have tried a variety of other values (i.e. not zero) for the time field but haven't yet found one that works.
The discussion here on devblogs may help. This quote seems relevant:
And who knows what sort of havoc that will create if a program checks
the timestamps and notices that they are either from the future or
have traveled back in time.

VS2010, scanf, strange behaviour

I'm converting some source from VC6 to VS2010. The code is written in C++/CLI and it is an MFC application. It includes a line:
BYTE mybyte;
sscanf(source, "%x", &mybyte);
Which is fine for VC6 (for more than 15 years) but causing problems in VS2010 so I created some test code.
void test_WORD_scanf()
{
char *source = "0xaa";
char *format = "%x";
int result = 0;
try
{
WORD pre = -1;
WORD target = -1;
WORD post = -1;
printf("Test (pre scan): stack: pre=%04x, target=%04x, post=%04x, sourse='%s', format='%s'\n", pre, target, post, source, format);
result = sscanf(source, format, &target);
printf("Test (post scan): stack: pre=%04x, target=%04x, post=%04x, sourse='%s', format='%s'\n", pre, target, post, source, format);
printf("result=%x", result);
// modification suggested by Werner Henze.
printf("&pre=%x sizeof(pre)=%x, &target=%x, sizeof(target)=%x, &post=%x, sizeof(post)=%d\n", &pre, sizeof(pre), &target, sizeof(target), &post, sizeof(post));
}
catch (...)
{
printf("Exception: Bad luck!\n");
}
}
Building this (in DEBUG mode) is no problem. Running it gives strange results that I cannot explain. First, I get the output from the two printf statemens as expected. Then a get a run time waring, which is the unexpected bit for me.
Test (pre scan): stack: pre=ffff, target=ffff, post=ffff, source='0xaa', format='%x'
Test (post scan): stack: pre=ffff, target=00aa, post=ffff, source='0xaa', format='%x'
result=1
Run-Time Check Failure #2 - Stack around the variable 'target' was corrupted.
Using the debugger I found out that the run time check failure is triggered on returning from the function. Does anybody know where the run time check failure comes from? I used Google but can't find any suggestion for this.
In the actual code it is not a WORD that is used in sscanf but a BYTE (and I have a BYTE version of the test function). This caused actual stack corruptions with the "%x" format (overwriting variable pre with 0) while using "%hx" (what I expect to be the correct format) is still causing some problems in overwriting the lower byte of variable prev.
Any suggestion is welcome.
Note: I edited the example code to include the return result from sscanf()
Kind regards,
Andre Steenveld.
sscanf with %x writes an int. If you provide the address of a BYTE or a WORD then you get a buffer overflow/stack overwrite. %hx will write a short int.
The solution is to have an int variable, let sscanf write to that and then set your WORD or BYTE variable to the read value.
int x;
sscanf("%x", "0xaa", x);
BYTE b = (BYTE)x;
BTW, for your test and the message
Run-Time Check Failure #2 - Stack around the variable 'target' was corrupted.
you should also print out the addresses of the variables and you'll probably see that the compiler added some padding/security check space between the variables pre/target/post.

Error Handling in C

Hello there and thank you for stopping by.
The solution has been found by making use of proprietary code. See bottom for used code.
Thanks everybody for input and help!
I'm fairly new to C programming (meaning I coded some very basic stuff). At the moment, I'm writing a script in C language. I've tried searching the site, but no examples seem applicable to my case, or simply do not work.
What it does is basically opening a file (or a batch of multiple files), telling the master program to plot a graph of selected string (the open_curve() is a program specific function).
Now my problem is that one of the strings (lets say string2 in the code below) is not always present. When it's missing, the script produces an error and quits. How to stop this happening? VBA has an option many people loathe, but I regard it to be quite useful in this situation: On Error resume next. I know it might be missing and it is not of importance in this case. When it's there I need it. When it's not, too bad... so if no errors pop up and the value is missing, that is perfectly fine.
Question is, how can I get a similar functionality in C?
#define NMAX 10 // max. Anzahl Dateien
string n[NMAX]; // Global wegen Stack!
void main(void)
{
string *q;
int i,nmax=NMAX;
nmax=GetOpenFileName(n, nmax, "Daten-Dateien (*.DAT)", "*.DAT");
for (i=0, q=n; i<nmax; i++,q++)
{
ql.open_file(.file=*q);
close_curve();
open_curve(.name="\"String1\"");
open_curve(.name="\"String2\"");
open_curve(.name="\"String3\"");
printf("File %2d: %s\n",i,*q);
}
getch();
}
The prototype:
int [ql.]open_curve(.name = string[, .no = int,
.color = int, .lthick = int,
.linetype = int,
.marker = int, .drawall = int,
.markersize = float,
.bars = int, .ynull = float,
.ymin = float, .ymax = float,
.xmin = float, .xmax = float,
.yaxis = int, .xaxis = int,
.axis = int, .colsrc = int,
.xsignal = string, .csignal = string,
.index = string,
.xfrom = float, .xuntil = float,
.usemarker = int]);
The Code, with my problem solved:
Now returns empty when not found.
for (i=0, q=n; i<nmax; i++,q++)
{
ql.open_file(.file=*q);
close_curve();
open_curve(.name="\"String1\"");
Warninglevel = 2;
Errorlevel3 = 3; // Fehler nicht melden, weitermachen
err = open_curve(.name="\"String2\"");
Errorlevel3 = 0;
Warninglevel = 0;
open_curve(.name="\"String3\"");
printf("File %2d: %s\n",i,*q);
}
getch();
}
with the warning and error levels explained as following:
extern int Warninglevel - Warnings will...
0 - Produce an Error box
1 - be ignored
2 - be ignored
extern int Errorlevel1 - Errors will...
0 - Produce an Error box (popup) and cancel script
1 - Produce an Error box and continue script 2 - ignored and cancel script.
There is nothing in C that corresponds to On Error Resume Next.
It is possible to catch errors using signal handling, but it is not the preferred way to do error checking in C.
Instead, best practice is to code defensively. Any variable that is allowed to be NULL should be checked before it is used.
In your case the code could look like this:
char *name; // In reality
name = get_graph_name(); // Generate a name somehow, or null.
if (name != NULL) { // Only open the curve if we have a name.
open_curve(name);
}

gtk_container_get_children

When list of stations is printed in Preset rol list and you are at the begining or at the end of the list, you see many empty items.
I create list of about 20 stations and try to display them, go to the begining and to the end of the list.
I think the problem is in this piece of code:
static void add_button_clicked_cb(GtkWidget *widget, gpointer data)
{
preset *ps;
gchar *buffer;
GtkTreeIter iter = {0};
GtkAdjustment* v_scb;
GtkTreePath *path = NULL;
GList* menuitems;
GtkWidget *menuitem;
ps = malloc(sizeof(preset));
ps->title = g_strdup(_("unnamed"));
ps->freq = rint(gtk_adjustment_get_value(adj)) / STEPS;
settings.presets = g_list_append(settings.presets, (gpointer) ps);
buffer = g_strdup_printf("%.2f", ps->freq);
gtk_list_store_append(list_store, &iter);
gtk_list_store_set(list_store, &iter, 0, ps->title, 1, buffer, -1);
g_free(buffer);
gtk_tree_selection_unselect_all(selection);
v_scb = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(list_view));
gtk_adjustment_set_value(v_scb, gtk_adjustment_get_upper(v_scb));
if (main_visible) {
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(preset_combo), ps->title);
mom_ps = g_list_length(settings.presets) - 1;
preset_combo_set_item(mom_ps);
menuitems = gtk_container_get_children(GTK_CONTAINER(tray_menu));
menuitem = gtk_menu_item_new_with_label(ps->title);
gtk_menu_shell_insert(GTK_MENU_SHELL(tray_menu), menuitem, mom_ps);
g_signal_connect(G_OBJECT(menuitem), "activate", (GCallback)preset_menuitem_activate_cb, (gpointer)mom_ps);
gtk_widget_show(menuitem);
}
buffer = g_strdup_printf("%d", g_list_length(settings.presets) - 1);
path = gtk_tree_path_new_from_string(buffer);
g_free(buffer);
gtk_tree_view_set_cursor(GTK_TREE_VIEW(list_view), path, NULL, FALSE);
gtk_tree_path_free(path);
}
Could you please suggest me how can correct it?
Inside of the if (main_visible), you have:
menuitems = gtk_container_get_children(GTK_CONTAINER(tray_menu));
That is, you're setting the value of menuitems to the return value of that function call. However, none of the rest of your code actually does anything with menuitems. So, the compiler gives you that warning.
Fixing it might be as easy as simply getting rid of menuitems:
gtk_container_get_children(GTK_CONTAINER(tray_menu));
That would definitely get rid of the warning. But you have to think about it: Maybe you should be doing something with the return value of that function call, and the reason that you're not doing anything with it is that you forgot to (or whatever).
warning: variable 'menuitems' set but not used
Your compiler in layman's terms is saying the following:
Dude... youve created a class with a few attributes, one of which are called 'menuitems'. That's all well mate, but when you actually create an instance and assign all the variables, youve set 'menuitems' but youre never using that variable to do anything with it. its just sitting there doing nothing at all. So mate it's no problem at all as I can still run your program but im just warning you that the 'menuitems' variable is doing ** all as you're never using the variable itself. Just warning you bro :D other than that have a good day
So its just a warning to let you know you have a variable no being used for any specific reason other than storing a value in there but never accessing it to do something useful. So either comment it out since youre not actually doing anything with it.. or use it, or just enjoy reading the warning but it wont affect the way your program runs.
The warnings get displayed mainly to get the developer more aware of their own code and whats happening.

Resources