krischk123
May 21st, 2008, 05:53 AM
Hi All
I will start with a very simple concept of pointers and memory that I have confusion about, before I move on to something related to MFC.
assume the following piece of code:
void func()
{
int *p = 10;
......
......
}
Here 'p' points to a memory location that contains the value 10; When the func() is exited, 'p' is popped oof the stack right? So the reference to the memory having the value 10 is gone. Now when is the memory released? Will it be any problem if some other piece of code tries to access this same memory location after 'p' goes out of scope?
Please don't tell me anything about auto_ptr here. I want to know about the scope of pointers and memory they point to.
Now lets come to the more complex involving threads (though I am going into multithreading).
Say I have the following piece of code:
HANDLE hThread ; //global var
void funcCreateThread()
{
CWinThread *pthread = AfxBeginThread(ControllingFunc, .........);
hThread = pthread->m_hThread;
......
......
}
AfxBeginThread creates a CWinThread object (on the process heap ? ) and returns a pointer to that memory which we are storing in pthread. I also store the handle to the created thread in a global var.
When the function funcCreateThread() is exited, we lose the reference to this memory location but the memory for the object still persists right?
Say towards the end of my application, I close the thread( by returning from its controlling function) and then call CloseHandle to release the handle as follows:
WaitForSingleObject(hThread,10000)
{
.....
}
CloseHandle(hThread);
hThread = NULL;
Now what will happen to the CWinThread object and its memory location?
Is it going to create a memory leak?
The reason I am asking this is because my multithreaded application is causing EXCEPTION_ACCESS_VIOLATION and it also contains 3rd party DLL's and involves JNI. Is it possible that code in 3rd party DLL is trying to access this memory location which results in error?
http://www.codeguru.com/forum/showthread.php?t=452661
I will start with a very simple concept of pointers and memory that I have confusion about, before I move on to something related to MFC.
assume the following piece of code:
void func()
{
int *p = 10;
......
......
}
Here 'p' points to a memory location that contains the value 10; When the func() is exited, 'p' is popped oof the stack right? So the reference to the memory having the value 10 is gone. Now when is the memory released? Will it be any problem if some other piece of code tries to access this same memory location after 'p' goes out of scope?
Please don't tell me anything about auto_ptr here. I want to know about the scope of pointers and memory they point to.
Now lets come to the more complex involving threads (though I am going into multithreading).
Say I have the following piece of code:
HANDLE hThread ; //global var
void funcCreateThread()
{
CWinThread *pthread = AfxBeginThread(ControllingFunc, .........);
hThread = pthread->m_hThread;
......
......
}
AfxBeginThread creates a CWinThread object (on the process heap ? ) and returns a pointer to that memory which we are storing in pthread. I also store the handle to the created thread in a global var.
When the function funcCreateThread() is exited, we lose the reference to this memory location but the memory for the object still persists right?
Say towards the end of my application, I close the thread( by returning from its controlling function) and then call CloseHandle to release the handle as follows:
WaitForSingleObject(hThread,10000)
{
.....
}
CloseHandle(hThread);
hThread = NULL;
Now what will happen to the CWinThread object and its memory location?
Is it going to create a memory leak?
The reason I am asking this is because my multithreaded application is causing EXCEPTION_ACCESS_VIOLATION and it also contains 3rd party DLL's and involves JNI. Is it possible that code in 3rd party DLL is trying to access this memory location which results in error?
http://www.codeguru.com/forum/showthread.php?t=452661