|
Ok I found the mistake, but still have one question:
This is the code that allocates some memory:
GeoIP *gi = (GeoIP *)malloc(sizeof(GeoIP));
gi->cache = (unsigned char*)malloc(sizeof(char) * dat_size);
if (gi->cache != NULL)
gi->cache = (unsigned char*)rc_dat;
else
gi->cache = NULL;
This allocates memory for the contents of the db from the resource.
In the DLL_PROCESS_DETACH I call this code:
if(gi != NULL)
free(gi);
When I comment out the code above I don't get the assertion, but if not, the debug assertion occurs. It seems like gi->cache points to a non-null memory but free() still can't free the memory
Do I still need to free the gi->cache structure?
modified 12-Sep-18 21:01pm.
|
|
|
|
|
gi->cache isn't pointing at anything. in the first code sample, you allocated memory, then threw away the pointer when you set gi->cache to NULL or to rc_dat.
yes, you still need to free the memory, but you need to have the address of the memory before you can free it
-c
"To announce that there must be no criticism of the president, or that we are to stand by the president right or wrong, is not only unpatriotic and servile, but is morally treasonable to the American public."
-- Theodore Roosevelt
|
|
|
|
|
I do not understand this part.
GeoIP *gi = (GeoIP *)malloc(sizeof(GeoIP));<br />
gi->cache = (unsigned char*)malloc(sizeof(char) * dat_size);<br />
if (gi->cache != NULL) <br />
gi->cache = (unsigned char*)rc_dat;<br />
else <br />
gi->cache = NULL;
You allocate some memory and then you loose the pointer because you modify the pointer gi->cache. gi->cache now points to a different location and you cannot delete it if it is not on the heap. Also, you will surely have a memory leak; because you lost the pointer for the newly allocated memory space.
Mustafa Demirhan
http://www.macroangel.com
Sonork ID 100.9935:zoltrix
<nobr>They say I'm lazy but it takes all my time
|
|
|
|
|
Hi !!
I have 5 threads that takes the milisecond that take 10000, 15000, 20000, 25000, 30000 milisecond as parameter respectively. In the main thread i pass these parameter to the CreateThread function, then display how many threads are still running every second. It seems working seamlessly, but when i really notice, the first thread which I pass 5000 milisecond in only display 9 times(it should be 10 times), whereas the thread with 250000 as parameter is displayed 6 times. Where as each thread is 5 units away each the other.
this is my code
INT volatile runningThread = 5;
void Thread(DWORD time)
{
Sleep(time);
}
void main()
{
INT i;
HANDLE handle[5];
DWORD threadID[5];
DWORD time[5];
time[0] = 10000;
time[1] = 15000;
time[2] = 20000;
time[3] = 25000;
time[4] = 30000;
BOOL flag[5];
for(i = 0; i < 5; i++)
flag[i] = TRUE;
for(i = 0; i < 5; i++)
{
handle [i] = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)Thread
, (VOID *)time[i], NULL, &threadID[i]);
if (handle[i] == 0)
cout << "Create thread " << i << "failed " << GetLastError() << endl;
cout << threadID[i] << endl;
}
while(WaitForMultipleObjects(5, handle, TRUE, 1000) == WAIT_TIMEOUT)
{
for (i = 0; i < 5 ; i++)
{
if(WaitForSingleObject(handle[i], 0) == WAIT_OBJECT_0 && flag[i] == TRUE)
{
runningThread--;
flag[i] = FALSE;
}
}
printf(" %d ", runningThread);
}
for(i = 0; i <5; i++)
{
if(!CloseHandle(handle[i]))
cerr << "Failed to close process. Error number" << GetLastError() << endl;
}
cout << "\nAll threads have been terminated " << endl;
thanks in advance!!!
ADDED(after modification) : I've noticed one thing, that is if i debug it step by step (F11) then it works ok, but when running Ctrl+F5 it just doesnt seem run right!!!
|
|
|
|
|
You code looks okay. However, I am not sure if your design will work. For example, in the first loop that creates when threads and assign them to the handles, time elapses from the first thread. Again, your second loop also takes time to iterate.
Kuphryn
|
|
|
|
|
So you moved from processes to threads now?
I am not sure if I really understood your question (what do you mean with "thread x display only y times?). Therefore it may be that I completly lost your point.
However, thread execution is not a deterministic thing. Depending on other ready-to-schedule thread it may be, that your 5000 msec thread is started after the 25000 msec thread. On a machine with more than one CPU they may be started at really the same time. Besides timing with Sleep, WaitForXXX and so on is not completly precise, standard timer resolution is about 10 msec on NT and about 50 msec on Win9x. This means your 5000 msec thread might sleep just 4990 msec.
Long story, short message: A threads execution time is not reliable and it is just bad design to relay on it.
--
Daniel Lohmann
http://www.losoft.de
(Hey, this page is worth looking! You can find some free and handy NT tools there )
|
|
|
|
|
Hi Daniel! It's good hear from u!
Sorry, i was abit rush last night! But here is the requirment
<quote>
write a Win32 Console application that starts five threads, passing each a parameter which is the number of milliseconds that this thread is to simulate execution. The parameter must have a different value for each thread invocation, and should be in the range 20 to 40 seconds. You may predetermine these values, or determine them randomly. Your main thread should monitor these threads, outputting a digit every second indicating how many threads are still running. Your program should terminate after all five new threads have terminated.
I basically have done all of this but the result is as i explained above. Where as the first thread should run for 5 second it only run in 4, however other threads are running ok.
Do you notice any bugs or logical error??
Thanks in advance !!!
|
|
|
|
|
tcuong wrote:
Do you notice any bugs or logical error??
No, the code looks fine. I assume it is a timing problem: As I wrote the timing precision on NT is about 10 msec. If your Sleep( 5000 ) actually returns after 4990 msec and maybe the WaitForMultipleObjects(..., 1000) returns after 1010 msec it may well happen you "loose" one active messsage of the thread.
To verify this you could modify the timings by small amounts. I'd also recommend to add printfs before and after the Sleep() statement and monitor the output sequence.
--
Daniel Lohmann
http://www.losoft.de
(Hey, this page is worth looking! You can find some free and handy NT tools there )
|
|
|
|
|
Is there any other solutions? To conclude this thread, when dealing with thread, there is no way that the time allocated is run precisely when the thread executes , doesnt it???
Thanks !!
|
|
|
|
|
Yep.
A thread may get or loose the CPU at any time, therefore precise execution times are not possible in general.
--
Daniel Lohmann
http://www.losoft.de
(Hey, this page is worth looking! You can find some free and handy NT tools there )
|
|
|
|
|
As the others said, exact timings wont work!
Instead of monitoring each second, you can monitor twice each second. Then in the end; if you get an odd number (19 for the first thread for example) add 1 to it and divide it by 2. If you get an even number, then this means that the timings worked perfectly (which won't me the case for most of the time).
Mustafa Demirhan
http://www.macroangel.com
Sonork ID 100.9935:zoltrix
<nobr>They say I'm lazy but it takes all my time
|
|
|
|
|
Does anyone have any code where a text box
is highlightedin red when the mouse hovers
over it or when it has focus? There is a
demo project on codeguru, but it crashes
easily and that worries me.
Please, any response any one can give me
will be greatly appreciated.
Sincerely,
Danielle (an overworked graduate student)
|
|
|
|
|
Please don't laugh to hard but how do you except a command line parameter such as a flag?
Ex: MyProj.exe /s
Thanks Eric
|
|
|
|
|
In your main program put something like this:
void main(dataType flag)
{
switch flag:
case: s
printf("hello your input param is s"
break;
case:
.............
.............
}
whereas dataType is the dataType you want, such as integer or whatever flags you want. In the main program you can have switch case statement according to each input parameter
Hope this helps!!!
|
|
|
|
|
I knew that it was very simple. Thank you.
Eric
|
|
|
|
|
Very sorry for waisting your time
I figured it out
void main(int argc, char* flag[])
{
// acrgc = number of string param
// flag[1] = first string param
// ...
// flag[n] = n string param
}
Thanks for your time though
Eric
|
|
|
|
|
how can i update the dialogs of my dialog application in my own class. Currently i've inherited from the *Dlg class, and even though the compiler returns no errors, i get a error for the command UpDateData(TRUE) while the program is running, saying Debug Assertion Failed.
Anyone know what the problem is?
|
|
|
|
|
Probably something wrong in the DDX variable mapping declared in ::DoDataExchange(CDataExchange* pDX).
Be sure to use the Dialog classwizard to add or modify those members. Also check if member types are adequate (MSDN MFC Technical Note can help for that).
Let's swallow a small raisin.
|
|
|
|
|
Will you be able to post your code ?? I think it's likely that the data that you're trying to use is no longer exist. This happens when you don't overide the OK button!!
|
|
|
|
|
I'm writing to a database using appendChunk. it just goes and keeps adding to the same database - instead of overwriting it. THere dont seem to be any parameters that I can set in the method. I open the database using the recordset.Open function and I didnt see anything there that I could set either to make the database get rewritten.
PLease help!
ns
|
|
|
|
|
Try setting that field to null first.
Joel Lucsy (jjlucsy@ameritech.net)
|
|
|
|
|
By default when I create propertysheets for my dialog they have Help button.How can I remove this button?
Mazy
"If I go crazy then will you still
Call me Superman
If I’m alive and well, will you be
There holding my hand
I’ll keep you by my side with
My superhuman might
Kryptonite"Kryptonite-3 Doors Down
|
|
|
|
|
In OnInitDialog() , do GetDlgItem(IDHELP)->ShowWindow(SW_HIDE);
---
Shog9
Actually I use to find learning in bars when drinking really useful.
It sort of makes a language liquid. - Colin Davies, Thinking in English?
|
|
|
|
|
Thank you Shog9.
Mazy
"If I go crazy then will you still
Call me Superman
If I’m alive and well, will you be
There holding my hand
I’ll keep you by my side with
My superhuman might
Kryptonite"Kryptonite-3 Doors Down
|
|
|
|
|
Anybody knows where i can download the OpenML SDK?
Kiddxin
|
|
|
|