1

I am currently working on a project where I have to build a shell for a C++ dll, so a new C# GUI can use its functions. However I got the following problem, in the C++ portion I have to create a new thread for specific reasons, and I want to pass an int array to the new thread. Note that the values assigned to the array in the function in which this happens are gained from the C# portion of the code.

__declspec( dllexport ) void CreateReportPane(int &id, int &what)
{
    DWORD threadId; 
    int iArray[2] = { id, what};    

    HANDLE hThread = CreateThread( NULL, 0, CreateReportPaneThread, iArray, 0,  &threadId);
    if (hThread == NULL) 
    {           
        ExitProcess(3);
    }    
}

The problem arises in the new thread, I can reliably fetch the first value out of the array, but the 2nd value seems to be released, here is the code on the other side.

DWORD WINAPI CreateReportPaneThread(LPVOID lparam)
{
    int id, what;
    id = *(( int * )lparam);
    what = *(((int *)lparam)+1) ; 
    CreateReportPaneOriginal(id, what);

    return 0;
}

Is there any way to prevent the values in the array from getting released while not holding the original thread captive? A big thank you in advance

2
  • Don't forget to CloseHandle(hThread), else you're going to leak handles and memory. Commented Nov 8, 2012 at 9:06
  • I would probably create a structure for this, but if you want to use an array why don't you use array notation in the thread? E.g. int* iArray = (int*) lparam; int id = iArray[0]; int what = iArray[1]; Commented Nov 8, 2012 at 9:09

2 Answers 2

3
int iArray[2] = { id, what};    

HANDLE hThread = CreateThread(...,CreateReportPaneThread, iArray, ...);

The problem is that iArray is a local array which means this gets destroyed when the function CreateReportPane() returns. So what CreateReportPaneThread() refers to doesn't exist. You get the first value just by chance. There is no such guarantee that you would get even the first value.

Use dynamic array:

int * iArray  = new int[2];
iArray[0] = id;
iArray[1] = what;

HANDLE hThread = CreateThread(...,CreateReportPaneThread, iArray, ...);

Remember to write deallocate the array once you're done with it in CreateReportPaneThread:

DWORD WINAPI CreateReportPaneThread(PVOID *data)
{
     int *array = static_cast<int*>(data);

     int id = array[0], what = array[1];

     delete []array; //MUST DO IT to avoid memory leak!

     //rest of your code
}
Sign up to request clarification or add additional context in comments.

3 Comments

Ah yes ofcourse, thank you very much people, it solved my problem!
@wyndesmit: If it solved your problem, you should accept this as "answer" by clicking on the tick mark.
ah yes, I am still new to this interface :)
2

Dynamically allocate the array to prevent the array going out of scope when CreateReportPane() exits:

int* iArray = new int[2];
iArray[0] = id;
iArray[1] = what;    

otherwise the thread is accessing an array that is no longer valid, which is undefined behaviour. The thread routine CreateReportPaneThread() must then delete[] the array when it is no longer required (note the use of delete[] and not delete).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.