Re: Problem with CMapStringToPtr 's Lookup problem
- From: "Heinz Ozwirk" <hozwirk.SPAM@xxxxxxxx>
- Date: Wed, 21 Jun 2006 22:34:21 +0200
"Prasad" <prasadk14@xxxxxxxxx> schrieb im Newsbeitrag news:1150779844.921715.41620@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CMapStringToPtr chat;
vector<UserMessage *> v;
UserMessage *obj_msg=new UserMessage("prasad","Prasad",'n',"Hi
how r u ?");
void * rValue;
CString toUser="prasad";
v.push_back(obj_msg);
chat.SetAt(toUser,&v); /// storing the vector's reference in
Map variable "chat"
chat.Lookup(toUser,rValue); //getting the vector's refrence
from Map
vector<UserMessage *> *vec=((vector<UserMessage *> *)rValue);
if(!vec->empty())
{
UserMessage *u=vec->at(0);
cout<<(LPCSTR)u->msg<<endl;
}
else
{
printf("Vector is empty");
}
}
return nRetCode;
}
this is working as I want it to be,,,(displays the msg "Hi how r u ?"
on console)
But If i insert the vector 's reference in one block of code
UserMessage *obj_msg=new UserMessage("prasad","Prasad",'n',"Hi how r
u ?");
void * rValue;
CString toUser="prasad";
v.push_back(obj_msg);
chat.SetAt(toUser,&v); /// storing the vector's reference in
Map variable "chat"
,and get the vector's reference in another block of code like
chat.Lookup(toUser,rValue); //getting the vector's refrence from Map
vector<UserMessage *> *vec=((vector<UserMessage *> *)rValue);
if(!vec->empty())
{
}
else
{
printf("Vector is empty");
}
, ( map variable 'chat' is global ) , always its been dispalyed that
"Vector is empty"..
In the working code, the vector object (v) exists as long as the map, so there is no problem. But when you insert the vector's address in one block you must also read it in the same block. Once the execution path leaves a block, all variables defined in that block run out of scope and will be destroyed. All pointers to such variables are invalid and your program's behaviour will be undefined.
Here I am storing the vector's reference into map in first block of
code and trying to reference the vector in second block...
Wll the vector object created in the first block be lost as it comes
out of the scope?
If your definition of block matches mine (a compound statement or the body of some function) -- yes.
If so, how can i solve this problem?
Don't store pointers to local varaibles longer than the variable exists. Use new to allocate memory for such data, or put an instance of such objects into your map instead of a pointer. You can do that with an std::map, but you cannot do it wirh a CMap...ToPtr. If you don't want or can use STL, use CMap<>, but better forget about all MFC containers that can only store pointers.
1) Think about using std::map instead of CMapStringToPtr. All those void*Is std::map available in predefined library or STL?
used in some of MFC's container classes may cause many problems.
Initially ..i used map template from STL ,but if i include the
<hash_map> header file in my project , its conflicting with some other
header file which i used for thread template defintion( ou_thread.h
using namespace openutils )
if i include these two in a single project ,its giving compile errors..
Thats why i shifted to Mfc classes support for CMapStringToPtr class
std::map is part of the STL. it is defined in <map>. I don't know about ou_thread.h or openutils, but IIRC it is neither part of C++ (including STL) nor of any Microsoft SDK. If it doesn't work with standard C++, you should think about replacing it with a better library.
2) Don't use vectors of pointers unless you really have to. And if you haveI really need pointer to vector to be stored in map (as i thought i
to use vectors of pointers, think about using some smart pointer.
could access the vector in another block of code) and also vector of
pointers (UserMessage *)..
How to use smart pointers here?
3) NEVER put a pointer to an object on the stack into a collection (unless_____ No answer ..:-) ______
you really know what you are doing).
Think about it. It might be your problem. But without seeing a not-working example of your code, I can only guess.
Heinz
.
- References:
- Problem with CMapStringToPtr 's Lookup problem
- From: Prasad
- Re: Problem with CMapStringToPtr 's Lookup problem
- From: Heinz Ozwirk
- Re: Problem with CMapStringToPtr 's Lookup problem
- From: Prasad
- Problem with CMapStringToPtr 's Lookup problem
- Prev by Date: Re: stop Thread problem
- Next by Date: Re: class unable to write the memory
- Previous by thread: Re: Problem with CMapStringToPtr 's Lookup problem
- Next by thread: Problem with CMemDC and CScrollView
- Index(es):
Relevant Pages
|