"Tcl_ServiceModeHook: Notifier not initialized" error with FLTK and c++11 thread - tcl

I'm trying to get FLTK running in my c++ project. I'm creating an OMNeT++ simulation and would like to use the GUI like an emulated host, that is, I can make a send button and the host attached to the GUI will queue up a message to send in the simulation.
The issue I'm having is that, when the FLTK window runs, it waits for a response which causes the whole simulation to freeze until I close the window. My solution was to run the GUI in a separate thread. However, now I'm getting this error:
Tcl_ServiceModeHook: Notifier not initialized
Below is the code for the class containing the thread and the GUI.
#include <Enumerations.H>
#include <Fl.H>
#include <Fl_Box.H>
#include <Fl_Widget.H>
#include <Fl_Window.H>
#include <GUI.h>
void GUI::callThread() {
t = std::thread(&GUI::openWindow, this);
}
int GUI::openWindow() {
Fl_Window *window = new Fl_Window(300,180);
Fl_Box *box = new Fl_Box(20,40,260,100,"Hello, World!");
box->box(FL_UP_BOX);
box->labelsize(36);
box->labelfont(FL_BOLD+FL_ITALIC);
box->labeltype(FL_SHADOW_LABEL);
window->end();
window->show();
return Fl::run();
}
All I do to call it is:
GUI *g = new GUI();
g->callThread();
If I instead just call openWindow() directly, The window opens fine but it waits for an action so the rest of the simulation is not able to continue.
I'm trying to make this cross-platform (Has to work on Mac and Windows). I tried Qt (too complicated to set up) and wxWidgets (make errors on my computer) so FLTK seemed like one of the next best choices.
If anyone knows how to fix this error or has any opinions on a better way to set up my GUI, I'm all ears. This just seemed like the only thing I could do.

OMNeT++ 5.0 and up has Qtenv, which is a QT based runtime (Tkenv is now deprecated), so the whole QT setup, dependency etc. is done for you by OMNeT++. Qtenv will be the default runtime in OMNeT++ 5.1
It's not really clear what you are trying to achieve. If you want to insert events into the event queue whenever you press a button, you definitely have to use a separate thread, but you have to clarify how the simulation and wall clock time is related to each other. If they must be synced, you need to implement a real-time scheduler that syncs the simulation time with the wall clock time. The sockets example in OMNeT++ does this and behaves similarly what you describe except that the external event inserted into event queue is coming from a socket (where you can attach with a browser) isntead of coming from a GUI.
If you don't care about synchronization i.e. you want to use this for some kind of demonstration, then you don't have to use a real-time scheduler, but you should be aware the OMNeT++ is inherently single threaded and your GUI is running in a separate thread, so you have to sync those too. i.e. It's absolutely forbidden to access ANYTHING that OMNET is using from the GUI thread (or bad things will happen).
And the poor man's solution: if you just want to trigger an event (i.e. you really need only a button). Write an application in the host which regularly polls a volatile bool parameter. If it finds the parameter "true" it sends the event and then sets the parameter to "false". Now how you trigger an event? You can browse the parameters of the given node in the property inspector (lower left panel in runtime) and you can change it's value by hand back to true, then allow the simulation to continue. On the next poll, the host's app will detect again that the parameter is true and do the sending again. Obviously it's limited, but it gives a limited interaction without writing too much code (and all the multithreading issues are handled by the runtime itself)

Related

MFC application exception handling weird behaviour

I have a 64bit MFC application, in which I use crash reporting. The method is based on this article:
http://www.codeproject.com/KB/debug/XCrashReportPt1.aspx?display=Print
So I have a __try - __except wrapper frame around AfxWinMain.
Last year it seemed to work well (I mean on different operating systems and also with 32 bit and 64 bit application).
Let's take this exception providing code part:
void CMyDig::Foo()
{
std::vector<int> v;
int i = v.at(42);
}
When I write this code inside a dialog's message-handler, for example a button's clickhandler called Foo, then the exception is not caught in AfxWinMain.
If I write the same to a global function, it passes the exception to AfxWinMain.
Now we have this strange issue:
On some computers the dialog's case also generates the crash report(enters the __except block in afxwinmain), but others no. I managed to create a crash report on win7 64-bit, but the same program on win8, win8.1 and another win7 64bit doesn't make the report!
I compile with vs2010, and in code generation options, the "enable c++ exceptions" is set to EHsc (as last year, when it was working well).
Thanks for any ideas!
Attila
Wouldn't be easier to just define SetUnhandledExceptionFilter to create the crash report.
In this case you don't need such a __try / __except block
WndProcs have their own __try __except blocks and some of the exceptions are handled by themselves.
But anyhow. I never had problems with an exception filter.
My routines usually create a crash dump (minidump) and terminate. I always felt that this bare information is never enough for our technical stuff to find problems.

Run Perl in Browser with PerlTray

I am using perl tray from activestate and have a question. I am wanting to make some type of ui or way for a user to set "Settings" on my application. These settings can just be written / read from a text file that is stored on the users computer.
The part I am not understanding though is how to go about making a ui. The only thing i can think of is showing a local perl page that runs on their computer to write to the file. However, I'm not sure how i could get perl to run in the browser when only using perltray.
Any suggestions?
PerlTray is an odd duck. It has an implicit event loop that kicks in after you either fall off the end of your program or after your 1st call to exit(). This makes it incompatible with most other common GUI event loops or most mini-server techniques that operate in the same process & thread.
2 possibilities come to mind:
Most Likely you'll have success spawning a thread or process that creates a traditional perl GUI or a mini-server hosting your configuration web-app. I'd probably pick Tkx, but that's just my preference.
I have a suspicion that the Event Loop used by Win32::GUI may actually be compatible with the event loop in PerlTray, but some experimentation would be required to verify that. I generally avoid Win32::GUI because it's not platform independent, but if you're using PerlTray, you're tied to Windows anyway...

Preferred way to DB connection in iOS

I'm a beginner iOS developer, and I'm trying to build a CRM system to learn the different aspects of developing.
I have a question regarding the preferred way to connect to an external SQL-server. I'm using Karl Krafts' Obj-C MySQL Connector by the way.
Right now I init the Database-controller (which in turn creates, then idles the connection to the server) object in my app delegate (didFinishLaunchingWithOptions), and that gives me some unwanted side-effects.. The screen is black a long time at startup if connection to the DB is slow, and sometimes the app is "too fast" and the query is trying to execute before the connection has been fully established - resulting in an exception being thrown.
The behavior I want (and guess is the preferred) is that the GUI loads up first, and then the initialization of the DB-controller and connection is established in a background thread - updating the GUI when the data has been acquired.
How would I achieve this? I have tried a number of different ways i've come across in my research, dispatch_queues and initing it straight from the viewDidLoad etc, but none give me the desired "GUI then data"-effect.
Also, would it be preferred to have an idling connection during the session of the program - or should each query 'connect - do its thing - disconnect'?
Regards, Christopher
Commandment One: don't do networking on the main thread - it's reserved for the UI. Else your app will have a laggy and frozen UI.
Commandment Two: instead of a lot of sequential synchronous calls, use asynchronous calls (GCD, background threads, etc.), events and callbacks. Cocoa (Touch) is designed with this in mind, so it's easy to do.
Commandment Three: if you launch something automatically, let it be launched when the app is fully ready. Let the call to the web service be the last one in application:didFinishLaunchingWithOptions:. Even better, let the user have the possibility to initiate the login via a user action, i. e. by pressing a "Login" button.
Commandment Four: read the first three Commandment again and keep them in mind. Practice them until you know them well.

What's debug section in IDA Pro?

I try to analyze a dll file with my poor assembly skills, so forgive me if I couldn't achieve something very trivial. My problem is that, while debugging the application, I find the code I'm looking for only in debug session, after I stop the debugger, the address is gone. The dll doesn't look to be obfuscated, as many of the code is readable. Take a look at the screenshot. The code I'm looking for is located at address 07D1EBBF in debug376 section. BTW, where did I get this debug376 section?
So my question is, How can I find this function while not debugging?
Thanks
UPDATE
Ok, as I said, as soon as I stop the debugger, the code is vanished. I can't even find it via sequence of bytes (but I can in debug mode). When I start the debugger, the code is not disassembled imediately, I should add a hardware breakpoint at that place and only when the breakpoint will be hit, IDA will show disassembled code. take a look at this screenshot
You see the line of code I'm interested in, which is not visible if the program is not running in debug mode. I'm not sure, but I think it's something like unpacking the code at runtime, which is not visible at design time.
Anyway, any help would be appreciated. I want to know why that code is hidden, until breakpoint hit (it's shown as "db 8Bh" etc) and how to find that address without debugging if possible. BTW, could this be a code from a different module (dll)?
Thanks
UPDATE 2
I found out that debug376 is a segment created at runtime. So simple question: how can I find out where this segment came from :)
So you see the code in the Debugger Window once your program is running and as you seem not to find the verry same opcodes in the raw Hex-Dump once it's not running any more?
What might help you is taking a Memory Snapshot. Pause the program's execution near the instructions you're interested in to make sure they are there, then choose "Take memory snapshot" from the "Debugger" Menu. IDA will then ask you wether to copy only the Data found at the segments that are defined as "loder segments" (those the PE loader creates from the predefined table) or "all segments" that seem to currently belong to the debugged program (including such that might have been created by an unpacking routine, decryptor, whatever). Go for "All segments" and you should be fine seeing memory contents including your debug segments (a segment
created or recognized while debugging) in IDA when not debugging the application.
You can view the list of segements at any time by pressing Shift+F7 or by clicking "Segments" from View > Open subviews.
Keep in mind that the programm your trying to analyze might choose to create the segment some other place the next time it is loaded to make it harder to understand for you what's going on.
UPDATE to match your second Question
When a program is unpacking data from somewhere, it will have to copy stuff somewhere. Windows is a virtual machine that nowadays get's real nasty at you when trying to execute or write code at locations that you're not allowed to. So any program, as long as we're under windows will somehow
Register a Bunch of new memory or overwrite memory it already owns. This is usually done by calling something like malloc or so [Your code looks as if it could have been a verry pointer-intensive language... VB perhaps or something object oriented] it mostly boils down to a call to VirtualAlloc or VirtualAllocEx from Windows's kernel32.dll, see http://msdn.microsoft.com/en-us/library/windows/desktop/aa366887(v=vs.85).aspx for more detail on it's calling convention.
Perhaps set up Windows Exception handling on that and mark the memory range als executable if it wasn't already when calling VirtualAlloc. This would be done by calling VirtualProtect, again from kernel32.dll. See http://msdn.microsoft.com/en-us/library/windows/desktop/aa366898(v=vs.85).aspx and http://msdn.microsoft.com/en-us/library/windows/desktop/aa366786(v=vs.85).aspx for more info on that.
So now, you should take a step trough the programm, starting at its default Entrypoint (OEP) and look for calls tho one of those functions, possibly with the memory protection set to PAGE_EXECUTE or a descendant. After that will possibly come some sort of loop decrypting the memory contents, copying them to their new location. You might want to just step over it, depending on what your interest in the program is by justr placing the cursor after the loop (thick blue line in IDA usually) and clicking "Run to Cursor" from the menu that appears upon right clicking the assembler code.
If that fails, just try placing a Hardware Breakpoint on kernel32.dll's VirtualAlloc and see if you get anything interestin when stepping into the return statement so you end up wherever the execution chain will take you after the Alloc or Protect call.
You need to find the Relative Virtual Address of that code, this will allow you to find it again regardless of the load address (pretty handy with almost all systems using ASLR these days). the RVA is generally calculated as virtual address - base load address = RVA, however, you might also need to account for the section base as well.
The alternative is to use IDA's rebasing tool to rebase the dll to the same address everytime.

What is an efficient way for logging in an existing system

I have the following in my system:
4 File folders
5 Applications that do some processing on files in the folders and then move files to the next folder (processing: read files, update db..)
The process is defined by Stages: 1,2,3,4,5.
As the files are moved along, the Stage field within them is updated to the next Stage.
Sometimes there are exceptions in the system, not necessarily exception in code but exception in the process.
For instance, there is an error in transmitting the file to the next folder. In this case the stage is not updated and an record is written in the DB for this file.
What I want to do, what is the best approach?
I want to plug a utility of some sort or add code to the applications that will capture any exceptions in the process. Like if a file was not moved, I want to know what stage and why. This will help in figuring out the break down in the process.
I need something that will provide the overall health of the process.
Now sure how to go about doing this from an architectural point of view.
The scheduler? Well that might knock the idea out anyway.
Exit code is still up and running from dos days.
it's a property of the Application Class (0 the default) is success
So from your app you'd detect an error and set ApplicationExitCode to some meaning number like 1703 (boo hoo)
Application.ShutDown(1703);// is the .net4 way
However seeing as presumably the scheduler is just running the app, you'd have to script it all up. Might as well just write a common logging dll and add it to each app as mess about with that, especially if you want the same behaviour if it's run from outside the scheduler.
Another option would be delegating. ie you write an app that runs the app (passed in as a command line parameter) and logs the result (via exit code for instance) and then change scheduler items to call that with the requisite parameter.