|
|
Hello everyone,
This is what mentioned in the book, ATL Internals about why Release function of CComPtr and CComQIPtr is needed for global and static variable.
--------------------
The destructor for global and static variable only executes after the main function exits, long after CoUninitialize runs, (That is, if it runs at all. You must link with the C++ runtime library for the constructors and destructors of global and static variable to run.) ATL itself doesn't use the C/C++ runtime library; thus, by default, ATL components don't link with the library.
--------------------
I do not quite understand what means "must link with the C++ runtime library for the constructors and destructors ... to run"? Could anyone help please?
thanks in advance,
George
|
|
|
|
|
Try this experiment and you will see.
Find or write some code like this
<br />
<br />
class CSomeclass<br />
{<br />
public:<br />
static int MyMember;<br />
};<br />
<br />
int CSomeclass::MyMember = 42;<br />
<br />
Place a breakpoint on the assignment and also one at the beggining of your code, in main or InitiInstance or whatever.
Run under Debug and you may be surprised at what happens. When the debugger stop take a look at the call stack and the order in which the breakpoints are hit.
Bootstrapping an MSVC C++ program is about 10,000x more complex than most people realize.
Nothing is exactly what it seems but everything with seems can be unpicked.
modified on Tuesday, April 8, 2008 6:40 AM
|
|
|
|
|
Thanks Matthew,
To my surprise, when set a break point to the assignment line, when execute the program, the break point will automatically moved to the beginning of main. Any ideas?
class CSomeclass
{
public:
static int MyMember;
};
int CSomeclass::MyMember = 42;
int main()
{
return 0;
}
regards,
George
|
|
|
|
|
Hmm, not sure, are you linking to the C Runtime, MSVCRT.dll or that static lib equivalent. You can find out by looking at the value of CSomeclass::MyMember when main() gets called if it's not 42 then static initialization is not being called hence it's reasonable it won't let you set a breakpoint on a line that's never called. Try changing from a CONSOLE subsystem app to a WINDOWS subsystem app as well as that makes a differenece to the entry point used. You'll need a WinMain function to get a raw windows app off the ground without MFC. If you have a look in your equivalent of
C:\Program Files\Microsoft Visual Studio 8\VC\crt\src\crtexe.c
, assuming you installed it, you'll see the code that actually runs when you application starts up. The call to _initterm( __xc_a, __xc_z ) at around Line 512 is where your static class members like CSomeclass::MyMember.
Please don't ask me to explain it. It's 'for reference only'.
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
Thanks Matthew,
I think C++ Runtime library is fundamental and ATL is also C++ code, I do not understand why ATL is not dependent on C++ Runtime library? What functions are provided by C++ Runtime library besides STL, which ATL is not dependent on?
regards,
George
|
|
|
|
|
You could try using the depends.exe tool that comes with Visual Studio. Use it to open MSVCRT.dll and look at the lower of the 2 panes on the right hand half of the upper window.
This will list all the stuff MSVCRT exports, a lot of functions. Many of these are defined by the C Language standard which describes a set of library functions which are to be available with the language. You can probably find a copy of the standard on the internet somewhere. Some of the others are widely implemented extensions to the standard, others are Microsoft specific additions and a few are wierd undocumented stuff for supporting the underpinnings of Windows and even integrating with .NET. It's a beastly big library and although Microsoft do provide the source, due to code injection they don't actually provide quite all of it and it certainly isn't open. The main alternative C Library which has all the standard functions in it and more is glibc the GNU C Library which is what GNU-Linux uses and which runs just about everywhere except on Windows
It would be lovely if someone produced a fully open source completely standards compliant C Library compatible with MSVCRT and with glib and that runs absolutely everywhere , sadly that would take many, many person years of work.
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
Thanks Matthew,
Suppose I have an ATL DLL, which is not dependent on CRT. And in the DLL, I implement a COM in-proc server. If I have another EXE, which will use the DLL COM server and the EXE is dependent on CRT.
Are there any chances the constructors of the global/static objects executed, e.g. when loaded by EXE before DllMain? And why?
regards,
George
|
|
|
|
|
I'm not sure exactly what you're asking. If you have an ATL DLL which doesn't link, even implicitly, to the CRT but contains statics and non const globals then 1 of 2 things must be true.
1 ATL must provide a static initialization mechanism independent of the CRT. (As far as I know this is not true)
or
2 Your statics won't get initialized.
I would guess that in fact if you add statics and non const globals to an ATL DLL then it does become implicitly dependent on the CRT, even if there's no explicit linkage.
You could experiment with the /nodefaultlib option of the linker and specify exactly which libs you will and won't link to to see what effect that has. You can code breakpoints directly into any static initialization code you think may be being called before your debugger normally get involved using DebugBreak(); or even __asm{ int 3 }; a useful if naughty trick
Watch out for implicitly linked Dlls that get loaded dynamically but right at startup and for Libs your project 'inherits' out of thin air. The $NOINHERIT linker options can be invaluable.
Obviously I wouldn't recommend doing any of this with any project you can't afford to break as break it you most ccertainly will if you banish the CRT.
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
Thanks Matthew,
Matthew Faithfull wrote: get involved using DebugBreak(); or even __asm{ int 3 };
Why I need to code explicitly DebugBreak? Why not can I just double click to set a break point to the line of constructor of global/static variable in VS?
regards,
George
|
|
|
|
|
Believe it or not the VS Debugger is not perfect and sometimes it will miss breakpoints if they are too 'early' in the code. Either because it doesn't think the line will be called or because it isn't completely ready when that line is hit. Sometimes static construction can fall into this trap and become 'undebuggable' although not usually. It may also depend on whether the Debugger can find and match the source for the CRT.
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
Thanks Matthew,
Where to set DebugBreak in the sample we discussed before? Confused.
class CSomeclass
{
public:
static int MyMember;
};
int CSomeclass::MyMember = 42;
int main()
{
return 0;
}
regards,
George
|
|
|
|
|
In this case you can't because int doesn't have a constructor, at least not one with source code. If you make CSomeclass::MyMember something more complex like a COtherclass then you could insert a DebugBreak(); in COtherclass::COtherclass();
"The secret of happiness is freedom, and the secret of freedom, courage."
Thucydides (B.C. 460-400)
|
|
|
|
|
Thanks Matthew,
I have made exercise (post code below), and find there is no need to use DebugBreak(). I can set breakpoint directly in class CSomeclass. Please help to review my code and feel free to correct me if I am wrong.
class Foo {
private:
int i;
public:
Foo (int i)
{
this->i = i;
}
};
class CSomeclass
{
private:
Foo* pFoo ;
public:
CSomeclass()
{
pFoo = new Foo (100);
}
~CSomeclass()
{
delete pFoo;
}
};
CSomeclass ginstance;
int main()
{
return 0;
}
have a good weekend,
George
|
|
|
|
|
I have 2 dialogs in my application. now i need to pass continuous values from one dialog box to another.
both the dialogs are opened simultaneously.
the calculations are done on one dialog box and the result needs to be shown in another dialog box. how do i do it?
i tried passing the value by
<br />
m_realtime.value = m_value <br />
but nothing is displaying on the edit box.
|
|
|
|
|
Chandrasekharanp wrote: but nothing is displaying on the edit box.
Why would it? All you're doing is changing a variable. Windows / MFC is not psychic enough to know that variable should be put into some control on a dialog box.
m_realtime.UpdateData (FALSE); will call that dialog DoDataExchange method, which would call any DDX_etc lines of code put in there by codewizard (or yourself) to transfer variables to controls.
Iain.
Iain Clarke appears because CPallini still cares.
|
|
|
|
|
You can use like this code:
CMain *main=(CMain*)GetParent();
main->yourcontrol
|
|
|
|
|
Hi all,
Anyone can tell me how can resize the control which is inside of other control.
i m using "BPCtrlAnchorMap.h" this class for resizing this available on CodeProject.
in brief i m using a SDI type of application here in 1 FormView i m using a TabCtrl and inside the Tab Control i m using diffrent dialog box those are having ListCtrl and some other control.
with the using of class for resizing the TabCtrl is Resized Properly but other control inside the Tab control are not resized.
Please anyone can help me.
Thanks in advance.
|
|
|
|
|
ThatsMe. wrote: Anyone can tell me how can resize the control which is inside of other control.
Control is a fancy name for Window. And all windows are inside other windows. So, you do it the same way you resize anything:
SetWindowPos.<br />
<br />
If you are using one of the articles on codeproject that does resizing for you, then you need to look at the example code that comes with that article. And each article has a message board on it. First read all the postings on that article; someone may have already fixed your problem. If you're still struggle, try posting on that message board first - the author is notified of any posting, and may be able to help you.<br />
<br />
Iain. <br />
<br />
<div class="ForumSig">Iain Clarke appears because <sup class="Error">C</sup>Pallini still cares.</div>
|
|
|
|
|
Did you use of MoveWindow?
|
|
|
|
|
hi
i need study about Structure of MFC Architecture ...please send me some useful links ..
Thanks And Regards
Nisha.S
|
|
|
|
|
Never heard of something called google[^] ?
|
|
|
|
|
Well, there are lots of books on MFC but i'll particularly recommend
http://www.codeproject.com/?cat=2[^] as it contains lots of userful articles for both biginners and advanced users/programmers...
|
|
|
|
|
|
For the structure of MFC, I find the MFC source code to be the best place to study.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|