Thread: Extremely odd Serialization error

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    Extremely odd Serialization error



    This one has me stumped big time. I'm in the process of serializing all my dialogs and objects to disk and everything was going well until.....attempting to serialize the layers dialog information to disk.

    I have a layers dialog that tells you how many tile maps you have in the final map. The actual list box has nothing to do with the class that manages the maps except that when you click add, it tells the map manager to add a map. The map size is determined by the options dialog data members which is a member of the main frame window.

    Now when I only have 2 layers, the movement layer (always layer 0) and one draw layer (layer 1) it works fine. When you add a layer and then save it all goes to hell.

    For some reason this code is not working right when more than 2 layers are available.

    Code:
    void LayersDlg::Serialize(CArchive &ar) 
    {
       if (ar.IsStoring())
      {	
        // storing code
    
        DWORD size=LayerInfo.size();
    
        CString text;
        text.Format("NumLayers: %u",size);
        ::MessageBox(0,text,0,0);
    
        ar << size;
    
      }
        else
      {	
        // loading code
    
        DWORD temp=0;
        ar >> temp;
    
        CString text;
        text.Format("NumLayers in file: %u",temp);
        ::MessageBox(0,text,0,0);
    
    
        for (DWORD i=0;i<temp;i++)
        {
          if (i>1) AddLayer();
        }
    
      }
    
    }
    Now get this. Even when you add a map and save, the dialog box in the storing section prints out the correct value. When you then attempt to load the same file, the temp variable in the loading section prints out the horizontal size of the map, which happens to be the first piece of data in the entire project file.
    I cannot for the life of me see where the CArchive is being rest to the front of the file.

    And it is not writing the correct value. I can start from scratch with a fresh compile and the number does not change. So why is the storing section writing a totally different number than is being displayed and why does it only work with 2 layers?

    Seems I may have a serious memory problem here.

    Anyone please help.

    Add button code:
    Code:
    void LayersDlg::OnButtonAdd() 
    {
      CMainFrame *ptrFrame=(CMainFrame *)AfxGetMainWnd();
      CZeldaEditorDoc *ptrDoc=(CZeldaEditorDoc *)ptrFrame->GetActiveDocument();
    
      ptrDoc->m_pMapManager->Add();
        
      AddLayer();
    
    }
    Add layer code:
    This can be called from anywhere, even in setup. This is why the count is checked. The SetupProject() function in the main window calls this to add layers for the user. By default when the project starts, it has a move layer and one draw layer.
    Code:
    void LayersDlg::AddLayer(void)
    {
      int count=m_lstLayers.GetCount();
    
      CString text;
      if (count==1)
      {
        text="Movement";
    
      }
      else
      {
        text.Format("Layer %d",count-1);
      }
    
      LayerData *data=new LayerData;
      data->dwLayerNum=count-1;
    
      data->strName=text;
    
      //CString test;
      //test.Format("Vector size: %d",LayerInfo.size());
      //::MessageBox(0,test,0,0);
    
    
      LayerInfo.push_back(data);
      m_dwNumLayers=LayerInfo.size();
    
      m_lstLayers.InsertString(count,text);
    
      
    }
    And the map manager add code:
    Code:
    DWORD Add(void)
    {
        CMap *map=new CMap();
        map->Create(m_dwWidth,m_dwHeight);
        map->Clear(0xFFFFFFFF);
    
        MapLayers.push_back(map);
        return MapLayers.size()-1;
        }
    Map create and clear code:
    Code:
    bool Create(DWORD dwWidth,DWORD dwHeight)
    {
        m_dwSize=dwWidth*dwHeight;
        m_pMap=new DWORD[m_dwSize];
    
        //return non-zero on failure
        if (!m_pMap) return true;
    
        m_dwWidth=dwWidth;
        m_dwHeight=dwHeight;
    
        return false;
    }
      
    void Clear(DWORD value=0)
    {
      DWORD *ptrMap=m_pMap;
      DWORD dwSize=m_dwSize;
    
      _asm {
        mov   edi,[ptrMap]
        mov   ecx,[dwSize]
        mov   eax,[value]
        rep   stosd
        and   ecx,03h
        rep   stosb
      }
    }
    Sorry so long but this editor is really really big and a lot of the classes, windows, objects, and functionality of it is extremely reliant on other object, classes, windows, etc.
    Last edited by VirtualAce; 01-02-2006 at 06:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  5. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM