Returning Managed __gc Class array to function parameter... parameter stays <undefined value>  
Author Message
RyanH314





PostPosted: Visual C++ Language, Returning Managed __gc Class array to function parameter... parameter stays <undefined value> Top

 

Hi there,

I have a function which is to Enumerate Project Tasks. Each Task goes into it's own __gc class, and therefore is in a class array. With unmanaged C++ it is easy to change the data a pointer points to. However, this managed C++ seems to garbage collect when the original pointer leaves the scope.

I am using /oldSyntax line.

Here is some brief code of my function. Please note that CSchedTask is __gc class CSchedTask.


int EnumScheduleTasks(String *ProjectID, CSchedTask *Tasks[])

{

MyDataset *dsTasks = new MyDataset();

//... more code is here which defines iTasks, int iTasks = ...;

//Initialize array.

CSchedTask __pin *cTasks[] = new CSchedTask*[iTasks];

for(int i=0;i<iTasks;i++)

cTasksIdea = new CSchedTask();

//Enumerate tasks.

strQuery = S"SELECT * FROM HS_ProjectTask WHERE ProjectID = '";

cstrcat(&strQuery, ProjectID);

cstrcat(&strQuery, S"' AND ProjectElementID Is NULL ORDER BY Sequence");

dsTasks->SQLText = strQuery;

//Populate cTasks class array with Tasks values.

for(int i=0;i<iTasks;i++)

{

cTasksIdea->ProjectTaskID = dsTasks->ReadString(S"ProjectTaskID");

cTasksIdea->ProjectComponentID = dsTasks->ReadString(S"ProjectComponentID");

cTasksIdea->IsDetailedSchedule = dsTasks->ReadShort("IsDetailedSchedule");

cTasksIdea->IsComplete = dsTasks->ReadShort("IsComplete");

cTasksIdea->IsStartDateFixed = dsTasks->ReadShort("IsStartDateFixed");

cTasksIdea->Duration = Convert::ToUInt32(dsTasks->ReadString(S"Duration"));

cTasksIdea->StartDate = dsTasks->ReadDateTime("StartDate");

cTasksIdea->EndDate = dsTasks->ReadDateTime("EndDate");

dsTasks->MoveNext();

}

//Tasks array now is equal to cTasks array. After this function ends, the array Tasks points to is <undefined value>

Tasks = cTasks;

return iTasks;

}


The calling code is below:

CSchedTask *cTask[];

int iTasks = EnumScheduleTasks(S"C7D2D63F2FD84C18AD9EB9BBF66D585E", cTask);

 

At this point cTask is undefined and contains no data.

Any help is greatly appreciated. I'm likely missing something incredibly simple.



Visual C++13  
 
 
Mike Danes





PostPosted: Visual C++ Language, Returning Managed __gc Class array to function parameter... parameter stays <undefined value> Top

This has nothing to do with garbage collection. The same thing will happen in native C++ when you pass an array (or pointer) to a function. You can change the contents of the array but you cannot change the array (pointer). You need a pointer to pointer paramter to do that:

int EnumScheduleTasks(String *ProjectID, CSchedTask* (*Tasks)[])

{

... your code

*Tasks = cTasks; // set the new array

return iTasks;

}

The calling code is below:

CSchedTask *cTask[];

int iTasks = EnumScheduleTasks(S"C7D2D63F2FD84C18AD9EB9BBF66D585E", &cTask); // pass the address of cTask


 
 
RyanH314





PostPosted: Visual C++ Language, Returning Managed __gc Class array to function parameter... parameter stays <undefined value> Top

Hello Mike,

Thankyou for your timely reply. This has helped me greatly.

-Ryan