Have a situation with my code, where it appears the stringstream object is not being cleared.

Here is my code, this code will set an interprocess shared variable,
The code is simplified to illustrate my problem, and all other variables are there to aid in that, for instance, the ints in this code are not the types I am using in my app which are uintptr. But the problem remains the same.

int _tmain()
{

   TCHAR szName[]=TEXT("MyFileMappingObject");
   HANDLE hMapFile;
   LPCTSTR pBuf;

   hMapFile = CreateFileMapping(
                 INVALID_HANDLE_VALUE,    
                 NULL,                    
                 PAGE_READWRITE,          
                 0,                       
                 BUF_SIZE,                
                 szName);                 

   if (hMapFile == NULL)
   {
      _tprintf(TEXT("Could not create file mapping object (%d).\n"),
             GetLastError());
	  _getch();
      return 1;
   }
   pBuf = (LPTSTR) MapViewOfFile(hMapFile,   
                        FILE_MAP_ALL_ACCESS, 
                        0,
                        0,
                        BUF_SIZE);

   if (pBuf == NULL)
   {
      _tprintf(TEXT("Could not map view of file (%d).\n"),
             GetLastError());

       CloseHandle(hMapFile);
	   _getch();
	   
      return 1;
   }

   stringstream ss (stringstream::in | stringstream::out | stringstream::trunc);
      
   int Int = 55525;
   ss << Int;
   string s = ss.str();
   const char * p = s.c_str();
   ss.clear(); // here I am trying to clear the buffer
  
   CopyMemory((PVOID)pBuf, p, (_tcslen(p) * sizeof(TCHAR)));

   //at this point the shared variable read from other app is "55525"

   _getch();
   
  
   int Int2 = 44425;
   ss << Int2;
   string s2 = ss.str();
   const char * p2 = s2.c_str();
   ss.clear();

   CopyMemory((PVOID)pBuf, p2, (_tcslen(p2) * sizeof(TCHAR)));

   //at this point the shared variable read from other app is "5552544425"
   //when I was expecting it to be "44425"

   _getch();
   
   UnmapViewOfFile(pBuf);

   CloseHandle(hMapFile);

   return 0;
}

I am assuming it's the stringstream, but of course it could be a buffer problem.
The only two common vars used are ss and pBuf.

I have use this method of sharing between process in a different fashion, and not had this problem, which is why I'm thinking it is the ss.

I'd appreciate if an eye cast over my code for any glaring errors I may have made.
Or indeed any other comments or advice.

Recommended Answers

All 4 Replies

All clear does in this is remove any error flags that might be present in the stringstream object. If you want to empty the stringstream then you want to use the ignore() function. Try replacing ss.clear() on line 59 with ss.ignore(std::numeric_limits<streamsize>::max()); . You will need to include the <limits> header for the numeric_limits() function.

Thank you for the reply and suggestion.
I am getting different behaviour now, but still not what I am expecting :(

Now I am getting "55525" in both cases

I now appears that it is actually a pBuf problem, where it was being appended
but it might not be able to be replacecd.

I am getting different behaviour now, but still not what I am expecting :(

Now I am getting "55525" in both cases

Use stringstream::str() instead of ignore() .

I.e.

ss.str(""); // clear the buffer

Thanks, I appreciate your help.
That has moved me on a fair bit, in that now the stringstream is definitely getting cleared.

But it's still not what I'm after, because the buffer pBuf still holds "55525"

If I copy 44425 into it, that is fine, and it seems as though it is working as I want it to, but infact it is not.

If instead of 44425, I copy say 44, then the buffer pBuf then holds "44525", the "525" being from the tail end of the last value copied into it.

I tried

ss << Int2 << '\0';

to try and terminate it, but to know avail.

I have had to loop through the buffer array and set all elements to '\0' before copying another value into it, it does the job, but just seems a bit to hackish.

I'm still open to any suggestions if anyone has them, I'm not sure of the expense of looping through the buffer to clear it each time you see.

I tried this

string s2 = ss.str() + '\0';

But it seems to not copy the terminator into buffer :(

Thank you again for your time.

(edit)

I think this might me cheaper than looping the buffer, and seems to work fine.

pBuf[s2.length()] = '\0';

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.