SCIP 3.2.0 doesn't replace message handler - scip

I want the SCIP callable to print all messages to stderr (using it as a flatzinc solver). I've tried
SCIP_DECL_MESSAGEWARNING(printMsg) {
cerr << msg << flush;
}
...
SCIP_MESSAGEHDLR* pHndl=0;
SCIP_CALL ( SCIPmessagehdlrCreate ( &pHndl, FALSE, NULL, FALSE, printMsg, printMsg, printMsg, NULL, NULL) );
without effect...

Try alse making SCIP aware of your message handler:
SCIP_CALL( SCIPsetMessagehdlr(scip, pHndl) );
This will make SCIP also capture your message handler, so if you don't need it anymore, you can release it:
SCIP_CALL( SCIPmessagehdlrRelease(&pHndl) );
This will not overwrite the handler for error messages, as these might have to be printed when no SCIP is around yet. You can set a handler for this one via SCIPmessageSetErrorPrinting(). But they already go to stderr.

Related

How to do a non-recurring timeout in Gtk#?

I'm trying to do a thing a second in the future, but only once. The docs for GLib.Timeout.Add say that the "delegate is invoked repeatedly until it returns false", so I'm returning false and then I get an error from glib.
Example code:
using System;
class foo {
static void Main() {
Gtk.Application.Init();
uint t = GLib.Timeout.Add(1000, () => {
Console.WriteLine("returning false from timeout");
Gtk.Application.Quit();
return false;
});
Console.WriteLine("timeout added with source id = {0}", t);
Gtk.Application.Run();
}
}
Sample output:
$ mono foo.exe
timeout added with source id = 6
returning false from timeout
(foo:19030): GLib-CRITICAL **: Source ID 6 was not found when attempting to remove it
How do I stop my timeout from repeating after it times out without getting errors?
$ pkg-config --modversion gtk-sharp-3.0
2.99.3
Note that the error message apparently appears at program exit, and in my real program it does not seem to appear until the gc runs (I can get it immediately after the timeout if I manually call the gc and wait for finalizers).
Short answer, you can't. The timeout won't repeat because it has been properly removed. That's what the "error" is from because the gtk runtime is trying to dispose the timeout twice. This issue isn't just the C# gtk implementation, it's a bug in the C gtk+ library. There is a way to disable gtk from reporting errors, but I'm not sure how to do that.

How to change error behavior of Flex lexer?

I have built a lexer with Flex. There is one major problem though: Flex by default generates this error method:
void yyFlexLexer::LexerError( yyconst char msg[] )
{
std::cerr << msg << std::endl;
exit( YY_EXIT_FAILURE );
}
That means that on every syntax error my application just quits. Of course that's not what I want, since I need to call the lexer over and over even with faulty inputs.
Is there a way to change Flex's behavior on this?
Solved it! I just added a simple "#define YY_FATAL_ERROR" to the top of my .l file.
This will make sure that Flex won't call the LexerError-method and thus the application does not exit.

User triggered event in libevent

I am currently writing a multi-threaded application using libevent.
Some events are triggered by IO, but I need a couple of events that are triggered accross threads by the code itself, using event_active().
I have tried to write a simple program that shows where my problem is:
The event is created using event_new(), and the fd set to -1.
When calling event_add(), if a timeout struct is used, the event is later properly handled by event_base_dispatch.
If event_add(ev, NULL) is used instead, it returns 0 (apparently successful), but event_base_dispatch() returns 1 (which means no the event was not properly registered.)
This behavior can be tested using the following code and swapping the event_add lines:
#include <event2/event.h>
#include <unistd.h>
void cb_func (evutil_socket_t fd, short flags, void * _param) {
puts("Callback function called!");
}
void run_base_with_ticks(struct event_base *base)
{
struct timeval one_sec;
one_sec.tv_sec = 1;
one_sec.tv_usec = 0;
struct event * ev1;
ev1 = event_new(base, -1, EV_PERSIST, cb_func, NULL);
//int result = event_add(ev1, NULL);
int result = event_add(ev1, &one_sec);
printf("event_add result: %d\n",result);
while (1) {
result = event_base_dispatch(base);
if (result == 1) {
printf("Failed: event considered as not pending dispite successful event_add\n");
sleep(1);
} else {
puts("Tick");
}
}
}
int main () {
struct event_base *base = event_base_new();
run_base_with_ticks(base);
return 0;
}
Compilation: g++ sample.cc -levent
The thing is, I do not need the timeout, and do not want to use a n-years timeout as a workaround. So if this is not the right way to use user-triggered events, I would like to know how it is done.
Your approach is sound. In Libevent 2.0, you can use event_active() to activate an event from another thread. Just make sure that you use evthread_use_windows_threads() or evthread_use_pthreads() as appropriate beforehand, to tell Libevent to use the right threading library.
As for needing an extra event: in Libevent 2.0 and earlier, an event loop will exit immediately when there are no pending events added. Your best bet there is probably the timeout trick you discovered.
If you don't like that, you can use the internal "event_base_add_virtual" function to tell the event_base that it has a virtual event. This function isn't exported, though, so you'll have to say something like:
void event_base_add_virtual(struct event_base *);
// ...
base = event_base_new();
event_base_add_virtual(base); // keep it from exiting
That's a bit of a hack, though, and it uses an undocumented function, so you'd need to watch out in case it doesn't work with a later version of Libevent.
Finally, this method won't help you now, but there's a patch pending for future versions of Libevent (2.1 and later) to add a new flag to event_base_loop() to keep it from exiting when the loop is out of events. The patch is over on Github; it is mainly waiting for code review, and for a better name for the option.
I just got burned by this with libevent-2.0.21-stable. It is quite clearly a bug. I hope they fix it in a future release. In the meantime, updating the docs to warn us about it would be helpful.
The best workaround seems to be the fake timeout as described in the question.
#nickm, you didn't read the question. His example code uses event_new() like you described; there is a bug in libevent that causes it to fail when using a NULL timeout (but return 0 when you call event_add()).

IsProcessInJob with GetCurrentProcess aways true

(Visual Studio 2010 - C++)
Hello,
I'm trying to set a JOB to a process, but AssignProcessToJobObject returns ACCESS_DENIED and IsProcessInJob returns TRUE.
I call IsProcessInJob to a process immediately after call CreateProcess (Suspended) end tried call IsProcessInJob with my process (a few lines after main entry point) and it returns true.
void main()
{
BOOL bIsInJob;
IsProcessInJob( GetCurrentProcess(), NULL, &bIsInJob );
printf( "IsProcessInJob (me): %s\n", bIsInJob ? "true" : "false" );
// RET True ! inside and outside IDE
...
Someone saw it before?
Thanks for any help.
Sources:
Kill child process when parent process is killed
How do I automatically destroy child processes in Windows?
I found.
For some reason, my process was child of Explorer.exe then Explorer set a job to my process and the notepad (my child) inherits this job.
I could not find until see with ProcessExplorer. I can not saw my process in process list, when i find below Winlogon->Explorer, i understood.
Resolution: CREATE_BREAKAWAY_FROM_JOB
if (!CreateProcess(L"c:\\windows\\system32\\notepad.exe", L"", NULL, NULL, FALSE,
CREATE_BREAKAWAY_FROM_JOB, NULL, NULL, &startupInfo, &processInformation))
...
Thanks for your comments, patience and time.

How do I map Windows System error codes to boost::error_condition?

In the code below I would like to replace Windows WinSock error WSAEINTR=10004 with a generic boost system error code, but how do I map the code I found in the debugger, with the generic enums?
timeout_.expires_from_now(posix_time::seconds(15));
timeout_.async_wait( boost::bind(&cancel_socket_fn,this,_1) );
asio::streambuf rd_buf;
unsigned length = read_until( socket_, rd_buf,delimiter_string, error );
timeout_.cancel();
if(error)
{
// how do I make this portable?
if(error.value()==WSAEINTR) throw request_timeout_exception()
else throw my_asio_exception(error,"Unable to read header");
}
...
cancel_socket_fn(system::error_code e) { socket_.cancel(); }
if (error == boost::asio::error::interrupted)...
And I think here is a design error because if this code is called from the thread where io_service::run() (or similar) is called then cancel_socket_fn() will not be called until read_until() finishes. Or if they are in different threads then here are synchronization problems because timer methods are not thread-safe.