CArray and SetSize member error.

The helpturorial is recommending me to
use the SetSize memberfunction i CArray.
The problem is that an exception is thrown when i do that. Here are the important code in different places in my project CDoc .



protected:

    CArray<CString*, CString*> m_strings;







BOOL CMySerializeDoc::OnNewDocument()
{
      if (!CDocument::OnNewDocument())
            return FALSE;
m_strings.SetSize( 1000);


      // TODO: add reinitialization code here
      // (SDI documents will reuse this document)

      return TRUE;
}







      CString *mystring=new CString;
      *mystring="This is a string written by the author";
                        
      m_strings.Add(mystring);
                        

      ::MessageBox(0,"",*(m_strings.GetAt(0)),0);



Your sincerely andla.

LVL 1
andlaAsked:
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.

mnbmnbCommented:
Why you do not want to use CStringArray? I myself used it and SetSize worked OK.
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
andlaAuthor Commented:
Yes you are right thanks :-), but i think it's important why it doesent work with CArray. With the nowledge of the problem i can deal with other similar problems.
0
andlaAuthor Commented:
Yes you are right thanks :-), but i think it's important why it doesent work with CArray. With the nowledge of the problem i can deal with other similar problems.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

andlaAuthor Commented:
Oops! Double ...
0
mnbmnbCommented:
Did you include <afxtempl.h>?
0
andlaAuthor Commented:
Yes i did. Do you get the same exception?
0
mnbmnbCommented:
I check the same CArray<CString*, CString*> on VC++ 6.0  and its SetSize works OK. I do not see all your code, but for member m_strings try to set "public" access specifier instead of "protected".
0
mnbmnbCommented:
Or place here what "exception" compiler throws.
0
andlaAuthor Commented:
Unhandled exception in MySerialize (MFC40D.DLL): 0XC0000005: Access Violation.

*It does not look pretty this message* :-)
0
andlaAuthor Commented:
Tryed to change to public but the same problem occured.


*Thanks for trying to help me* :-)
0
mnbmnbCommented:
This exception means that you try to access to restricted or invalid area of memory. As long as you create your Carray on the stack, probably you simply set too big size. Try to create your array on the heap:

CArray<CString*, CString*> *m_strings = new CArray<CString*, CString*>;

Hope this help.
0
mnbmnbCommented:
Naturally, member functions further must be called with -> operator:

m_strings->SetSize(1000);
0
andlaAuthor Commented:
Same error. I even tryed to make the setsize smaller. Try to make this sample your self and tell me if it works.

Create a simple MDI project.

Add to CDoc class
public:
CArray<CString*, CString*> *m_strings;

Add to OnNewDocument i CDoc
BOOL CMySerializeDoc::OnNewDocument()
{
      if (!CDocument::OnNewDocument())
            return FALSE;
      m_strings= new CArray<CString*, CString*>;
      m_strings->SetSize(1);
      // TODO: add reinitialization code here
      // (SDI documents will reuse this document)

      return TRUE;
}


Add to serialize in CDoc
void CMySerializeDoc::Serialize(CArchive& ar)
{
      if (ar.IsStoring())
      {
            // TODO: add storing code here
            //ar<<mytest[0];
      CString *mystring=new CString;
      *mystring="This is a string written by the author";
                        
      m_strings->Add(mystring);
                        

      ::MessageBox(0,"",*(m_strings->GetAt(0)),0);

      }//CArray

      else
      {

      }
}


The reason i use it in serialize is because im learning about it.

Good luck!

:-)
0
mnbmnbCommented:
*This* code works OK. Try to do the same(e.g. create new project, past frome here your code, do not forget include in the header file of CDoc class file <afxtempl.h>) and then during debugging step into function SetSize() and catch the line on which exception happens.
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
C++

From novice to tech pro — start learning today.