I am using PInvoke to pass the following class to unmanaged C++ when the app starts up. All the values are passed in correctly as I step through the code. Inside the called C++ method I save a the ptr to the passed in struct.
[StructLayout(LayoutKind.Sequential)]
public class Config
{
public int Speed;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.LPStr, SizeConst = 2)]
public string[] Settings = new string[2];
}
Matching C++ struct
struct config
{
int Speed;
char* Settings[2];
}
This is the method in C++ that saves ptr to the class that is passed in
Config *m_config;
void SetConfig(Config *config)
{
m_config = config;
}
Question: if the marshaller allocated memory for the C++ struct at the time of marshaling when does it free the memory?
If I save the ptr to the passed in struct, will that ptr be invalid as soon as the function returns to C#?
Is there a way to tell the marshaller to NOT deallocate the memory it allocated during marshaling?
Thanks for any info or suggestions.
Mitch