Results 1 to 7 of 7

Thread: Программисты нужны

  1. #1
    Завсегдатай
    Join Date
    Feb 2003
    Location
    с. Хреновое Воронежской обл.
    Posts
    2,481
    Rep Power
    14

    Программисты нужны

    Кто-то здесь что-то знает по Visual C++? На codeguru.com никак не помогали, пришлось спросить здесь, у друзей

  2. #2
    Почтенный гражданин
    Join Date
    Oct 2004
    Location
    SPb.ru -> California.us
    Posts
    394
    Rep Power
    10
    Давай вопрос, попробую помочь.

  3. #3
    Завсегдатай
    Join Date
    Feb 2003
    Location
    с. Хреновое Воронежской обл.
    Posts
    2,481
    Rep Power
    14
    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:

    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();
    };
    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:
    CMainFrame::CMainFrame()
    {
    	Create(NULL, "Rectangles", WS_OVERLAPPEDWINDOW, CRect(0,0,400,400));
    	CenterWindow();
    	CMyRect* pRect;		
    	pRect = new CMyRect;
    	mRectList.AddTail(pRect);
    }
    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.

    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...

  4. #4
    Administrator MasterAdmin's Avatar
    Join Date
    Oct 2002
    Location
    MasterRussian.com
    Posts
    1,731
    Rep Power
    13
    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
    ~ Мастерадминов Мастерадмин Мастерадминович ~

  5. #5
    Почтенный гражданин
    Join Date
    Oct 2004
    Location
    SPb.ru -> California.us
    Posts
    394
    Rep Power
    10
    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
    Code:
       ASSERT(pWnd->m_hWnd == NULL);   // only do once
    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.

    So first step is to comment out this line:
    Code:
      Create(NULL, "Rectangles", WS_OVERLAPPEDWINDOW, CRect(0,0,400,400));
    If you need to change size, position, style, caption etc of implicitly created window like this one, do it in PreCreateWindow() - see code below.

    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 ):

    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.
    See my correction of this problem in the ~CMainFrame().

    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;
    }
    Good luck in learning C++

  6. #6
    Почтенный гражданин
    Join Date
    Oct 2004
    Location
    SPb.ru -> California.us
    Posts
    394
    Rep Power
    10
    Quote Originally Posted by Pravit
    ...BTW, does anyone know any Visual C++ forums besides CodeGuru? They didn't seem to help my problem very much...
    http://www.windev.org/

    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/

  7. #7
    Завсегдатай
    Join Date
    Feb 2003
    Location
    с. Хреновое Воронежской обл.
    Posts
    2,481
    Rep Power
    14
    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:

    Code:
    #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();
    };
    Thanks for your comments on the other stuff, they were really helpful! They will be filed away and applied in my next MFC program

    My advice for you is to buy a good book on MFC if you're going to use it.
    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.

    BTW, would you know why the original code worked when I made a copy of the project but wouldn't work in the original?

Similar Threads

  1. Мне нужны ещё примеры... ;-) (??)
    By Hanna in forum Grammar and Vocabulary
    Replies: 8
    Last Post: August 3rd, 2010, 09:38 AM
  2. Мне нужны русские!
    By Duke Atreides in forum Penpals and Language Exchange
    Replies: 18
    Last Post: December 20th, 2009, 10:58 AM
  3. Как шутят программисты
    By Ramil in forum Fun Stuff
    Replies: 13
    Last Post: August 5th, 2007, 09:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Russian Lessons                           

Russian Tests and Quizzes            

Russian Vocabulary