From: Eric Lilja on
MrAsm wrote:
> On Mon, 21 May 2007 11:38:25 +0200, Eric Lilja
> <mindcoolerremoveme(a)gmail.com> wrote:
>
>> Just be a bit careful with operator [] for std::map. If there's no
>> element with the given key a new, default-constructed one, is inserted
>> and a reference to it is returned. And in some cases you just want to
>> know if an element is in the map without actually inserting as part of
>> that check and then you use find().
>
> Yes, I know both this aspect of operator[] and find() method.
> But I was not going deep into the discussion because I saw no check
> for existing/non-existing elements with a given key in the OP's code.
>
> Of course, you're right that if the OP needs to check if a key is
> already in the map, he could use find method:
>
> if ( data.find( key ) != data.end() )
> // ... key already in the map
>

I was pointing it out as a FYI to the OP even though it was a reply to
your post. :) It's easy to perform involuntary inserts if one doesn't
know how operator[] works. I've never used CMap myself, so I couldn't
comment on it. The standard map works just fine for me.

> MrAsm
From: MrAsm on
On Mon, 21 May 2007 12:10:37 +0200, Eric Lilja
<mindcoolerremoveme(a)gmail.com> wrote:

>It's easy to perform involuntary inserts if one doesn't
>know how operator[] works.

Yes, you're right :)

And I think that it is always better to post more information than
less :)

>I've never used CMap myself, so I couldn't
>comment on it. The standard map works just fine for me.

I started with MFC collections in VC6, then I moved to STL collections
(but I'm not an STL guru).

MrAsm
From: Joseph M. Newcomer on
It is entirely possible a data overrun or bad pointer usage has clobbered the structure,
so CMap is merely an innocent bystander that happens to detect some other error.
joe

On Mon, 21 May 2007 09:27:19 GMT, MrAsm <mrasm(a)usa.com> wrote:

>On Mon, 21 May 2007 08:53:47 +0200, Anders Eriksson
><andis59(a)gmail.com> wrote:
>
>>I'm using a CMap<int,int,CString,LPCTSTR> for storing error messages which
>>I get from a PLC as error codes. This works very well, except that when I
>>exit the program it crashes in CPlex::FreeDataChain() with this error
>>message:
>>
>>Unhandled exception at 0x78356c6e (mfc80ud.dll) in Marker.exe: 0xC0000005:
>>Access violation reading location 0x00000002.
>
>Another "plan of attack" to your problem could also be to use STL
>std::map instead of CMap.
>
>std::map is very easy to use (at least, its basic use), e.g.:
>
> //
> // Map int to CString
> // (note that you can mix STL std::map with MFC CString)
> //
> typedef std::map< int, CString > IntStringMap;
> IntStringMap data;
>
> // Add some data into the map; very easy with the
> // operator[] overload
> data[3] = _T("Hello");
> data[10] = _T("All right");
>
> // You can retrieve map data also using operator[]:
> Print( data[3] ); // Hello
> Print( data[10] ); // All right
>
> // Remove map data using map::clear method
> data.clear();
>
>You might consider refactoring your code using std::map and see if the
>program crashes again.
>
>However, I believe that the crash is not CMap's fault, but it's a
>fault of other part of the code (even if it seems not clear basing on
>the C++ statements you posted.)
>
>MrAsm
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Anders Eriksson on
On Mon, 21 May 2007 07:36:08 -0400, Joseph M. Newcomer wrote:

> It is entirely possible a data overrun or bad pointer usage has clobbered the structure,
> so CMap is merely an innocent bystander that happens to detect some other error.
> joe
>
> On Mon, 21 May 2007 09:27:19 GMT, MrAsm <mrasm(a)usa.com> wrote:
>
>>However, I believe that the crash is not CMap's fault, but it's a
>>fault of other part of the code (even if it seems not clear basing on
>>the C++ statements you posted.)

Hmmm... I have not thought about that! This makes more sense, since I'm
pretty sure I using the CMap correctly.

It should be possible to use Boundschecker to find where the overrun
happens. I wonder how (I have just bought it)...

Thank you for your answers!

// Anders
--
English is not my first, or second, language
so anything strange, or insulting, is due to
the translation.
Please correct me so I may improve my English!
From: Joseph M. Newcomer on
Application Verifier (free from Microsoft) would probably have done the job.
joe

On Tue, 22 May 2007 07:05:34 +0200, Anders Eriksson <andis59(a)gmail.com> wrote:

>On Mon, 21 May 2007 07:36:08 -0400, Joseph M. Newcomer wrote:
>
>> It is entirely possible a data overrun or bad pointer usage has clobbered the structure,
>> so CMap is merely an innocent bystander that happens to detect some other error.
>> joe
>>
>> On Mon, 21 May 2007 09:27:19 GMT, MrAsm <mrasm(a)usa.com> wrote:
>>
>>>However, I believe that the crash is not CMap's fault, but it's a
>>>fault of other part of the code (even if it seems not clear basing on
>>>the C++ statements you posted.)
>
>Hmmm... I have not thought about that! This makes more sense, since I'm
>pretty sure I using the CMap correctly.
>
>It should be possible to use Boundschecker to find where the overrun
>happens. I wonder how (I have just bought it)...
>
>Thank you for your answers!
>
>// Anders
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm