CObArray memory leak

Posted on 2006-04-26
Last Modified: 2013-11-20
I have a CObArray that after I the program runs a little bit I start getting crashes due to bad pointers so I must have a memory leak someone.  I would like to know a few things in hopes that this helps me find where it is.  Lets say I have an Object that looks like this:
class CMyObject : public CObject
      CMyObject ();
      virtual ~CMyObject ();
      CString  GetMyString(){return sMyString ;};
      void SetMyString(CString x){sMyString = x;};
      CString sMyString;
Can you tell me
1) How to "properly" fill the CObArray with CMyObject items
2) How to "properly" remove CMyObject items
3) How to "properly" udpate CMyObject items that are in CObArray

I'm assuming somewhere I did not do one of these processes correctly so maybe seeing the proper way of doing it will help me see the error.
Question by:docieb
    LVL 22

    Expert Comment

    Refer "MFC Collections: The CObArray Class" <====  

    LVL 43

    Accepted Solution

    If they are all of type CMyObject then I would recommend a typed pointer array rather than the generic CObArray.

    class CMyObject : public CObject
          typedef CTypedPtrArray<CObArray, CMyObject*> CMyObjectArray;

    then elsewhere
    CMyObjectArray m_arMyObjects;

    to add
    CMyObject* pOb = new CMyObject(...);

    to use
    CMyObject* pOb = m_arMyObjects[3]; //or whatever

    to delete
    CMyObject* pOb = m_arMyObjects[3]; //or whatever
    m_arMyObjects.RemoveAt(3);  //remove from array - does NOT delete the object at position 3
    delete pOb;   //and clean up memory
    LVL 43

    Expert Comment

    bad pointer - I guess you are doing the delete without the RemoveAt
    LVL 49

    Expert Comment

    Since your CObArray just encapsulates an array of CStrings, I recommend that you use the existing CStringArray class.  See:

    Or, if you are more interested in the "proper mechanisms" then you may be surprised to know that a very complete example already exists on your hard disk!  I suggest you peruse the MFC source code for that object.  Look in...

        C:\Program Files\Microsoft Visual Studio\VC98\MFC\Include\AFXCOLL.H
        C:\Program Files\Microsoft Visual Studio\VC98\MFC\SRC\ARRAY_S.CPP

    It includes an implementation of
        operator []
    which will certainly impress even the most jaded professor!

    -- Dan

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    configuration management 2 96
    Process filename extension 3 139
    modThree challenge 4 53
    how to split multiple lines delimiter : 8 25
    In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
    Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now