CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2003
    Location
    PA
    Posts
    124

    Allocating Heap Memory

    So far, you all have been a greate resource so I am asking one last question (hopefully).

    I am threading some database functions and am passing a struct via the CreateThread(...) function. No problem there. (See definition of the struct)
    Code:
    	typedef struct _dbThreadData{
    		CString dbData1;
    		CString dbData2;
    		char dbTime[6];
    		CString dbCommand;
    		CString dbStatus;
    		long dbProcess;
    	}DBTHREAD, *PDBTHREAD;
    I am initializing in the main() like so:
    Code:
    	PDBTHREAD dbth;
    	dbth-> dbData1 = Data1;
    	dbth->dbData2 =Data2;
    	dbth->dbStatus = stat;
    	sprintf(dbth->dbTime , "%d", stopTime);
    	dbth->dbProcess = 1;
    	// create thread here for mdtMetrics
    	dbThreadHandle[1] = CreateThread(
    		NULL,		// default security attributes
    		0,		// use default stack size
    		dbThreadProc,
    		dbth,		// argument to thread function
    		0,		// use defailt creation flags
    		&dwThreadID	// returns thead identifier
    	);
    The compiler barked that the dbth struct was not initialized
    C:\Visual Studio Projects\Poller\PollerDlg.cpp(1096) : warning C4700: local variable 'dbth' used without having been initialized
    , so investigation shows that the HeapAlloc(...) might be needed.

    I added:
    Code:
    	PDBTHREAD dbth;
    	dbth = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DBTHREAD));
    but am now getting teh error:
    C:\Visual Studio Projects\Poller\PollerDlg.cpp(1095) : error C2440: '=' : cannot convert from 'void *' to 'struct _dbThreadData *'
    Conversion from 'void*' to pointer to non-'void' requires an explicit cast
    .

    Am I doing this part right??? Since i am declaring the struct as a pointer (_dbThreadData *dbth), is HeapAlloc(...) necessary to initialize the struct? I am missing something but cannot figure out what? Suggestions?
    There are 10 types of people in the world, those that understand binary and those that don't.
    Using VS 2010

  2. #2
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Allocating Heap Memory

    try:

    dbth =(DBTHREAD*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DBTHREAD));
    to explicitly cast as the compiler req.

    HTH,

    ahoodin

    PS If this post helped dont forget to rate. Just click the scale icon and click the approve radio button.
    Last edited by ahoodin; June 6th, 2005 at 12:16 PM. Reason: PS + comment

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Allocating Heap Memory

    Quote Originally Posted by rick7423
    I am initializing in the main() like so:
    Code:
    	PDBTHREAD dbth;
      	dbth-> dbData1 = Data1;
      	dbth->dbData2 =Data2;
      	dbth->dbStatus = stat;
      	sprintf(dbth->dbTime , "%d", stopTime);
      	dbth->dbProcess = 1;
      	// create thread here for mdtMetrics
      	dbThreadHandle[1] = CreateThread(
      		NULL,
      		0,	
      		dbThreadProc,
      		dbth, 
      		0,	 
      		&dwThreadID  );
    One problem here is that you declared a pointer to the structure rather than the structure itself.
    Try the following (which should work as long as dbth doesn't go out of scope).

    Code:
    DBTHREAD dbth;
       	dbth.dbData1 = Data1;
       	dbth.dbData2 =Data2;
       	dbth.dbStatus = stat;
       	sprintf(dbth.dbTime , "%d", stopTime);
       	dbth.dbProcess = 1;
       	// create thread here for mdtMetrics
       	dbThreadHandle[1] = CreateThread(
       		NULL,		
       		0,		
       		dbThreadProc,
       		static_cast<LPVOID>(&dbth),
       		0,	   
       		&dwThreadID  );

    Quote Originally Posted by rick7423
    The compiler barked that the dbth struct was not initialized , so investigation shows that the HeapAlloc(...) might be needed.
    You may need to allocate the DBTHREAD structure memory on the heap if there is a chance that a stack allocated struct will go out of scope. If you do need to allocate it, prefer to use new/delete over HeapAlloc et. al. Maybe this is just personal preference, but you already are using CString which internally uses new to allocate. My feeling is you should be consistent with your memory allocation throughout the program ( well as much as possible anyway ).
    Code:
      
      PDBTHREAD pdbth = new DBTHREAD;
        	dbth->dbData1 = Data1;
        	dbth->dbData2 =Data2;
        	dbth->dbStatus = stat;
        	sprintf(dbth->dbTime , "%d", stopTime);
        	dbth->dbProcess = 1;
        	// create thread here for mdtMetrics
        	dbThreadHandle[1] = CreateThread(
        		NULL,		
        		0,		
        		dbThreadProc,
        		static_cast<LPVOID>(pdbth),
        		0,		
        		&dwThreadID  );
      
        // Wait on thread
      
        // cleanup 
        if(pdbth)
        {
      	delete pdbth;
      	pdbth = NULL;
        }
      
        CloseHandle( dbThreadHandle[1] );
    Arjay

  4. #4
    Join Date
    Jul 2003
    Location
    PA
    Posts
    124

    Re: Allocating Heap Memory

    Thanks for your help. That works for this problem (it compiles at least, testing will tell for sure).
    There are 10 types of people in the world, those that understand binary and those that don't.
    Using VS 2010

  5. #5
    Join Date
    Feb 2003
    Location
    BeiJing China
    Posts
    290

    Re: Allocating Heap Memory

    A question :

    Is "HeapAlloc()" fater than "malloc" or "new" ?

    There has been a time I used "HeapAlloc()" in a project, with flag "HEAP_NO_SERIALIZE" which causes an unpredicted nightmare, my project is down from time to time(only happen in some PC, and release version !), and can't found the reason. It really costs me two weeks to found the reason! Should be very careful to use flag "HEAP_NO_SERIALIZE" !

    wuxf
    Waiting OFFERS from CMU, UMass, UC Davis, OSU
    Mail to me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured