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++)
cTasks = 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++)
{
cTasks ->ProjectTaskID = dsTasks->ReadString(S "ProjectTaskID");
cTasks ->ProjectComponentID = dsTasks->ReadString(S "ProjectComponentID");
cTasks ->IsDetailedSchedule = dsTasks->ReadShort( "IsDetailedSchedule");
cTasks ->IsComplete = dsTasks->ReadShort( "IsComplete");
cTasks ->IsStartDateFixed = dsTasks->ReadShort( "IsStartDateFixed");
cTasks ->Duration = Convert::ToUInt32(dsTasks->ReadString(S "Duration"));
cTasks ->StartDate = dsTasks->ReadDateTime( "StartDate");
cTasks ->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++16
|