Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi All,

I am getting Memory Exception when appending a Long String to CString many times.

C++
try
{
    for(int i=0;i<100; i++)
    {
        ..
        ..
        ..
        pQueryInfo->m_strQueryText += strQueryText; // Appending String --> MemoryException.
     }
} catch (CMemoryException *e){
e->Delete();
}

pQueryInfo->m_strQueryText and strQueryText are CString Variables.After appending a Long String many times i am getting this CMemoryException. How can it be
resolved. Help me out.

Thanks
Uday
Posted
Updated 23-Oct-13 20:20pm
v2
Comments
Bernhard Hiller 24-Oct-13 2:07am    
Duplicate of http://www.codeproject.com/Questions/673086/CString-Appending-getting-CMemoryExcepton

1 solution

There isn't enough information here to solve this problem, but I'll toss you a checklist.

1. Is your CString initialized properly with a constructor? A static declaration like:

CString cs = "This is a string."

Could throw an error when using an assignment operator like +=

2. Is your CString created outside the scope of the for loop? If not, your case statement might be reached before the string is initialized and Delete() would be called on a null object.

3. Is your query text initialized properly as well? If not, it could be pointing to some random trash in memory that might cause a memory exception.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900