?
Solved

Pointer to NETRESOURCE

Posted on 2004-10-08
4
Medium Priority
?
237 Views
Last Modified: 2010-04-01
Hi, this is a little complicated...
I'm passing a pointer to NETRESOURCE to my class to be used later, like so:

NETRESOURCE *nr; //valid at this point
HTREEITEM  ht;
CDomains * e = new CDomains( FALSE, &ht, nr );

Then in the class constructor I'm making a copy of the pointer:

CDomains( BOOL bWMI, HTREEITEM *hProvider, NETRESOURCE * nr = NULL, LPVOID r = NULL )
{
      m_hProvider = new HTREEITEM;
      memcpy(m_hProvider, hProvider, sizeof(HTREEITEM));
      if ( nr ) {
            m_pnr = new NETRESOURCE;
            memcpy(m_pnr, nr, sizeof(NETRESOURCE));
      } else {
            m_pnr = NULL;
      }
}

Now comes the tricky part. This class inherits from another class that has Start() function. I call it next:
e->Start();

The Start() function of the class CDomains inherits from:
void CSuper::Start()
{
      HANDLE      hThread = NULL;
      InterlockedIncrement( &g->ThreadCount );

      DWORD      id;
      hThread = CreateThread( NULL, THREAD_STACK_SIZE, Enumerate, this, CREATE_SUSPENDED, &id );

      DWORD dwWaitResult = WaitForSingleObject( g->hSemaphore, INFINITE);  //wait

      switch( dwWaitResult )
      {
          
      case WAIT_OBJECT_0:
            {
                  if ( hThread )  {
                        // thread created
                        SetThreadPriority( hThread, THREAD_PRIORITY_LOWEST );
                        ResumeThread( hThread );
                  }
            }
            break;

      case WAIT_TIMEOUT:
      case WAIT_ABANDONED:
            {
                  //report error
                  TRACE(_T("startThreadProcessLog SEMAPHORE Timed Out"));
            }
            break;
      }
      CloseHandle( hThread );
      
}

This goes well, the parameter passed to Enumerate function (this) contains the inheriting class CDomain
with all variables still valid.

The Enumerate function:
DWORD WINAPI  CSuper::Enumerate( LPVOID This )
{
       CSuper * o = ( CSuper *)This;
      o->Enumerate();
      return 0;
}

This where the things go sour. All the variables in o->CDomains are still valid,
numeric values in o->CDomains->m_pnr are still valid, but all the string
members of that struct are now invalid pointers.

Please help, I'm willing to give more points if necessary.
0
Comment
Question by:jd9288
[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
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 12260446
>>but all the string members of that struct are now invalid pointers.

That's not really surprising, since the strings/arrays might go out of scope.

>> memcpy(m_pnr, nr, sizeof(NETRESOURCE));

Is prone to 'loose' the strings. How are you filling the NETRESOURCE structures? You should always take care of that memory on your own instead of using 'memcpy()', e.g. by using

void CopyNetReource ( NETRESOURCE* dst, NETRESOURCE* src) {

   dst->dwScope = src->dwScope;
   dst->dwType = src->dwType;
   dst->dwDisplayType = src->dwDisplayType;
   dst->dwUsage = src->dwUsage;
   dst->lpLocalName = _tcsdup(src->lpLocalName);
   dst->lpRemoteName = _tcsdup(src->lpRemoteName);
   dst->lpComment = _tcsdup(src->lpComment);
   dst->lpProvider = _tcsdup(src->lpProvider);
}

void FreeNetResource ( NETRESOURCE* p) {

   free(p->lpLocalName);
   free(p->lpRemoteName);
   free(p->lpComment);
   free(p->lpProvider );
}
0
 

Author Comment

by:jd9288
ID: 12260575
Brilliant! I suspected this might be the case, but since I'm so unsure of myself,
I thought I'm missing something.

Could you please tell me why it would still work sometimes
the way I had it?
0
 
LVL 86

Expert Comment

by:jkr
ID: 12260614
>>Could you please tell me why it would still work sometimes
>>the way I had it?

Because the strings are 'there' for a while, at least during an enumeration. You never know when the system will free the momry it used to store the strings in.

BTW, you could extend the above to

class CNetResource : public NETRESOURCE {

public:

    CNetResource () { /* ...  */ }
    CNetResource ( const NETRESOURCE& nr)  { /* ...  */ }
    CNetResource& operator=(const NETRESOURCE& nr)  { /* ...  */ }
};

to 'automate' that copying process.
0
 

Author Comment

by:jd9288
ID: 12260703
Thank you very much, I think I'm getting there.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

770 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