What I really want is, how do I know when each stage of C++ initialization is truly DONE?
There is static initialization where simple things get assigned. Then there's dynamic static initialization where more complicated statics get assigned, which is not defined across 'translation units'. This is kind of horrible, and there are not many easy ways to cope. I use namespaces in places to make an immediate assignment that happens on loading header files, but the flaw here is that this can then be overwritten in one of the initialization phases.
I can set the initialization to be a function which does 'the right thing' but it would be much easier if I could KNOW what 'phase' I am in somehow. So far as I can tell, this is not possible in any way at all, but I am hoping someone out there will have some good news.
I have worked around the issue that was causing this, which was code not used being unexpectedly linked in because it was in the project. It would still be nice to know the answer to this, but I am guessing the answer is 'there is no way to know for sure'.
I edited the question, I don't really want to know main is started per se.
I don't get what problem are you trying to solve.
When you build your application, the linker adds the startup code that is the first code to be executed when the OS loads your program in memory. This code will do all the initialization stuff and, when finished, will call your main() function.
If you are talking about replacing this code with your own, you should check the inner details of your compiler/linker (and be very sure you know what are you doing!!).
If your question is about having multiple processes and you need to know if one of the process has started, you should use a proper syncronization mechanism (that can be one of those provided by the underlying OS or one you make your own).
how about something like this:
bool is_started(bool set_started=false){
static bool flag = false;
if(set_started)
flag=true;
return flag;
}
main(){
is_started(true);
}
If your question is about windows, I know you can detect the messagepump from a process has started. This way you know for sure everything is initialized.
Of course this doesn't fly for *nix
if your running on windows, create a mutex after your done initializing. You can then WaitForSingleOject on that mutex to detect if your program is truly initialized.
Many applications do this to detect if startup was complete and what the other instance of the application is. This is especially true if you want only 1 instance of a program running.
Related
So, this question might sound quite strange, but let me explain:
I have written a code in C++ to analyze some data, and this data is separated in files. I pass names of these files (there are many of them) as an argument to the program.
Maybe I've made some mistake in my code or maybe the data in some of these files ain't "good" and that doesn't matter for now. But for some files the program stops and returns me a segfault message.
There is a way to even with the segfault jump to the next file in the argument list using only C++?
Maybe I could use some shell script to run it for each of the files and then, if I get a segfault it'll continue running one by one. But that is not what I want for now, if I can't solve this I will try this way.
Thank you guys in advance.
You can in fact install a signal handler for SIGSEGV (on most Unix like operating systems) that will be called when your program runs into a segfault, and if you wish you can handle that in whatever way seems appropriate (including ignoring it). Doing so is quite esoteric (java does it, but that's the only semi-sane usage I've ever seen) and rarely the right thing to do, and dealing with the fault correctly in such a handler is difficult as you can't really be sure what caused the fault and what the state of your program is after you return from the signal handler.
So, while you can "handle" SIGSEGV I'd advice you instead look into other ways of sanitizing your input data so that you instead avoid the segfault in the first place.
I have a C++ wxWidgets program, that uses threads. Therefore I needed to make sure the threads are safely accessed. In the wxThread documentation it is explained how to do so. They use friendclasses and wxCriticalSection to secure their threads. I did it like in the example and it works fine, though when I talked to a collegue about it, he told me friend classes are evil and should be avoided alltogether to avoid unexpected behaviour. I should use wxMutex instead.
Now I understand his point, because having my main as a friend class gives the thread class complete access to it. I understand, that this can cause problems, for example if I have similarly named variables or uninentionally access something else that I should not use outside of main. But I wonder if there are any advantages of this method. I mean, there has to be something, otherwise I can't understand why this way should be (as the only way) described in the wxWidgets documentation?
Could someone please enlighten me about the advantages and disadvantages of both methods? Or is there maybe a third way how I can access only the wxCriticalSection from the main without using friend or making it public? Thank you for your help.
Edit: As I realized that the critical part in my code is an artifact from long time ago, that is not neccessary anymore, the question is not vital for my programming. Nevertheless I think this is an interesting topic and would be usefull for future situations.
There are 2 completely orthogonal things in this question:
Using friend is indeed a bad idea and the example in wxThread documentation could (and should) be rewritten to avoid it. The simplest way to do it would be to reset the thread pointer in wxEVT_COMMAND_MYTHREAD_COMPLETED event handler.
wxCriticalSection is semantically exactly the same as wxMutex and, in fact, under non-Windows platforms they are just exactly the same. Under Windows, wxCriticalSection is just a more efficient kind of mutex (both classes directly correspond to their counterparts in Win32 API). It's perfectly fine and, in fact, preferred, to use wxCS instead of wxMutex if all you use it for is protecting some shared data. You need to use wxMutex with a wxCondition however.
I had a program written using Qt's shared library version (5.2.0) and it displayed fonts okay (presumably filling it's Database from /usr/share/fonts). (Not sure whether the Designer text is from the same Database as the programmed font handling.) But then I recompiled it under the open source static version (5.2.0), and the fonts went ugly.
I disrupted the path to the installed Qt (since the whole idea of the static version is to be independent of development directories) and sure enough it complained of not being able to access any fonts. So I concluded Qt changed their static version to no longer input /usr/share/fonts. A little research on the internet told me they instead look under ...somedir/lib/fonts.
With a little more research I found I could use env var QT_QPA_FONTDIR to set the directory of search (though I suspect it might have been reduced to a one-level search, not recursive). Not wanting to have to write a script to wrap the program and set the environment variable before calling it, I proceeded to use a putenv() in the program. I chose a place that I thought would set the variable before Qt read the environment, and since global contructors are called before main() and certainly before QApplication is instantiated, I put it in a constructor.
But, guess what, they seem to read the environment before my constructor set the QT_QPA_FONTDIR=/usr/share/fonts.
So please help!!!! How do I make MY constructor get executed first?
Some compilers offer pragmas that attempt to help you do such things, but the simple truth is that C++ offers no guarantees of the order of global construction between modules. There isn't even a guarantee that one object dependent on another in another module will be initialized second. Globals are a minefield in this respect.
If you need a portable solution, there really isn't one.
If you can guarantee your compiler will always be the same flavor, then you can use hacks like pragmas, attributes, or link order to work around the problem. For instance, gcc offers a variable attribute called init_priority, though I'm not sure you can make your priority higher than the default.
Really, it would be better to solve the problem another way.
Thank you Aiken Drum. The link order worked. I created the smallest .cpp that made a constructor to be instantiated globally calling the putenv() to set up my environment. I put it first in the .pro file, and checked that the makefile it created had it first in the link line. And, hey presto!! the Qt program now gets the font Database from the right place. Thank you so much. It's the link order that sets the constructor order (at least with GCC).
I migrated a project from Qt4 to Qt5, and it compiles and everything but the application crashes before it even reaches the main function. I know there is a null value that fucks up something at some point, maybe a file that cant find or something, but there are so many .cpps and .h and libraries that its pretty hard to locate the source of the error plus I cant set any breakpoints. I have a lot of debugging data so maybe any of you can guide me in the right direction. I dont know what I would be doing without stack overflow honestly, so thankyou in advance.
When debugging I get different crashes:
The stack in each case shows different crashes, but all of them have something in common, which is this __do_global_ctors thingy, I have researched and apparently it has to do with constructors, but I have no idea what I should be looking for.
if I missed any info please do ask. I hope someone can enlighten me, I am so so close to get this working.
The __do_global_ctors() is called before your main(), as the framework needs to instantiate all of the global objects that main() might use.
This method will call the constructors for all static objects, and their component objects. I.e. all static constructors.
From the look of the stack trace, it appears that the segfault occurs during the construction of a QGlobalStatic<QMutex, [incomlpete types - see trace for details]> object, which makes sense. This is being constructed by qRegisterResourceData as part of qInitResources_mimetypes.
Try placing a breakpoint in this function in qrc_mimetypes.cpp (if you have the source) and see where that gets you. Or look at the Qt documentation for mimetypes initialisation and make sure you've specified your application's resources correctly.
I managed to solve the issue by thoughtfully re-compiling all the libraries to Qt5 and making sure all the cpps that the program refered were Qt5 too. Also double-checked the linkings. I thought I had done it but apparently I missed one library.
Mind that some libraries need to be migrated and there are others that you can download and compile directly with Qt5. If you are having this same problem make sure that there are no Qt5 versions of that library before migrating them yourself.
Good morning all,
Forgive me if the title is not too clear, I'll try to explain more here:
I am currently working with the ASI for VBS2. VBS2 executes functions from a VBS2 DLL plugin. I have my own application which I want to use to modify variables within that plugin whilst it is being used, to change what is being executed by VBS2. I began by, foolish as it may be, directly changing the variables with my application whilst the VBS2 program was running.
When this did not work I tested and found that the VBS2 program was using a different instance of the "message" object, in which I was storing the variable, to the one being accessed by my application.
What I would like to do is have my application access the same instance of the object being accessed by VBS2. I have experimented a bit with
#pragma data_seg(".testseg")
Message msg;
void foo(...); //etc.
#pragma data_seg()
but for some reason or another it still appears there are two instances being used.
I would greatly appreciate any and all help, and would add that C++ is a new language to me so please be gentle. :)
Thanks,
M
You need to use linker flags to tell the linker to place that segment in sharable memory.
See http://msdn.microsoft.com/en-us/library/ms933104.aspx
I belive you need to add something like
#pragma comment(linker, "/SECTION:.testseg,RWS")
to your program.
I'm not sure, this may only work in a DLL...
If I understand correctly what you want, you can't do this with standard C/C++ tools. Your program and the other program live in separate memory spaces and they are completely insulated from each other. If your program has administrative privileges, you can attempt to read & write the memory space of the other process using WriteProcessMemory():
http://msdn.microsoft.com/en-us/library/ms681674%28v=VS.85%29.aspx
But then there's a problem of finding the right object in that memory space.
It's not clear whether you have the source for the plugin. If you do, there are other interprocess communication techniques that can be utilised. None as simple as just changing the variable, unfortunately.