|
AFXbeginthread creates an object on the heap.
so what is the question ?
This signature was proudly tested on animals.
|
|
|
|
|
So when the function that creates it goes
the Thread still exists
|
|
|
|
|
Yes, if you create an object on the heap, it will remain until you clear it off manually (I mean - by using delete if created with new or with fr ee if created with malloc ).
ForNow wrote: I guess you create a thread via AFXbeginthread it rerun a pointer to a CWinThread and I have noticed that after the object which created it end the thread is stil around
No, after the thread function returns, the CWinThread object delete s itself (unless you manually set the m_bAutoDelete to false ).
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Rajesh R Subramanian wrote: Yes, if you create an object on the heap, it will remain until you clear it off manually (I mean - by using delete if created with new or with free if created with malloc).
An addendum, if I may...
If you create an object on the heap, it'll remain there until you clean it up... or the process under which it was allocated is terminated, at which point it'll be cleaned up by the OS.
The critical point to understand is this. If you allocate memory once in the course of your program and don't delete (or free, whichever is appropriate) it, it's not a big deal - the OS will clean it up. You get into trouble when you allocate blocks repeatedly and don't free (or delete) them. It's true that the OS will still clean it all up when the process terminates, but the longer the process runs the more memory it 'leaks', with detrimental effect on the whole system for the duration of the process' lifetime. This is why it's considered to be good form to always free any memory you've allocated.
L u n a t i c F r i n g e
|
|
|
|
|
LunaticFringe wrote: at which point it'll be cleaned up by the OS.
I had kept that off deliberately in order for the OP to come up with a query of "What happens if I do not clean it up?". Also, it would do good go the OP if you replied to his query directly (because he will receive an email notification - now, I've received it).
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Rajesh R Subramanian wrote: I had kept that off deliberately in order for the OP to come up with a query of "What happens if I do not clean it up?".
Sorry I messed up your subtle plotting.
Rajesh R Subramanian wrote: Also, it would do good go the OP if you replied to his query directly (because he will receive an email notification - now, I've received it).
Meh. My reponse was meant to be an addendum to your response - it's at the right place in the thread as far as I'm concerned. If the OP turns out to be too lazy to read the thread he started, I'm not going to lose sleep over it.
L u n a t i c F r i n g e
|
|
|
|
|
Well, usually a new heap creation should be ended with a free , delete and/or other "destructions" to avoid overflow or undesired remanence. Specially with pointers.
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpfull answers is nice, but saying thanks can be even nicer.
modified on Tuesday, December 22, 2009 2:04 PM
|
|
|
|
|
Nelek wrote: Well, usually a new creation should be ended with a free, delete and/or other "destructions" to avoid overflow or undesired remanence.
Well, except for the fact that memory allocated with new should be cleaned up with delete , and NOT with free ...
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
Right. Instead of "new" should be "heap". My bad
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpfull answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Hehe. But I am not going to miss a good chance to nitpick.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
I am launching a worker thread . Inside the thread function i open CDialog's using DoModal().There is no problem in the first launch of the thread.
But in second launch the stack seems to get corrupted, the CString member variable of the Dialogs show values like "C4".. "D5" etc before even they r initialized in the Dialogs contructor(i.e when the execution is within the constructor).Even after Initializing the dialog's CString variables in constructor the variables do not get initialized, they still show "C4" etc
psuedo code below:
line 1:CWelcomeDlg dlg;
line 2:dlg.SetTitle(strDialogTitle);//Set the dialog Title
line 3 :ret = dlg.DoModal();//show dialog
//Constructor
CWelcomeDlg::CWelcomeDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWelcomeDlg::IDD, pParent)
{
m_strData1=_T("");---->m_strData1 shows value "C4" and cannot b initialized to empty string.
m_strData2=_T("");--->m_strData1 shows value "C4"
m_strData3=_T("");--->m_strData1 shows value "C4"
}
|
|
|
|
|
Pls note that the above problem is seen only in the Release mode
|
|
|
|
|
Does your program otherwise work ok? If yes, then take into account that the debugger might fool you, afterall, it IS a release build, we sadly have seen such "odd" occurances many times when trying to debug release builds, the "tooltips" VS displays when pointing at a variable during runtime to show you the value of the variable (or the quick watch window, or or or...) would show all kinds of exotic, strange values which clearly aren't there (you can for example check this if you bring up a message box showing the value you are interested in formatted into a string, in your case, the string itself i guess...).
p.s: ah yes, and as others said, try to avoid creating secondary UI threads because they mean a lot of trouble...
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
modified on Tuesday, December 22, 2009 12:36 PM
|
|
|
|
|
Shailesh H wrote: Pls note that the above problem is seen only in the Release mode
Please note that you have an article to read[^].
In addition to that, I'd second the opinion of other poster, who asked you not to touch the UI from within a worker thread.
“Follow your bliss.” – Joseph Campbell
|
|
|
|
|
don't do UI in the worker thread; ask the main application thread to do it by sending messages and using event and things like that to keep everything synchronized.
read :
http://www.flounder.com/workerthreads.htm[^]
and everything thread related on that site.
This signature was proudly tested on animals.
|
|
|
|
|
Is the second launch waiting for the first to be ended?
If not... you may have problems with DoModal () multiple calls. Would not be better to use ModalLess dialogs?
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpfull answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Shailesh H wrote: line 2:dlg.SetTitle(strDialogTitle);//Set the dialog Title
Is strDialogTitle of type CString? If then is it shared across threads?
-Suhredayan
|
|
|
|
|
Hi friends,
I am having just one month of experience in network programming. I want to sniff TCP packets using Winsock. I could right the code for that as below. It worked on XP 32 bit but not on Vista Home 32 bit machine.
SOCKET sniffSocket;
if((sniffSocket= socket(AF_INET, SOCK_RAW, IPPROTO_IP))==SOCKET_ERROR)
{
return 0;
}
struct sockaddr_in src;
memset(&src, 0, sizeof(src));
src.sin_addr.S_un.S_addr = inet_addr (pAdapterInfo->IpAddressList.IpAddress.String);
src.sin_family = AF_INET;
src.sin_port = 0;
if (bind(sniffSocket,(struct sockaddr *)&src, sizeof(src)) == SOCKET_ERROR)
{
return 0;
}
int j=1;
if (WSAIoctl(sniffSocket, SIO_RCVALL, &j, sizeof(j), 0, 0, &in, 0, 0) == SOCKET_ERROR)
{
return 0;
}
char *pkt_data = (char *)malloc(65536);
int res;
do
{
res = recvfrom(sniffSocket, pkt_data, 65536, 0, 0, 0);
...
...
}
while (res > 0);
...
...
The above code is working fine on XP. I could sniff TCP/IP packets (in pkt_data variable) this way. But when I run the same code on Vista, I am able to trace only UDP/IP packets. Can anybody tell me please why is it not working on Vista? I mean what is the problem with my code that I cannot sniff TCP/IP packets?
My second query: A system can have multiple adapters/devices. When we type a web-site address on a browser, we don't tell which adapter to connect to. Browser automatically opens adapter that is connected to internet. How it does that? I mean how can I detect which adapter is being used for internet connection? Is there any Winsock API for the that?
Thanks in Advance
Regards
Aseem
modified on Wednesday, December 23, 2009 1:30 AM
|
|
|
|
|
Please use the code block to post code snippets (as it stands your code is unreadable).
Please elaborate, i.e. what does it mean 'it doesn't work on Vista'? Are you getting errors? Are you getting a misbehaviour (detail it)?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hi Pallini,
I am sorry I did not write the code snippet right way. I have corrected it now. Please have a look over it.
As I said my code is working fine on XP. In my application I need to make a dump of TCP/IP packets. I could do so on XP. But when I run the same code on Vista, I get only UDP/IP packets (in variable pkt_data). I do not know why I am not getting TCP/IP packets (in variable pkt_data). Apart from it, I am not getting any error or misbehavior.
My second query is that is there any way by which I can know through which adapter the system is connected to internet so that I can sniff TCP/IP packets from that adapter only.
Thanks and Regards
Aseem
|
|
|
|
|
I developed a windows service which was written in VC++ (6.0) earlier and was directly migrated to VS2008 (C++).
The migrated service works fine in Windows 2000 Server but gives access violation exceptions when started in Windows Server 2003 and Windows XP system.
Is there some thing different in Windows 2003 server with respect to windows 2000?
Do I need to make some settings for this to make work?
With Regards,
Abraham Jose
|
|
|
|
|
Did you run it in the debugger to find out what the exceptions are?
|
|
|
|
|
After converting project from VC6.0 to VS2008,while running application it shows the following error
Unhandled exception at 0x787ac40c (mfc90d.dll) in CS3OperatorStation.exe: 0xC0000005: Access violation reading location 0x00000020.
When i debug it,i got this error in this particular line
void tSendActiveData()
{
while(1)
{
AfxGetMainWnd()->SendMessage(WM_COMMAND,ID_SENDACTIVEDATA,NULL); Sleep(500);
}
}
tSendActiveData is a thread.
I want to invoke this fucntion in this thread.thts why im using it.
Its working fine in VC6.0 version.
Anu
|
|
|
|
|
You shouldn't do this. You should post a message to the main thread, using global namespace's PostMessage [^] with target the main window handle (you may pass it to the worker thread).
See this article [^] on how to use worker threads (read carefully the "Worker Threads and the GUI IV: AfxGetMainWnd" section).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Please check the return value of AfxGetMainWnd(). This function is supposed to return the main windows associated with the thread. As your thread does not have a window, the return value might be zero.
|
|
|
|