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. Mobile Development
  3. Mobile
  4. CArray makes me cry !!!!!!!

CArray makes me cry !!!!!!!

Scheduled Pinned Locked Moved Mobile
helpdata-structuresdebuggingquestion
20 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.
  • H Hadi Rezaee

    Hi :( I have a very odd problem that made me really crazy ! :eek: I've an member variable in my dialog class: public: CArray < SomeStruct, SomeStruct > m_MrOdd; In a function I added some values to m_MrOdd, like this: void CMyDialog::SomeFunc1() { m_MrOdd.Add( SomeStruct( 1, 2 ) ); m_MrOdd.Add( SomeStruct( 10, 2 ) ); m_MrOdd.Add( SomeStruct( 1, 212 ) ); } And when i want to use of values, the array show me nothing ! void CMyDialog::SomeFunc2() { if(m_MrOdd.GetSize() == 0) MessageBox( _T("What did you do with my array ?!!!!!!") ); . . . } I don't know what to do ... plz help me. The intresting part is when i debug the program ;P In Adding values, it shows that everything is ok and in using data in another function it shows there is not any value :(( Regards, Hadi

    J Offline
    J Offline
    Joao Paulo Figueira
    wrote on last edited by
    #8

    Got it! Most of the times, we have to read MFC's source code to know whais is happening. Look:

    AFX_INLINE int CArray<TYPE, ARG_TYPE>::Add(ARG_TYPE newElement)
    { int nIndex = m_nSize;
    SetAtGrow(nIndex, newElement);
    return nIndex; }
    .
    .
    .
    template<class TYPE, class ARG_TYPE>
    void CArray<TYPE, ARG_TYPE>::SetAtGrow(int nIndex, ARG_TYPE newElement)
    {
    ASSERT_VALID(this);
    ASSERT(nIndex >= 0);

    if (nIndex >= m\_nSize)
    	SetSize(nIndex+1, -1);
    m\_pData\[nIndex\] = newElement;
    

    }

    The code is using a reference type (ARG_TYPE) to add the objects, that you are passing as unnamed temporaries (when are these being deleted?). Try something like this:

    void CMyDialog::SomeFunc1()
    {
    SomeStruct a(0,0);

    a = SomeStruct( 1, 2 );
    m_MrOdd.Add( a );

    a = SomeStruct( 10, 2 );
    m_MrOdd.Add( a );

    a = SomeStruct( 1, 212 );
    m_MrOdd.Add( a );
    }

    I know it looks odd, but try it. Regards, João Paulo Figueira Embedded MVP

    H 2 Replies Last reply
    0
    • J Joao Paulo Figueira

      What is the value of GetUpperBound ? Regards, João Paulo Figueira Embedded MVP

      H Offline
      H Offline
      Hadi Rezaee
      wrote on last edited by
      #9

      In Adding Function (I added 2 items to array): GetSize() is 2, and GetUpperBound() is 1 In Using Function: GetSize() is 0, and GetUpperBound() is -1 Best Regards, Hadi

      J 1 Reply Last reply
      0
      • H Hadi Rezaee

        In Adding Function (I added 2 items to array): GetSize() is 2, and GetUpperBound() is 1 In Using Function: GetSize() is 0, and GetUpperBound() is -1 Best Regards, Hadi

        J Offline
        J Offline
        Joao Paulo Figueira
        wrote on last edited by
        #10

        Are you absolutely sure that the functions are called in this order? By any chance are you not clearing the array between the calls? (this is weired) Regards, João Paulo Figueira Embedded MVP

        H 1 Reply Last reply
        0
        • J Joao Paulo Figueira

          Got it! Most of the times, we have to read MFC's source code to know whais is happening. Look:

          AFX_INLINE int CArray<TYPE, ARG_TYPE>::Add(ARG_TYPE newElement)
          { int nIndex = m_nSize;
          SetAtGrow(nIndex, newElement);
          return nIndex; }
          .
          .
          .
          template<class TYPE, class ARG_TYPE>
          void CArray<TYPE, ARG_TYPE>::SetAtGrow(int nIndex, ARG_TYPE newElement)
          {
          ASSERT_VALID(this);
          ASSERT(nIndex >= 0);

          if (nIndex >= m\_nSize)
          	SetSize(nIndex+1, -1);
          m\_pData\[nIndex\] = newElement;
          

          }

          The code is using a reference type (ARG_TYPE) to add the objects, that you are passing as unnamed temporaries (when are these being deleted?). Try something like this:

          void CMyDialog::SomeFunc1()
          {
          SomeStruct a(0,0);

          a = SomeStruct( 1, 2 );
          m_MrOdd.Add( a );

          a = SomeStruct( 10, 2 );
          m_MrOdd.Add( a );

          a = SomeStruct( 1, 212 );
          m_MrOdd.Add( a );
          }

          I know it looks odd, but try it. Regards, João Paulo Figueira Embedded MVP

          H Offline
          H Offline
          Hadi Rezaee
          wrote on last edited by
          #11

          I tried this way: OrderItems item(0, 0); item = OrderItems( 10, 96 ); m_OrderItems.Add( item ); item = OrderItems( 5, 13 ); m_OrderItems.Add( item ); But ...... :( Regards, Hadi

          J 1 Reply Last reply
          0
          • J Joao Paulo Figueira

            Are you absolutely sure that the functions are called in this order? By any chance are you not clearing the array between the calls? (this is weired) Regards, João Paulo Figueira Embedded MVP

            H Offline
            H Offline
            Hadi Rezaee
            wrote on last edited by
            #12

            Yes, I'm sure ... As you can see the code is so simple, and I didn't clear any item ... Best Regards, Hadi

            1 Reply Last reply
            0
            • H Hadi Rezaee

              I tried this way: OrderItems item(0, 0); item = OrderItems( 10, 96 ); m_OrderItems.Add( item ); item = OrderItems( 5, 13 ); m_OrderItems.Add( item ); But ...... :( Regards, Hadi

              J Offline
              J Offline
              Joao Paulo Figueira
              wrote on last edited by
              #13

              Man, this is maddening! What SDK are you using? Regards, João Paulo Figueira Embedded MVP

              H 1 Reply Last reply
              0
              • J Joao Paulo Figueira

                Got it! Most of the times, we have to read MFC's source code to know whais is happening. Look:

                AFX_INLINE int CArray<TYPE, ARG_TYPE>::Add(ARG_TYPE newElement)
                { int nIndex = m_nSize;
                SetAtGrow(nIndex, newElement);
                return nIndex; }
                .
                .
                .
                template<class TYPE, class ARG_TYPE>
                void CArray<TYPE, ARG_TYPE>::SetAtGrow(int nIndex, ARG_TYPE newElement)
                {
                ASSERT_VALID(this);
                ASSERT(nIndex >= 0);

                if (nIndex >= m\_nSize)
                	SetSize(nIndex+1, -1);
                m\_pData\[nIndex\] = newElement;
                

                }

                The code is using a reference type (ARG_TYPE) to add the objects, that you are passing as unnamed temporaries (when are these being deleted?). Try something like this:

                void CMyDialog::SomeFunc1()
                {
                SomeStruct a(0,0);

                a = SomeStruct( 1, 2 );
                m_MrOdd.Add( a );

                a = SomeStruct( 10, 2 );
                m_MrOdd.Add( a );

                a = SomeStruct( 1, 212 );
                m_MrOdd.Add( a );
                }

                I know it looks odd, but try it. Regards, João Paulo Figueira Embedded MVP

                H Offline
                H Offline
                Hadi Rezaee
                wrote on last edited by
                #14

                I forgot to say that i got thease warnings in compiling: e:\windows ce tools\wce420\pocket pc 2003\mfc\include\afxtempl.h(65) : warning C4291: 'void *__cdecl operator new(unsigned int,void *)' : no matching operator delete found; memory will not be freed if initialization throws an exception e:\windows ce tools\wce420\pocket pc 2003\mfc\include\wcealt.h(235) : see declaration of 'new' e:\windows ce tools\wce420\pocket pc 2003\mfc\include\afxtempl.h(334) : see reference to function template instantiation 'void __cdecl ConstructElements(class OrderItems *,int)' being compiled OrderListDialog.cpp e:\windows ce tools\wce420\pocket pc 2003\mfc\include\afxtempl.h(65) : warning C4291: 'void *__cdecl operator new(unsigned int,void *)' : no matching operator delete found; memory will not be freed if initialization throws an exception e:\windows ce tools\wce420\pocket pc 2003\mfc\include\wcealt.h(235) : see declaration of 'new' e:\windows ce tools\wce420\pocket pc 2003\mfc\include\afxtempl.h(334) : see reference to function template instantiation 'void __cdecl ConstructElements(class OrderItems *,int)' being compiled ReportPage.cpp ShamsiDate.cpp Visitor.cpp e:\windows ce tools\wce420\pocket pc 2003\mfc\include\afxtempl.h(65) : warning C4291: 'void *__cdecl operator new(unsigned int,void *)' : no matching operator delete found; memory will not be freed if initialization throws an exception e:\windows ce tools\wce420\pocket pc 2003\mfc\include\wcealt.h(235) : see declaration of 'new' e:\windows ce tools\wce420\pocket pc 2003\mfc\include\afxtempl.h(334) : see reference to function template instantiation 'void __cdecl ConstructElements(class OrderItems *,int)' being compiled Generating Code... Linking...

                J 1 Reply Last reply
                0
                • J Joao Paulo Figueira

                  Man, this is maddening! What SDK are you using? Regards, João Paulo Figueira Embedded MVP

                  H Offline
                  H Offline
                  Hadi Rezaee
                  wrote on last edited by
                  #15

                  Yes I know but what can i say ? :( I'm using eVC++ 4.0 SP3, PPC 2003 SDK, WinCE 4.2 SDK is also installed. PS: I just stop the project, don't know what the hell is this :( Anyway thanks alot for your time ;) :rose: Regards, Hadi

                  1 Reply Last reply
                  0
                  • H Hadi Rezaee

                    I forgot to say that i got thease warnings in compiling: e:\windows ce tools\wce420\pocket pc 2003\mfc\include\afxtempl.h(65) : warning C4291: 'void *__cdecl operator new(unsigned int,void *)' : no matching operator delete found; memory will not be freed if initialization throws an exception e:\windows ce tools\wce420\pocket pc 2003\mfc\include\wcealt.h(235) : see declaration of 'new' e:\windows ce tools\wce420\pocket pc 2003\mfc\include\afxtempl.h(334) : see reference to function template instantiation 'void __cdecl ConstructElements(class OrderItems *,int)' being compiled OrderListDialog.cpp e:\windows ce tools\wce420\pocket pc 2003\mfc\include\afxtempl.h(65) : warning C4291: 'void *__cdecl operator new(unsigned int,void *)' : no matching operator delete found; memory will not be freed if initialization throws an exception e:\windows ce tools\wce420\pocket pc 2003\mfc\include\wcealt.h(235) : see declaration of 'new' e:\windows ce tools\wce420\pocket pc 2003\mfc\include\afxtempl.h(334) : see reference to function template instantiation 'void __cdecl ConstructElements(class OrderItems *,int)' being compiled ReportPage.cpp ShamsiDate.cpp Visitor.cpp e:\windows ce tools\wce420\pocket pc 2003\mfc\include\afxtempl.h(65) : warning C4291: 'void *__cdecl operator new(unsigned int,void *)' : no matching operator delete found; memory will not be freed if initialization throws an exception e:\windows ce tools\wce420\pocket pc 2003\mfc\include\wcealt.h(235) : see declaration of 'new' e:\windows ce tools\wce420\pocket pc 2003\mfc\include\afxtempl.h(334) : see reference to function template instantiation 'void __cdecl ConstructElements(class OrderItems *,int)' being compiled Generating Code... Linking...

                    J Offline
                    J Offline
                    Joao Paulo Figueira
                    wrote on last edited by
                    #16

                    This may well be the source of this problem. To diagnose it, I would have to take a look at your entire project. Regards, João Paulo Figueira Embedded MVP

                    H 2 Replies Last reply
                    0
                    • J Joao Paulo Figueira

                      This may well be the source of this problem. To diagnose it, I would have to take a look at your entire project. Regards, João Paulo Figueira Embedded MVP

                      H Offline
                      H Offline
                      Hadi Rezaee
                      wrote on last edited by
                      #17

                      Here's the story: class OrderItems { public: OrderItems(){} OrderItems(int code, int count) { m_nCode = code; m_nCount = count; } int m_nCode; int m_nCount; }; class CMainPage : public CPropertyPage { . . . void UseOrders(); // Where we should read items. public: CArray < OrderItems, OrderItems& > m_OrderItems; } And somewhere, in one of CMainPage methods I show another dialog and i add the items in that: void COrderListDialog::OnOK() { CMainPage* dlgMain = (CMainPage*) GetParent(); OrderItems item(0, 0); item = OrderItems( 10, 96 ); dlgMain->m_OrderItems.Add( item ); item = OrderItems( 5, 13 ); dlgMain->m_OrderItems.Add( item ); CDialog::OnOK(); } And then when i back to Main form i call UseOrders() to read the items but the problem occrues :( I hope it's clear ... Regards, Hadi

                      J 1 Reply Last reply
                      0
                      • J Joao Paulo Figueira

                        This may well be the source of this problem. To diagnose it, I would have to take a look at your entire project. Regards, João Paulo Figueira Embedded MVP

                        H Offline
                        H Offline
                        Hadi Rezaee
                        wrote on last edited by
                        #18

                        I did some new tests on the code that the results are strange too. 1- I declared m_OrderItems as a pointer variable and then use new, when i want to add items it shows me an error: "Out Of Memory.", But the program doesn't crash. 2- I used vector instead of CArray and i get same result. (No Item !) Regards, Hadi

                        1 Reply Last reply
                        0
                        • H Hadi Rezaee

                          Here's the story: class OrderItems { public: OrderItems(){} OrderItems(int code, int count) { m_nCode = code; m_nCount = count; } int m_nCode; int m_nCount; }; class CMainPage : public CPropertyPage { . . . void UseOrders(); // Where we should read items. public: CArray < OrderItems, OrderItems& > m_OrderItems; } And somewhere, in one of CMainPage methods I show another dialog and i add the items in that: void COrderListDialog::OnOK() { CMainPage* dlgMain = (CMainPage*) GetParent(); OrderItems item(0, 0); item = OrderItems( 10, 96 ); dlgMain->m_OrderItems.Add( item ); item = OrderItems( 5, 13 ); dlgMain->m_OrderItems.Add( item ); CDialog::OnOK(); } And then when i back to Main form i call UseOrders() to read the items but the problem occrues :( I hope it's clear ... Regards, Hadi

                          J Offline
                          J Offline
                          Joao Paulo Figueira
                          wrote on last edited by
                          #19

                          Please make sure that this:

                          CMainPage* dlgMain = (CMainPage*) GetParent();

                          does return the pointer you want. If not, all is explained... Regards, João Paulo Figueira Embedded MVP

                          H 1 Reply Last reply
                          0
                          • J Joao Paulo Figueira

                            Please make sure that this:

                            CMainPage* dlgMain = (CMainPage*) GetParent();

                            does return the pointer you want. If not, all is explained... Regards, João Paulo Figueira Embedded MVP

                            H Offline
                            H Offline
                            Hadi Rezaee
                            wrote on last edited by
                            #20

                            Yessss, That's it :-D You solved the problem ! Thank you very much ! :rose: I changed code to this: CAppSheet *pAppSheet = (CAppSheet *) GetParent(); CMainPage *dlgMain = (CMainPage *) pAppSheet->GetPage(0); And now it works fine ... Best Regards, Hadi

                            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