|
I'm missing something totally fundamental here but have yet to figure it out. I've got a class called JOBINFO . I create instances of it on the heap and add those pointers to a map. When I need to reuse the map, I call its RemoveAll() method. Internally, that method is supposed to call the overridden DestructElements() which then calls each object's destructor. That is not happening.
class JOBINFO
{
public:
JOBINFO()
{
TRACE("Constructor\n");
}
~JOBINFO()
{
TRACE("Destructor\n");
}
};
...
void AFXAPI DestructElements( JOBINFO* pJobInfo, int nCount )
{
TRACE("DestructElements()\n");
}
...
CMap<int, int, JOBINFO*, JOBINFO*> m_mapJobInfo;
...
m_mapJobInfo.SetAt(123, new JOBINFO);
...
m_mapJobInfo.RemoveAll(); Any ideas?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
RemoveAll doesn't actually delete objects - it might call the d'tor on the pointer, but that's a no-op.
you have to delete the objects manually.
MSDN discussion[^]
|
|
|
|
|
Chris Losinger wrote: RemoveAll doesn't actually delete objects...
I know that. It internally calls DestructElements() which then calls each object's destructor. I'm trying to figure out why neither is getting called in my case.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
DavidCrow wrote: It internally calls DestructElements()
not in the version of MFC that comes with VS05:
afxtempl.h
template < class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
void CMap < KEY, ARG_KEY, VALUE, ARG_VALUE >::RemoveAll()
{
ASSERT_VALID(this);
if (m_pHashTable != NULL)
{
for (UINT nHash = 0; nHash < m_nHashTableSize; nHash++)
{
CAssoc* pAssoc;
for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL;
pAssoc = pAssoc->pNext)
{
pAssoc->CAssoc::~CAssoc();
}
}
}
...
VS08 has those two lines commented-out, as well.
|
|
|
|
|
Wow! In VS6, those two lines are not commented out. I guess that MSDN is wrong when it states:
Removes all the values from this map by calling the global helper function DestructElements.
So, is there no longer a way for the framework to assist in freeing heap objects from a map? I can work around this by deriving a class from CMap and implementing a "cleanup" method that I can call instead of RemoveAll() , but didn't want to unless I had to.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
that really is odd. wonder why they'd do that...
|
|
|
|
|
DavidCrow wrote: So, is there no longer a way for the framework to assist in freeing heap objects from a map?
Do you know any library with the maps,
that delete given pointers by default ?
You could define a map with CString values
and you would get their destructors at removing.
An "overwritten" DestructElements(..) function has no role,
elsewise we could not instance a map or - at least - call RemoveAll() ,
without to must implement it always
virtual void BeHappy() = 0;
|
|
|
|
|
Eugen Podsypalnikov wrote: Do you know any library with the maps,
that delete given pointers by default ?
I was not looking to have the map delete the pointers.
Eugen Podsypalnikov wrote: An "overwritten" DestructElements(..) function has no role,
elsewise we could not instance a map or - at least - call RemoveAll(),
without to must implement it always
Try as I might, I can make no sense of this.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Eugen Podsypalnikov wrote: Do you know any library with the maps,
that delete given pointers by default ? Smile
std::map with boost::shared_ptr comes in mind (or any STL compatible auto pointer)
/M
|
|
|
|
|
Good point !
This[^] class could provide a solution for David :
class Info
{
public:
Info() {};
~Info() {};
...
};
...
CMap <int,int, CAutoPtr<Info>, CAutoPtr<Info>> mapInfos;
...
mapInfos.SetAt(123, CAutoPtr<Info>(new Info));
...
mapInfos.RemoveAll();
virtual void BeHappy() = 0;
|
|
|
|
|
DavidCrow wrote: So, is there no longer a way for the framework to assist in freeing heap objects from a map?
No more, unless you wrap your pointers (if I understood correctly [^] and [^]).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
m_mapJobInfo.SetAt(123, new JOBINFO);
You are calling constructor: JOBINFO*, not JOBINFO.
It is important to understand the difference.
Make the map as:
CMap<int, int, JOBINFO, JOBINFO&> m_mapJobInfo;
And the DTOR will be called.
|
|
|
|
|
Hai friends,
i'm working with client server model socket programing using VC++.
it has MFC SOCKET in client side and WIN32 SOCKET in server console application.
i'm having proplem with receiving the strings in server side.
i displayed part of code here
Client Side Coding:
UpdateData(1);
CSocket client;
client.Create ();
CString sendstr;
sendstr="Hello";
client.Connect ("127.0.0.1",2500);
int len=sendstr.GetLength ();
int sent= client.Send(LPCTSTR(sendstr),len);
UpdateData(0);
Server side coding:
char recvbuf[32]="" ;
int bytesRecv = recv( server, recvbuf, 32, 0 );
printf( "Bytes Recv: %d\n", bytesRecv );
recvbuf[bytesRecv]=NULL;
printf("%S",recvbuf);
if i run the code i get 5 in bytesRecv correctly..
but in recvbuf buffer three letters only available... like "hel"
please help me l'm waiting for u...
|
|
|
|
|
Your string is not UNICODE, why do you want to print it as such?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Are you using TCP ?
If yes, put the flag TCP_NODELAY.
It's a classic error issue, in fact many people are confused by TCP.
It's a simple example but with the TCP_NODELAY flag you should be able to receive your 5 bytes at once.
[^]
|
|
|
|
|
Your client side project is UNICODE. Either select MBCS in project settings, or
CStringA sendstr;<br />
sendstr="Hello";<br />
client.Connect ("127.0.0.1",2500);<br />
int len=sendstr.GetLength ();<br />
int sent= client.Send( LPCSTR(sendstr),len);
CString is macro, which translates to CStringA or CStringW. You can assign either Unicode or ANSI string, CStringW/CStringA will take care of that.
|
|
|
|
|
hai,
I want to get notified of any process (E.g. mstsc.exe ) at the time or its starting.
How can I do it.
Please provide some useful hints.
-Cvaji
|
|
|
|
|
Cvaji wrote: How can I do it.
Please provide some useful hints.
See here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Hello,
i try to open a file with CStdioFile but fails any time when i try!
I receive an error 11, and GetErrorMessage returns a "File Error > (11) "A Sharing Violation Occurred ...""
This is when i use VS2008, but the same function and same file with VS2005 works normal, without any errors!
CStdioFile pStdFileLocal;
if(!pStdFileLocal.Open(csFilePath, CFile::modeRead, &e))
{
}
Is there any change in VS2008 with files and file functions???
Thanks for any help!
Arrin
|
|
|
|
|
did you try modShareDeny??
modified on Friday, April 9, 2010 11:37 AM
|
|
|
|
|
Hi,
sorry is my mistake
i have already open that file before and did n close the file handle
thanks for answer
Arrin
|
|
|
|
|
All the processes reading the same file, must open in CFile::shareDenyNone mode.
|
|
|
|
|
Hi,
sorry is my mistake
i have already open that file before and did n close the file handle
thanks for answer
Arrin
|
|
|
|
|
|
You mean you can step through the entire code but running it doesn't work?
-Saurabh
|
|
|
|