Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 406
  • Last Modified:

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.
0
docieb
Asked:
docieb
  • 2
1 Solution
 
mahesh1402Commented:
Refer "MFC Collections: The CObArray Class"

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

MAHESH
0
 
AndyAinscowCommented:
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
 
AndyAinscowCommented:
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now