CString::FormatMessage can throw out of memory exception - by Michail Frolov

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 699739 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 11/7/2011 9:45:34 PM
Access Restriction Public
Moderator Decision Sent to Engineering Team for consideration


The fix to the problem described in 


has caused another problem.

The new code simply checks GetLastError() return value without checking for return value of FormatMessage. As a result this function can (and has for us) throw out of memory exception when there is actually no problem and a formatted string is successfully returned in the supplied buffer.

I am guessing some internal Windows API sets an error during FormatMessage execution.

The correct way of fixing this would be to check for return value of FormatMessage and if it is 0 then check GetLastError() return value and only then throw an error.
Sign in to post a comment.
Posted by Microsoft on 12/6/2011 at 11:19 AM
Hello Michail,

Thanks for the report. This issue has been fixed in ATL for the next major release of Visual Studio.

Pat Brenner
Visual C++ Libraries Development
Posted by Michail Frolov on 12/5/2011 at 8:50 AM
We have determined why FormatMessage was failing for us. Our function that hooks HeapReAlloc was setting last error. This last error was used by the ATL implementation of FormatMessage even when the return code of FormatMessage indicated success.
Posted by Microsoft on 12/4/2011 at 11:46 PM
Thanks for your feedback.
We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Michail Frolov on 12/4/2011 at 1:55 AM
Unfortunately I am unable to reproduce the problem in a small new project. It only happens in our application. I would have to debug our application to see what is setting the last error before the function returns.
Posted by Microsoft on 11/25/2011 at 12:51 AM

Sorry for bothering. Is there any update?

It would be greatly appreciated if you could provide us with that information as quickly as possible.

Thanks you
Posted by Microsoft on 11/16/2011 at 7:32 PM
Hi Michail Frolov,

Unfortunately, we are unable to reproduce the issue with the steps you provided.

Could you please provide us with a sample project zip for further investigating?

Thanks again for your efforts and we look forward to hearing from you.
Posted by Michail Frolov on 11/10/2011 at 12:06 PM
CString str;
str.FormatMessage(IDS_FORMAT_STRING, pszData);

IDS_FORMAT_STRING corresponds to "New Logon for %1"
pszData is LPTSTR pointing to a string value that changes depending on context and is never an empty string.
Posted by MS-Moderator09 [Feedback Moderator] on 11/9/2011 at 11:34 PM
Thank you for reporting this issue.

Could you please provide us with a block of sample code to show this issue?

It would be greatly appreciated if you could provide us this information as quickly as possible.

Thank you
Posted by MS-Moderator01 on 11/7/2011 at 10:44 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)