CObArray memory leak

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
{
public:
      CMyObject ();
      virtual ~CMyObject ();
      CString  GetMyString(){return sMyString ;};
      void SetMyString(CString x){sMyString = x;};
private:
      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.
dociebAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mahesh1402Commented:
Refer "MFC Collections: The CObArray Class"

http://www.functionx.com/visualc/collections/cobarray.htm <====  

MAHESH
0
AndyAinscowFreelance programmer / ConsultantCommented:
If they are all of type CMyObject then I would recommend a typed pointer array rather than the generic CObArray.

eg.
class CMyObject : public CObject
{
...
};
      typedef CTypedPtrArray<CObArray, CMyObject*> CMyObjectArray;


then elsewhere
CMyObjectArray m_arMyObjects;

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

to use
CMyObject* pOb = m_arMyObjects[3]; //or whatever
pOb->DoSomething(...);

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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyAinscowFreelance programmer / ConsultantCommented:
bad pointer - I guess you are doing the delete without the RemoveAt
0
DanRollinsCommented:
Since your CObArray just encapsulates an array of CStrings, I recommend that you use the existing CStringArray class.  See:

   http://msdn2.microsoft.com/en-us/library/tddz3etf.aspx
and
   http://msdn2.microsoft.com/en-us/library/0aaaacss.aspx

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
and
    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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.