What's new

Need help with MFC

Garstyciuks

New member
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;
 

Doomulation

?????????????????????????
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).
 
OP
Garstyciuks

Garstyciuks

New member
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:
OP
Garstyciuks

Garstyciuks

New member
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;
}
 

Runik

Saturnin forever !
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;
}
 
OP
Garstyciuks

Garstyciuks

New member
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?
 

Runik

Saturnin forever !
"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 ;)
 
OP
Garstyciuks

Garstyciuks

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

Doomulation

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

Top