CStringArray reference
-
I have a problem into gather information into CStringArray reference : first , I define a CMap object :
CMap m_mapType;
and I insert a new element :
CString sTemp = \_T("test value"); CStringArray\* psaTest = new CStringArray; psaTest->Add(sTemp); m\_mapType\[\_T("index")\] = psaTest; psaTest->RemoveAll(); delete psaTest;
and when I try to read this element :
CStringArray\* psaRes = new CStringArray;; if(m\_mapType.Lookup(\_T("index"),psaRes))AfxMessageBox("Found index"); else AfxMessageBox("Not found"); if(psaRes->GetSize())AfxMessageBox(psaRes->GetAt(0)); else AfxMessageBox("Empty psaRes"); delete psaRes;
my code says that I found CMap element , but psaRes is empty , why ? Because I put wrong CStringArray reference in Lookup method ? Thank you . P.S. I put here entire code , because I try above code in the same place :
CString sTemp = \_T("test value"); CStringArray\* psaTest = new CStringArray; psaTest->Add(sTemp); m\_mapType\[\_T("index")\] = psaTest; psaTest->RemoveAll(); delete psaTest; CStringArray\* psaRes = new CStringArray;; if(m\_mapType.Lookup(\_T("index"),psaRes))AfxMessageBox("Found index"); else AfxMessageBox("Not found"); if(psaRes->GetSize())AfxMessageBox(psaRes->GetAt(0)); else AfxMessageBox("Empty psaRes"); delete psaRes;
-
I have a problem into gather information into CStringArray reference : first , I define a CMap object :
CMap m_mapType;
and I insert a new element :
CString sTemp = \_T("test value"); CStringArray\* psaTest = new CStringArray; psaTest->Add(sTemp); m\_mapType\[\_T("index")\] = psaTest; psaTest->RemoveAll(); delete psaTest;
and when I try to read this element :
CStringArray\* psaRes = new CStringArray;; if(m\_mapType.Lookup(\_T("index"),psaRes))AfxMessageBox("Found index"); else AfxMessageBox("Not found"); if(psaRes->GetSize())AfxMessageBox(psaRes->GetAt(0)); else AfxMessageBox("Empty psaRes"); delete psaRes;
my code says that I found CMap element , but psaRes is empty , why ? Because I put wrong CStringArray reference in Lookup method ? Thank you . P.S. I put here entire code , because I try above code in the same place :
CString sTemp = \_T("test value"); CStringArray\* psaTest = new CStringArray; psaTest->Add(sTemp); m\_mapType\[\_T("index")\] = psaTest; psaTest->RemoveAll(); delete psaTest; CStringArray\* psaRes = new CStringArray;; if(m\_mapType.Lookup(\_T("index"),psaRes))AfxMessageBox("Found index"); else AfxMessageBox("Not found"); if(psaRes->GetSize())AfxMessageBox(psaRes->GetAt(0)); else AfxMessageBox("Empty psaRes"); delete psaRes;
Your CMap holds the pointers to CStringArray. You should enure that those pointers are valid till you retrieve them from map. Here you are deleting CStringArray pointers after inserting them to map. Obviously you will not get what you actually inserted. Delete the pointers only when the map is no longer needed.
-
I have a problem into gather information into CStringArray reference : first , I define a CMap object :
CMap m_mapType;
and I insert a new element :
CString sTemp = \_T("test value"); CStringArray\* psaTest = new CStringArray; psaTest->Add(sTemp); m\_mapType\[\_T("index")\] = psaTest; psaTest->RemoveAll(); delete psaTest;
and when I try to read this element :
CStringArray\* psaRes = new CStringArray;; if(m\_mapType.Lookup(\_T("index"),psaRes))AfxMessageBox("Found index"); else AfxMessageBox("Not found"); if(psaRes->GetSize())AfxMessageBox(psaRes->GetAt(0)); else AfxMessageBox("Empty psaRes"); delete psaRes;
my code says that I found CMap element , but psaRes is empty , why ? Because I put wrong CStringArray reference in Lookup method ? Thank you . P.S. I put here entire code , because I try above code in the same place :
CString sTemp = \_T("test value"); CStringArray\* psaTest = new CStringArray; psaTest->Add(sTemp); m\_mapType\[\_T("index")\] = psaTest; psaTest->RemoveAll(); delete psaTest; CStringArray\* psaRes = new CStringArray;; if(m\_mapType.Lookup(\_T("index"),psaRes))AfxMessageBox("Found index"); else AfxMessageBox("Not found"); if(psaRes->GetSize())AfxMessageBox(psaRes->GetAt(0)); else AfxMessageBox("Empty psaRes"); delete psaRes;
You are putting a pointer to a CStringArray in your map, which means that it always points to the same instance of CStringArray that you were originally using (it's not a copy). And you do something like this:
psaTest->RemoveAll();
just after adding the pointer to the map. So, your CStringArray will be empty. By the way, you shouldn't do something like this:
CStringArray* psaRes = new CStringArray;
...
...
m_mapType.Lookup(_T("index"),psaRes);Because in that case you will have a memory leak. There's no need to make a new CStringArray: if the pointer is found in the map, it will be copied into the psaRes pointer (the address will be copied, not the content of course). BTW, I suggest you take a look at the STL containers (map, list, ...) they are much easier to use (once you are confortable with the syntax).
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
Your CMap holds the pointers to CStringArray. You should enure that those pointers are valid till you retrieve them from map. Here you are deleting CStringArray pointers after inserting them to map. Obviously you will not get what you actually inserted. Delete the pointers only when the map is no longer needed.
-
You are putting a pointer to a CStringArray in your map, which means that it always points to the same instance of CStringArray that you were originally using (it's not a copy). And you do something like this:
psaTest->RemoveAll();
just after adding the pointer to the map. So, your CStringArray will be empty. By the way, you shouldn't do something like this:
CStringArray* psaRes = new CStringArray;
...
...
m_mapType.Lookup(_T("index"),psaRes);Because in that case you will have a memory leak. There's no need to make a new CStringArray: if the pointer is found in the map, it will be copied into the psaRes pointer (the address will be copied, not the content of course). BTW, I suggest you take a look at the STL containers (map, list, ...) they are much easier to use (once you are confortable with the syntax).
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
Let's say that somewhere I serialize the map object , how can I retrieve CStringArray values ? Because there I simulate something like that.
CMap is derived from CObject, thereby Serialize method is there. I am not pretty sure what will happen if the map members are pointers, i mean whether the data it points to get serialized or the pointer itself. However its just a matter of a test app to verify the case. :)