Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Returning WCHAR* variable;

Returning WCHAR* variable;

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bo Hunter
    wrote on last edited by
    #1

    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

    T M 2 Replies Last reply
    0
    • B 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

      T Offline
      T Offline
      Tim Smith
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • B 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

        M Offline
        M Offline
        Maxwell Chen
        wrote on last edited by
        #3
        1. 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
        M 1 Reply Last reply
        0
        • M Maxwell Chen
          1. 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
          M Offline
          M Offline
          Mike Dimmick
          wrote on last edited by
          #4

          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 appropriate Capacity for the called function. Stability. What an interesting concept. -- Chris Maunder

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups