Re: Allocating Heap Memory
try:
Quote:
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.
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
Re: Allocating Heap Memory
Thanks for your help. That works for this problem (it compiles at least, testing will tell for sure).
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