Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CRuntimeClass::CreateObject() and delete

CRuntimeClass::CreateObject() and delete

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingtutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BruteBertus
    wrote on last edited by
    #1

    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

    T 1 Reply Last reply
    0
    • B BruteBertus

      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

      T Offline
      T Offline
      Tomasz Sowinski
      wrote on last edited by
      #2

      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

      B 1 Reply Last reply
      0
      • T Tomasz Sowinski

        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

        B Offline
        B Offline
        BruteBertus
        wrote on last edited by
        #3

        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

        T 1 Reply Last reply
        0
        • B BruteBertus

          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

          T Offline
          T Offline
          Tomasz Sowinski
          wrote on last edited by
          #4
          1. 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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups