Problem in accessing global pointer in a thread function.

S

sachinahuja82

Hi All
I am using multithreading in my console application.Here is the
problem.
I have created a global pointer of a structure.I allocated the memory
to this pointer using 'new' operator.The memory gets allocated and I
filled the desired value in this structure using global pointer.
After this I create a thread and reached to thread function.Now I want
to accesss the global pointer in this thread function,but I found that
the globalPointer is pointing to NULL.
Here is some part of code to describe my problem.

XLauncherConfig *gclpXLauncherCfg = NULL; //
global declaration of structure pointer
DWORD WINAPI fnRunThreadByPassUart(LPVOID lpParam) //Thread
function
{
if(!lpParam)
{
printf("Connection Thread function is null\n");
return ERR_AT_INVALID_ARG;
}
// XLauncherConfig *clpXlauncherCfg = (XLauncherConfig *)lpParam;
WNDCLASS wc;
HWND hwnd;
MSG msg;
HANDLE hInstance = GetModuleHandle(NULL);
wc.style = 0; // Class style
wc.lpfnWndProc = (WNDPROC) WndProc; // Window
procedure address
wc.cbClsExtra = 0; // Class extra
bytes
wc.cbWndExtra = 0; // Window extra
bytes
wc.hInstance = (HINSTANCE)hInstance; // Instance handle
wc.hIcon = LoadIcon (NULL, IDI_WINLOGO); // Icon handle
wc.hCursor = LoadCursor (NULL, IDC_ARROW); // Cursor handle
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); // Background
color
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = "MyWndClass"; // WNDCLASS name

RegisterClass (&wc);
////////////////////
hwnd = CreateWindow (
"MyWndClass", // WNDCLASS name
"RegisterApplication", // Window title
WS_OVERLAPPEDWINDOW, // Window style
CW_USEDEFAULT, // Horizontal position
CW_USEDEFAULT, // Vertical position
CW_USEDEFAULT, // Initial width
CW_USEDEFAULT, // Initial height
HWND_MESSAGE , //HWND_DESKTOP, // Handle of
parent window
NULL, // Menu handle
NULL, // Application's instance handle
NULL // Window-creation data
);

DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
TCHAR szMsg[80];
ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = GUID_CLASS_NDK10_BULK;
HDEVNOTIFY hDevNotify = RegisterDeviceNotification (hwnd,
&NotificationFilter,
DEVICE_NOTIFY_WINDOW_HANDLE);
//int nCmdShow = SW_RESTORE;
//ShowWindow (hwnd, nCmdShow);
UpdateWindow (hwnd);

while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}

return msg.wParam;

}


int main()
{
////some code.....!!!!!!!!!!!!!!!!!!!!!!!!!!
//XLauncherConfig is a structure which is given after
the main function.
XLauncherConfig *gclpXLauncherCfg = new XLauncherConfig;
///Some code to fill up this structure.
Error = fnSetConfigurationForXLauncher(argc , argv ,
gclpXLauncherCfg , nXLauncherElements);
if(Error != ERR_AT_NONE)
{
return Error;
}
//XLauncher.ini path is needed to check bypassuart
//some code for checking bypassuart flag
if(bUartBootByPass)
{
DWORD dwThreadId;
gclpXLauncherCfg->hThread= CreateThread(NULL,
0,fnRunThreadByPassUart , gclpXLauncherCfg , 0 , &dwThreadId);
if(gclpXLauncherCfg->hThread == NULL)
{
printf("Thread creation failure\n");
return ERR_AT_THREAD_FAIL;
}
WaitForSingleObject(hEvent , INFINITE);
//When code waits here and transfer control to
fnRunThreadByPassUart I found that gclXLauncherCfg points to NULL
}

//XLauncherConfig structure is shown below
struct XLauncherConfig
{
char sPlugin[32];
char sComPort[20];
char sPlatform[64];
char sOption[20];
char sChipName[64];
char sBootInterface[20];
char sSecurityProfile[64];
char sBoardName[128];
BOOL bBootDebugTrace;
BOOL bXLoaderDebugTrace;
BOOL bDisable2ndBootUsbDwnld; //It contains 2ndBootUSBFlag
BOOL bEnableUSB; //Contains the value of DownloadLinkType
int nAutoSign;
int m_SignFlag;
BOOL bUartBootByPass;
HANDLE hThread;
int nBaudRate;
int nDataBits;
int nParity;
int nStopBits;
int nFrequency;
eBootScenario iBootScenario;

FileConfig cl_NormalAppli;
FileConfig cl_MemInitFile;
}

Some Points for my console application.
1) It uses various DLL's.
2) when I take global object in place of global pointer to structure I
was bale to access that object in thread function.So what is the
problem with global pointer to structure.


I tried one sample appliccation in which I took one global int pointer
variable and allocated memory to it and assigned some value to it in
main function .Then I created thread and I saw in thread function that
I was able to access global int pointyer easily.

So I am not able to understand why am I not able to get the global
structure pointer in thread function.


Please help me out if you find some solution for the above problem.
Thanks in advancs


Regards
Sachin Ahuja
 
V

Victor Bazarov

Hi All
I am using multithreading in my console application.Here is the
problem.
I have created a global pointer of a structure.I allocated the memory
to this pointer using 'new' operator.The memory gets allocated and I
filled the desired value in this structure using global pointer.
After this I create a thread and reached to thread function.Now I want
to accesss the global pointer in this thread function,but I found that
the globalPointer is pointing to NULL.
[..]

It's likely that your threads each uses its own heap or something of that
nature. The problem is that threads are not part of C++ language and are
off-topic, generally, here. Please consider asking in the newsgroup
dedicated to your OS and/or in 'comp.programming.threads'.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top