Results 1 to 9 of 9
  1. #1
    EmuTalk Member Garstyciuks's Avatar
    Join Date
    Jan 2006
    Posts
    152

    Need help with MFC

    I have this code to create another dialog, for debugger:

    CDebug *dlg = new CDebug(this);

    dlg->ShowWindow(SW_NORMAL);
    dlg->UpdateWindow();

    It displays properly, etc... But I am not sure when to call delete dlg;



    • Advertising

      advertising
      EmuTalk.net
      has no influence
      on the ads that
      are displayed
        
       

  2. #2
    ????????????????????????? Doomulation's Avatar
    Join Date
    Nov 2001
    Location
    ????????????????
    Posts
    8,780
    When you call delete, it will destruct the class and destroy the window. Make sure you just call it whenever you're done with the window!
    I'm not really sure what you're trying to do, though, or when you want to delete the dialog. Provide more info.
    Btw, before showing a window, you must create it using the Create member, or alternatively use DoModal (don't create it first).
    Atashi wa juu-yon-sai no onna no ko! Atashi no namae wa Miizuki. Yurushiku ne!
    Nani? Atashi o shinjirimasen desu ka? Baka!
    "You're all doomed! Doomed, I say! Hehe... are we approaching the end of the world?"

    shikata ga kaite aru - "the instructions are written above"
    Need to download GoodN64 or instructions to use it? Need to check if it's a good or bad rom?
    Download: Glide64 | Hacktarux's wrapper

  3. #3
    EmuTalk Member Garstyciuks's Avatar
    Join Date
    Jan 2006
    Posts
    152
    The dialog gets created in the constructor of the class. I wonder if I could do "delete this;" at the CDebug::OnClose function (which is called when OK button is pressed or the X on the system menu)

    Edit: I can't test it if it gets deleted properly, because I get some strange errors when runing in debug mode which don't occure in normal mode. CDebug has base class CDialog, and all that code in my first post gets called from CMainWnd class, which base class is CWnd.
    Last edited by Garstyciuks; January 27th, 2006 at 17:47.

  4. #4
    EmuTalk Member Garstyciuks's Avatar
    Join Date
    Jan 2006
    Posts
    152
    What I am actually asking is can a class delete the instance of itself in some of its function. Eg. is this valid?:

    class asdf
    {
    asdf()
    {
    }
    void del()
    {
    delete this;
    }
    };

    int main()
    {
    asdf *zxcv = new asdf();
    zxcv->del();
    return 1;
    }

  5. #5
    Saturnin forever ! Runik's Avatar
    Join Date
    Aug 2005
    Posts
    45
    My guess is that your program will crash doing so ...
    You have to delete your class outside of it, i.e :

    Code:
    int main()
    {
    asdf *zxcv = new asdf();
    // do stuff
    delete zxcv;
    return 1;
    }
    Beware the duck !

  6. #6
    EmuTalk Member Garstyciuks's Avatar
    Join Date
    Jan 2006
    Posts
    152
    Hmm, I think that I don't understand all the concepts behind the new and delete operators. After deleting the class, the pointer still points to the same memory location and you can call all of its functions and variables after deleting it. I thought that I was supposed to get access violation error or something. This is the code I used for testing it:

    Code:
    class asdf
    {
    public:
    	asdf()
    	{
    	}
    
    	int a;
    
    	void inc()
    	{
    		a+=1;
    	}
    
    	void del()
    	{
    		delete this;
    	}
    };
    
    int main ()
    {
    	asdf *zxcv = new asdf();
    	zxcv->a = 5;
    	//zxcv->del();
    	delete zxcv;
    	zxcv->a = 1337;
    	zxcv->inc();
    	return 1;
    }
    And I used the MSVC debugger to see the values. delete zxcv; does the same as zxcv->del(); You don't get an error for deleting the class from the inside. Could someone explain me what actually new and delete operators do, or refer me to a page containing information about it?

  7. #7
    Saturnin forever ! Runik's Avatar
    Join Date
    Aug 2005
    Posts
    45
    "New" dynamically instanciates your class into an object, and "delete" frees the memory.
    Using the object after having deleted it is a very bad idea, as the memory pointed isn't valid.
    I'm surprised that you don't have a crash doing so ...

    The memory pointed after the delete can be anything. It can be the same than before, or it can be anything else.
    If you want to be sure to clear the pointer after the delete, you can add
    Code:
    zxcv = NULL;
    after your delete.

    I don't have any link for you, but a little research on OOP or C++ should give you answers
    Beware the duck !

  8. #8
    EmuTalk Member Garstyciuks's Avatar
    Join Date
    Jan 2006
    Posts
    152
    Not getting an error when using freed memory is amazing, that's why I thought that I am using delete wrong or something, but it all seems OK.

  9. #9
    ????????????????????????? Doomulation's Avatar
    Join Date
    Nov 2001
    Location
    ????????????????
    Posts
    8,780
    For your questions: yes, it is valid to let a class delete itself, BUT if you do, make sure the class DOES NOT access any of its members.
    The reason you don't get an access violation after deleting the object is because the functions doesn't exist in the class. Only the class's member data does. If you try to access the member data outside or in a function in the class after it's destructed, it's very likely you'll get an access violation.
    Avoid doing that. A good practice is to set a pointer to NULL when it's freed. This will make sure windows throws an access violation if you try to use it.

    As explained, new allocates memory for your object, and delete returns that memory to the OS. Freed memory doesn't mean you can't use it, but you'll likely get an access violation doing so. But sometimes, you won't. In any case, you should avoid that.

    Does that explain your questions?
    Atashi wa juu-yon-sai no onna no ko! Atashi no namae wa Miizuki. Yurushiku ne!
    Nani? Atashi o shinjirimasen desu ka? Baka!
    "You're all doomed! Doomed, I say! Hehe... are we approaching the end of the world?"

    shikata ga kaite aru - "the instructions are written above"
    Need to download GoodN64 or instructions to use it? Need to check if it's a good or bad rom?
    Download: Glide64 | Hacktarux's wrapper

Similar Threads

  1. Replies: 2
    Last Post: August 9th, 2005, 02:31
  2. mfc open dialg question
    By mesman00 in forum Programming
    Replies: 7
    Last Post: August 25th, 2004, 11:21
  3. MFC Tab Controls
    By zenogais in forum Programming
    Replies: 3
    Last Post: July 30th, 2004, 21:46
  4. fed up with mfc
    By mesman00 in forum Programming
    Replies: 17
    Last Post: July 19th, 2004, 15:14
  5. mfc close message
    By mesman00 in forum Programming
    Replies: 7
    Last Post: July 12th, 2004, 13:47

Posting Permissions

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