From: Ajay Kalra on
> I built it into a sandbox (_MBCS) and it worked OK for me; that doesn't mean
> it's OK for her though... I could try it in UNICODE if needed later.

I would assume that OP must have some special parameters that causes
failure on this line(according to OP). My guess is that problem is
elsewhere and it just happens to show up at this spot. CString is
pretty well used and tested.

---
Ajay

From: Joseph M. Newcomer on
As Ed indicates, using malloc is like thinking that programming in assembly code is
useful. In fact, I wouldn't do it; I would call GetBuffer on a CString to get the space I
needed, thus eliminating the need to worry about deallocation.

I can't find how the bug would manifest itself, especially for small integers.

There is no need to multiply by sizeof(TCHAR) if you do GetBuffer, because GetBuffer is
always in terms of TCHARs.
joe


On Tue, 22 Aug 2006 18:49:51 -0700, "Ed Weir \(ComCast\)" <Anon(a)Maus.duh> wrote:

>"Jen" <leonard522(a)aol.com> wrote in message
>news:1156292545.979110.54370(a)74g2000cwt.googlegroups.com...
>|
>| Ed,
>| I am using VS6, so this definitely sounds like it could be a possible
>| cause of my trouble. However, I am not sure how to implement it.
>|
>| First I need to malloc a buffer. Then sprintf into it, and then add it
>| to the CString. Is that correct? I have a few places where I directly
>| assign text into the CString. Would I keep that as is, and only modify
>| those .Format commands containing %d?
>|
>| Thanks for the help.
>| Jen
>
>The bug in the strex parser is very obscure. Because of that, it is only
>necessary to implement the workaround where .Format is actually causing a
>problem. As I mentioned in my last reply to Joe, use _stprintf to do the
>work and new TCHAR [nSize]/delete for memory blocks as needed. Another
>option is to use GetBuffer/ReleaseBuffer members of CString (ref: see the
>MSDN help on CString..) to get the memory to _stprintf(pBuffer, pszFormat,
>...) your formatted text into. Keep in mind that if you specify a memory
>buffer size for text you should use sizeof(TCHAR) as the multiplier for the
>size unless you use new to allocate the memory. Hope I haven't muddled it
>all up too badly for you... 8^)
>
>If so, I will be glad to give an example for you:
>
>CString str;
>LPTSTR pBuffer = str.GetBuffer(nSize * sizeof(TCHAR)); // or 8192...
>_stprintf(pBuffer, _T("String data format stuff %d yada yada%ul etc.") ...
>str.ReleaseBuffer();
>
>- and you're good to go. Let me know if this solves the problem!
>
>-- Ed.
>
>-----------------------------------------------------
>In dictatorships, you need courage to fight evil; in the free world, you
>need courage to see the evil."
>?Natan Sharansky
>
>F9E7707A2AF502D0A899C6ACB43A2D35EB7E->bin->b64
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Joseph M. Newcomer on
This is odd, because the way CStrings work, in debug mode, the minimum number of bytes is
allocated, whereas in release mode, CStrings are quantized to 64, 128, 256, or 512 bytes,
or "anything else bigger", which means memory clobbers are *more* likely in Debug than in
Release mode.

Have you tried stepping through the CString::Format code to see if it really *is* doing
something bad in your case?
joe

On 22 Aug 2006 17:18:05 -0700, "Jen" <leonard522(a)aol.com> wrote:

>Joseph ~
>Thank you for your advice. I have read several of your articles and
>found them very helpful. Unfortunately I still haven't tracked this
>problem down. After reading your articles, I realized there may be a
>key to solving this problem. The program works fine in Debug mode, and
>only crashes in Release mode. I tried the checking the Heap in several
>places including OnIdle in the debug version, and everything worked
>okay.
>
>Does knowing that give you any other things for me to try? I did turn
>off all optimizations and Rebuild All, but the program still crashed at
>the end. I didn't turn on the debug in release mode, because I didn't
>know where to try to debug since it crashes at the end.
>
>Thanks again!!
>Jennifer
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Joseph M. Newcomer on
Given how much I use CString::Format, I should have hit this bug years ago, but I've never
seen it. I suspect that there is memory damage elsewhere which is not being caught in the
debug mode and which the allocation pattern is sensitive enough that it shows up.

I truly hate memory damage bugs, which is why I've more and more adopted ways to avoid
ever having them. They rank among the most miserable bugs to track down.
joe

On 22 Aug 2006 19:07:25 -0700, "Ajay Kalra" <ajaykalra(a)yahoo.com> wrote:

>> I built it into a sandbox (_MBCS) and it worked OK for me; that doesn't mean
>> it's OK for her though... I could try it in UNICODE if needed later.
>
>I would assume that OP must have some special parameters that causes
>failure on this line(according to OP). My guess is that problem is
>elsewhere and it just happens to show up at this spot. CString is
>pretty well used and tested.
>
>---
>Ajay
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Ed Weir (ComCast) on
"Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message
news:v2mne293slei1fmvg1civ2o0c3rpju45jp(a)4ax.com...
| Given how much I use CString::Format, I should have hit this bug years
ago, but I've never
| seen it. I suspect that there is memory damage elsewhere which is not
being caught in the
| debug mode and which the allocation pattern is sensitive enough that it
shows up.
|
| I truly hate memory damage bugs, which is why I've more and more adopted
ways to avoid
| ever having them. They rank among the most miserable bugs to track down.
| joe

This bug was logged and repro'd by a lab tester some time ago - way back in
<spit> '97 or so by crackie (sure doesn't seem that long ago!). Looking at
the parser, I couldn't see anything mentioning a bug fix there. Sometimes,
you just have to avoid the ditch and get on with it. I never delved into
the GetBuffer routine - guess I should have assumed it would get the size
right... don't have time to do EVERYTHING!! After all.
- Ed.

| On 22 Aug 2006 19:07:25 -0700, "Ajay Kalra" <ajaykalra(a)yahoo.com> wrote:
|
| >> I built it into a sandbox (_MBCS) and it worked OK for me; that doesn't
mean
| >> it's OK for her though... I could try it in UNICODE if needed later.
| >
| >I would assume that OP must have some special parameters that causes
| >failure on this line(according to OP). My guess is that problem is
| >elsewhere and it just happens to show up at this spot. CString is
| >pretty well used and tested.
| >
| >---
| >Ajay
| Joseph M. Newcomer [MVP]
| email: newcomer(a)flounder.com
| Web: http://www.flounder.com
| MVP Tips: http://www.flounder.com/mvp_tips.htm