Returning WCHAR* variable;
-
I am trying to return a WCHAR* from a function. The WCHAR* variable will be Marshalled to managed code with Marshal.PtrToStringUni. If I don't new it then PtrToStringUni returns the wrong result or nothing. This does not work.
WCHAR\* GetRecorderDriveLetter() { WCHAR drive\[4\] = {0}; m\_hr = (\*this)->GetRecorderDriveLetter(drive, 4); //TRACEHR(\_T("CCDBurn::GetRecorderDriveLetter"),m\_hr); return (drive); }
This does.
WCHAR\* GetRecorderDriveLetter() { WCHAR\* drive = new WCHAR\[4\]; m\_hr = (\*this)->GetRecorderDriveLetter(drive, 4); //TRACEHR(\_T("CCDBurn::GetRecorderDriveLetter"),m\_hr); return (drive); }
What I am worried about is a memory leak. How does the memory get freed? Thank You Bo Hunter
-
I am trying to return a WCHAR* from a function. The WCHAR* variable will be Marshalled to managed code with Marshal.PtrToStringUni. If I don't new it then PtrToStringUni returns the wrong result or nothing. This does not work.
WCHAR\* GetRecorderDriveLetter() { WCHAR drive\[4\] = {0}; m\_hr = (\*this)->GetRecorderDriveLetter(drive, 4); //TRACEHR(\_T("CCDBurn::GetRecorderDriveLetter"),m\_hr); return (drive); }
This does.
WCHAR\* GetRecorderDriveLetter() { WCHAR\* drive = new WCHAR\[4\]; m\_hr = (\*this)->GetRecorderDriveLetter(drive, 4); //TRACEHR(\_T("CCDBurn::GetRecorderDriveLetter"),m\_hr); return (drive); }
What I am worried about is a memory leak. How does the memory get freed? Thank You Bo Hunter
Ok, the reason the first isn't working is because you are returning a pointer to local routine memory. This is memory on the stack. When the routine exits, the memory is removed from the stack and will soon contain other information. In your second example, memory will need to be freed by the caller. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
I am trying to return a WCHAR* from a function. The WCHAR* variable will be Marshalled to managed code with Marshal.PtrToStringUni. If I don't new it then PtrToStringUni returns the wrong result or nothing. This does not work.
WCHAR\* GetRecorderDriveLetter() { WCHAR drive\[4\] = {0}; m\_hr = (\*this)->GetRecorderDriveLetter(drive, 4); //TRACEHR(\_T("CCDBurn::GetRecorderDriveLetter"),m\_hr); return (drive); }
This does.
WCHAR\* GetRecorderDriveLetter() { WCHAR\* drive = new WCHAR\[4\]; m\_hr = (\*this)->GetRecorderDriveLetter(drive, 4); //TRACEHR(\_T("CCDBurn::GetRecorderDriveLetter"),m\_hr); return (drive); }
What I am worried about is a memory leak. How does the memory get freed? Thank You Bo Hunter
- Change the variable WCHAR drive[4] as a class member. 2) Or alter the function prototype into something like this: some_return_type GetRecorderDriveLetter(WCHAR* pDrv); Or even wrap the WCHAR array as an user type object (struct), then: some_return_type GetRecorderDriveLetter(DriveObj& ref); 3) Or, just as Tim said, // ...... WCHAR* pp = GetRecorderDriveLetter(); delete[] pp; ;P Maxwell Chen
-
- Change the variable WCHAR drive[4] as a class member. 2) Or alter the function prototype into something like this: some_return_type GetRecorderDriveLetter(WCHAR* pDrv); Or even wrap the WCHAR array as an user type object (struct), then: some_return_type GetRecorderDriveLetter(DriveObj& ref); 3) Or, just as Tim said, // ...... WCHAR* pp = GetRecorderDriveLetter(); delete[] pp; ;P Maxwell Chen
I second Maxwell's second proposal: change the function to
HRESULT GetRecorderDriveLetter( WCHAR* pDrv );
and P/Invoke as follows:
[DllImport(DLL_NAME, CharSet=CharSet.Unicode, ExactSpelling=true)]
private static extern int GetRecorderDriveLetter( StringBuilder pDrv );Make sure the
StringBuilder
you pass in has an appropriateCapacity
for the called function. Stability. What an interesting concept. -- Chris Maunder