tmccoy00
July 9th, 2007, 09:51 PM
I'm trying to allocate an array of managed structures inside a library, and return the allocated arrays back to the caller. I now realise this is a violation of scoping, and could do with some assistance to rectify this.
Library
public: ref struct Record
{
DateTime Timestamp;
Int32 Value;
};
public: myPublicLibraryFunction(array<Record^> ^Record);
...
int size = 50;
array<Record^> ^myArray = gcnew array<Record^>(size);
...
Record ^myRecord = gcnew Record^;
Record->Value = 5;
Record->Timestamp = DateTime::Now;
myArray->SetValue(Record, 0);
Library User
array<Record^> ^myUserArray;
...
myLibrary::myPublicLibraryFunction(myUserArray);
...but the end result is that the myUserArray appears to not be allocated when returning from the library call. Interestingly, when inside the library call, the array is allocated and available correctly - it appears to be suffering from garbage collection when the compiler thinks it's out of scope at the end of the myPublicLibraryFunction call.
This is showing holes in my understanding of Visual Studio 2005 memory allocation, but the short of what I want is to have pointers to arrays in the user code, have the arrays allocated inside the library and then accessible when returning to user code.
I can see why this is happening - it's a scope issue or similar, but am struggling to battle this.
Library
public: ref struct Record
{
DateTime Timestamp;
Int32 Value;
};
public: myPublicLibraryFunction(array<Record^> ^Record);
...
int size = 50;
array<Record^> ^myArray = gcnew array<Record^>(size);
...
Record ^myRecord = gcnew Record^;
Record->Value = 5;
Record->Timestamp = DateTime::Now;
myArray->SetValue(Record, 0);
Library User
array<Record^> ^myUserArray;
...
myLibrary::myPublicLibraryFunction(myUserArray);
...but the end result is that the myUserArray appears to not be allocated when returning from the library call. Interestingly, when inside the library call, the array is allocated and available correctly - it appears to be suffering from garbage collection when the compiler thinks it's out of scope at the end of the myPublicLibraryFunction call.
This is showing holes in my understanding of Visual Studio 2005 memory allocation, but the short of what I want is to have pointers to arrays in the user code, have the arrays allocated inside the library and then accessible when returning to user code.
I can see why this is happening - it's a scope issue or similar, but am struggling to battle this.