Solved

CObList have problem of check it's content???

Posted on 2001-08-01
3
309 Views
Last Modified: 2010-04-02
Hi:
  Here I have a block of code use CObList to store data. I check the list whether it contain the data I want to add or not before I added the input Data to the list in the following code.
.h file
void test(LPCSTR inputdata)

.cpp file
test(inputdata)
{
....
char* lpBuf;
memcpy(lpBuf, inputdata, Size);
lpBuf[lSize] = '\0';

POSITION posCount;
for(posCount = m_PdclistSend.GetHeadPosition(); posCount != NULL;)
{
(char*) lpList;
lpList = (char*)m_PdclistSend.GetNext(posCount);
if (strcmp(lpList,lpBuf)!=0)
{
                    m_PdclistSend.AddTail((CObject*)lpBuf);
}
}
}
With this block of code. It has a strange output.
The list size increse strange and some have strange symbol, I add ten inputdata first, then send ten same  message to this method again. It expected not add data to the list. But it add to the list and the list size increase from 10 to 100, 5000....

So I use another method.
{
...
POSITION pos;
if( ( pos = m_PdclistSend.Find( (CObject*)lpBuf)) == NULL ) // Hunt for lpBuf     {                          .
m_PdclistSend.AddTail((CObject*)lpBuf);
}
But the function return null always even i know the ObList has contain  the input data.So it still add inputdata to the lsit and cause problem.
Since it is an enhancement work. I cannot change the list type,

So anybody have idea of how to  check the content of the list with the inputdata without affect the content of COblist.
Millions of thanks in Advance.
                     

0
Comment
Question by:wky086
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6340153
>>m_PdclistSend.AddTail((CObject*)lpBuf);

Well, you're adding the object you found to the list again, so the increase in size isn't really surprising...
0
 

Accepted Solution

by:
sandeepsehgal earned 50 total points
ID: 6340327
Hi wky086,

Some of the major problems that appear to be in your code include :

char* lpBuf;
memcpy(lpBuf, inputdata, Size);
lpBuf[lSize] = '\0';

You are not allocating memory for lpBuf, but are copying data into it, this shall cause over runs, it should have caused your code to crash, but perhaps it is not crashing due to memory overwrite on the memory that is owned by your program. The memory overwrite is causing memory to be corrupted.

Look at the sample code listed hereunder.

The function AddItemToList adds an item to the list if it does not already exist in the list. It returns a BOOL value which specifies whether the item has been added to the list or not.

class MyList
{
private:
     CObList * m_pList;
public:
     MyList()
     {
          m_pList = new CObList;
     }
     ~MyList()
     {
          m_pList->RemoveAll();
          delete m_pList;
          m_pList = NULL;
     }
     BOOL AddItemToList(LPCTSTR lpPtr)
     {
          BOOL bFound = FALSE;
          LPSTR lpBuf = NULL;
          int nSize = strlen(lpPtr);
          lpBuf = new TCHAR[nSize+1];
          memcpy(lpBuf, lpPtr, nSize);
          lpBuf[nSize] = '\0';
          POSITION posCount = NULL;
          for(posCount = m_pList->GetHeadPosition(); posCount != NULL;)
          {
               LPSTR lpItem = (LPSTR)m_pList->GetNext(posCount);
               if (strcmp(lpItem,lpBuf) == 0)
               {
                    bFound = TRUE;
                    break;
               }
          }
          if (bFound == FALSE)
               m_pList->AddTail((CObject *)lpBuf);
          return bFound;
     }
};

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
     int nRetCode = 0;

     // initialize MFC and print and error on failure
     if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
     {
          // TODO: change error code to suit your needs
          cerr << _T("Fatal Error: MFC initialization failed") << endl;
          nRetCode = 1;
     }
     else
     {
          // TODO: code your application's behavior here.
          CString strHello;
          strHello.LoadString(IDS_HELLO);
          cout << (LPCTSTR)strHello << endl;
     }

     MyList oMyList;
     BOOL bFound = FALSE;
     for (int i = 0; i < 10; i++)
     {
          CString strItem;
          strItem.Format("%d%d%d%d%d%d", i,i,i,i,i);
          bFound = oMyList.AddItemToList(strItem.operator LPCTSTR());
          if (bFound == TRUE)
          {
               cout << "Found";
          }
     }

     for (i = 0; i < 10; i++)
     {
          CString strItem;
          strItem.Format("%d%d%d%d%d%d", i,i,i,i,i);
          bFound = oMyList.AddItemToList(strItem.operator LPCTSTR());
          if (bFound == FALSE)
          {
               cout << "Not Found";
          }
     }

     return nRetCode;
}

Try it out.

Sandeep
0
 

Author Comment

by:wky086
ID: 6342900
Thank you very much. I will try it out
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question