Solved

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

Posted on 2001-08-01
3
301 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
3 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>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
Comment Utility
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
Comment Utility
Thank you very much. I will try it out
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now