Re: Problem with CMapStringToPtr 's Lookup problem



"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*
used in some of MFC's container classes may cause many problems.

Is std::map available in predefined library or STL?
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 have
to use vectors of pointers, think about using some smart pointer.

I really need pointer to vector to be stored in map (as i thought i
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
you really know what you are doing).

_____ No answer ..:-) ______

Think about it. It might be your problem. But without seeing a not-working example of your code, I can only guess.

Heinz

.



Relevant Pages

  • Re: Problem with CMapStringToPtr s Lookup problem
    ... CMapStringToPtr chat; ... UserMessage is a class.. ... map "chat" ... to use vectors of pointers, think about using some smart pointer. ...
    (microsoft.public.vc.mfc)
  • Re: using map, list etc. in const methods
    ... and you need to make sure that the map doesn't hold ... how are your pointers allocated and deleted? ... need to read a couple of good books that deal with memory management issues. ... At the moment I'm just using lists, ...
    (comp.lang.cpp)
  • Re: Umbrarum Regnum
    ... looks like the basic idea in your current framework is that pointers are ... The game was actually ... Therefore it would be easy to add a "rewind time" feature and I'm ... so that the player doesn't even see the map ...
    (rec.games.roguelike.development)
  • Re: Strategies for avoiding new?
    ... > container is important (which is why I use map or whatever.) ... from the container and manipulate it directly. ... So after my snippet, ... using pointers is unnecessary. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: map.find doesnt find
    ... Basically the map.findmethod fails to find a match for a key that I ... The method is comparing pointers to determine if the objects are ... In my case the map key is of type const wchar_t* so this is ... Key type is a pointer so mapcompares those pointers, ...
    (microsoft.public.vc.stl)