CRuntimeClass::CreateObject() and delete
-
I tried to create a cobject derived class: CObject*t=(CObject*)RUNTIME_CLASS(CHScrollTitle)->CreateObject(); and then i tried to free it with: delete t; or CHScrollTitle::operator delete(t); but the delete operator failes in debug-mode with an assert-notice. (something with pHead->nBlockUse==nblockuse) does anybody know how to free a with CreateObject(or CArchive::ReadObject()) created object? \ TIA B.Bruggeman
-
I tried to create a cobject derived class: CObject*t=(CObject*)RUNTIME_CLASS(CHScrollTitle)->CreateObject(); and then i tried to free it with: delete t; or CHScrollTitle::operator delete(t); but the delete operator failes in debug-mode with an assert-notice. (something with pHead->nBlockUse==nblockuse) does anybody know how to free a with CreateObject(or CArchive::ReadObject()) created object? \ TIA B.Bruggeman
This problem isn't probably related to CRuntimeClass::CreateObject - seems that you've corrupted the heap somehow. BTW: calling CRuntimeClass::CreateObject makes absolutely no sense when you know the exact type of object - in your case, it's CHScrollTitle. You can just write
t = new CHScrollTitle;
Tomasz Sowinski -- http://www.shooltz.com
-
This problem isn't probably related to CRuntimeClass::CreateObject - seems that you've corrupted the heap somehow. BTW: calling CRuntimeClass::CreateObject makes absolutely no sense when you know the exact type of object - in your case, it's CHScrollTitle. You can just write
t = new CHScrollTitle;
Tomasz Sowinski -- http://www.shooltz.com
I also thought it was a memoryleak/overwrite but i wrote a simple test program and than already it assert's about the debug-memory-tracing-variable m_nBockUse, but when i use new/delete it doesn't assert.. i have different kind of classes in a list and i want to serialize them using Write/ReadObject, so i can't use an ordinary new/delete operator. but when i tested this it asserts in the (debug) operator delete, so i tested it with the lines i posted before, but it still asserted.. so do you have other suggestions? btw in the file afxmem.cpp are many operator delete which (in debugmode) either call _free_dbg(p, _CLIENT_BLOCK); or _free_dbg(p, _NORMAL_BLOCK);, so probably the wrong deconstructor is called.. but how do I fix this? TIA. B.Bruggeman
-
I also thought it was a memoryleak/overwrite but i wrote a simple test program and than already it assert's about the debug-memory-tracing-variable m_nBockUse, but when i use new/delete it doesn't assert.. i have different kind of classes in a list and i want to serialize them using Write/ReadObject, so i can't use an ordinary new/delete operator. but when i tested this it asserts in the (debug) operator delete, so i tested it with the lines i posted before, but it still asserted.. so do you have other suggestions? btw in the file afxmem.cpp are many operator delete which (in debugmode) either call _free_dbg(p, _CLIENT_BLOCK); or _free_dbg(p, _NORMAL_BLOCK);, so probably the wrong deconstructor is called.. but how do I fix this? TIA. B.Bruggeman
- memory overwrites: the fastest way to detect/remove them is by using automatic diagnostic tools, like BoundsChecker (www.numega.com) or Purify (www.rational.com) 2) serializing different CObject-derived classes from list: just use operator << with pointers to objects. MFC is smart enough to write metadata containing type information to archive. You don't need to write type information yourself.
// write
// p points to something derived from CBaseClass
CBaseClass *p = list.GetHead();
ar << p;
// ...
// read
CBaseClass *p;
ar >> p;
// now p contains address of dynamically created
// object derived from CBaseClass. The type is
// correctly restored. p should be deleted with a
// call to plain delete when you no longer need
// this object:
delete p;Tomasz Sowinski -- http://www.shooltz.com