Кто-то здесь что-то знает по Visual C++? На codeguru.com никак не помогали, пришлось спросить здесь, у друзей
Кто-то здесь что-то знает по Visual C++? На codeguru.com никак не помогали, пришлось спросить здесь, у друзей
Давай вопрос, попробую помочь.
Hooray! Anyhow, I don't know how Russians talk about code, so I'll just ask in English -
I have a class "A." Specifically, it's derived from CFrameWnd, but I don't think it really matters. Anyhow, here's the interface for it:
My problem is with this mRectList. In the constructor for this class, I try to add some objects to it(and yes, they are derived from CObject).Code:class CMainFrame: public CFrameWnd { private: CObList mRectList; public: CMainFrame(); afx_msg void OnPaint(); afx_msg void OnLButtonDown(UINT nFlags, CPoint pt); afx_msg void OnChar(UINT nChar, UINT nRep, UINT nFlags); DECLARE_MESSAGE_MAP(); };
The code that gives me problems is the AddTail bit. The program compiles fine, but when I run, it gives me the WinXP "x.exe has encountered a problem and needs to close" and then, well, it closes. No information about the error or nothing. Although one time when I ran it, it gave me something like "Debug Assertion Failure!" The odd thing is, if I run the program in debug, it works fine.Code:CMainFrame::CMainFrame() { Create(NULL, "Rectangles", WS_OVERLAPPEDWINDOW, CRect(0,0,400,400)); CenterWindow(); CMyRect* pRect; pRect = new CMyRect; mRectList.AddTail(pRect); }
Now - here's the real kicker - I copied all the code files from this program and made a new project out of it(with the intent of showing it to the people at CodeGuru). The thing is, this new project, although composed of the exact same code as the original, works completely fine!
Anyhow, if anyone can explain this behavior, they'll be my hero for life. Well, maybe not for life, but at least for a week or so.
BTW, does anyone know any Visual C++ forums besides CodeGuru? They didn't seem to help my problem very much...
It would be interesting to look into your puzzle if I had more time. But I know of a good resource where you could get help on that: http://www.experts-exchange.com
~ Мастерадминов Мастерадмин Мастерадминович ~
Pravit, there are two problems with your code:
1) You don't have to create the main window explicitly, since it is created by MFC framework implicitly. That's the reason for assertion btw - you're trying to create already created window. If you haven't installed MFC/STL/ATL/CRT sources for Visual studio it is a good time to do so, you will need them often. And after that next time when you'll see an ASSERT window don't forget to click "Retry" - it will start debugger and you see call stack and source code ( MFC internals in this case ). Here is your assert:
wincore.cpp Ln 626
It's pretty obvious that you're doing something wrong. And remember, that there is no asserts at all in release mode at all ( to make your application shorter and faster), so you need to fix all problems ( and asserts ) before compiling in release mode.Code:ASSERT(pWnd->m_hWnd == NULL); // only do once
So first step is to comment out this line:
If you need to change size, position, style, caption etc of implicitly created window like this one, do it in PreCreateWindow() - see code below.Code:Create(NULL, "Rectangles", WS_OVERLAPPEDWINDOW, CRect(0,0,400,400));
You can't call CenterWindow() in PreCreateWindow(), because the window is not created yet at the moment when PreCreateWindow is called. So, you need to do that in OnCreate() handler which is called right after window creation.
My advice for you is to buy a good book on MFC if you're going to use it.
2) You have a memory leak, "new CMyRect" object is never deleted. Note the following lines in output ( in debug mode only ):
See my correction of this problem in the ~CMainFrame().Code:Detected memory leaks! Dumping objects -> c:\...\visual studio projects\test3\mainfrm.cpp(28) : {75} client block at 0x00325378, subtype c0, 20 bytes long. a CObject object at $00325378, 20 bytes long Object dump complete.
Good luck in learning C++Code:BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) ON_WM_CREATE() END_MESSAGE_MAP() // CMainFrame construction/destruction CMainFrame::CMainFrame() { // Create(NULL, "Rectangles", WS_OVERLAPPEDWINDOW, CRect(0,0,400,400)); // CenterWindow(); CMyRect* pRect; pRect = new CMyRect; mRectList.AddTail(pRect); } CMainFrame::~CMainFrame() { while( !mRectList.IsEmpty() ) { delete mRectList.RemoveHead(); } } BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) ) return FALSE; cs.x = 0; cs.y = 0; cs.cx = 400; cs.cy = 400; cs.lpszName = _T("Rectangles"); return TRUE; } int CMainFrame::OnCreate( LPCREATESTRUCT lpCreateStruct ) { int result = CFrameWnd::OnCreate(lpCreateStruct); if( !result ) { CenterWindow(); } return result; }
http://www.windev.org/Originally Posted by Pravit
News group:
http://groups-beta.google.com/group/comp.lang.c++
If you'd like to learn russian and programming in the same time:
http://groups.yahoo.com/group/swrus-programming/
It's official - Denis, you are my new hero!!
There's one problem though - I put your code in, it compiles fine, but alas, I get a Debug Assertion Failure, file winocc.cpp, line 301! Maybe I did it wrong? I rewrote the header file to accomodate for the functions in your code. Anyhow, the error doesn't seem to be caused by the MRectList code. Here's how I wrote the header:
Thanks for your comments on the other stuff, they were really helpful! They will be filed away and applied in my next MFC programCode:#pragma once #include <afxwin.h> class CMainFrame: public CFrameWnd { private: CObList mRectList; public: CMainFrame(); ~CMainFrame(); BOOL PreCreateWindow(CREATESTRUCT& cs); // int OnCreate( LPCREATESTRUCT lpCreateStruct ); // afx_msg void OnPaint(); // afx_msg void OnLButtonDown(UINT nFlags, CPoint pt); // afx_msg void OnChar(UINT nChar, UINT nRep, UINT nFlags); afx_msg int OnCreate( LPCREATESTRUCT lpCreateStruct); DECLARE_MESSAGE_MAP(); };
Well, you see, the book I'm using does the Creates in the constructor - Introduction to MFC Programming with Visual C++, by Richard M. Jones. As far as I can tell he's trying to write very simple code in the beginning stages so people won't get lost.My advice for you is to buy a good book on MFC if you're going to use it.
BTW, would you know why the original code worked when I made a copy of the project but wouldn't work in the original?
Russian Lessons | Russian Tests and Quizzes | Russian Vocabulary |