BSTR을 만들고 COM 개체 간에 전달할 때는 BSTR에서 사용하는 메모리 처리에 주의하여 메모리 누수를 방지해야 합니다. BSTR이 인터페이스 내부에 있을 경우 사용한 메모리는 반드시 해제해야 합니다. 그러나 BSTR이 인터페이스 외부로 전달되는 경우에는 받는 개체에서 메모리를 관리합니다.
BSTR에 대해 메모리를 할당하고 할당된 메모리를 해제하는 일반적인 규칙은 다음과 같습니다.
- BSTR 인수가 필요한 함수를 호출할 때는 호출하기 전에 BSTR에 대한 메모리를 할당한 후 나중에 해제합니다. 다음 코드에서는 이러한 예를 보여 줍니다.
HRESULT IWebBrowser2::put_StatusText( BSTR bstr );
// shows using the Win32 function
// to allocate memory for the string:
BSTR bstrStatus = ::SysAllocString( L"Some text" );
if (bstrStatus == NULL)
return E_OUTOFMEMORY;
pBrowser->put_StatusText( bstrStatus );
// Free the string:
::SysFreeString( bstrStatus );
//...
- BSTR을 반환하는 함수를 호출할 때는 문자열을 직접 해제해야 합니다. 다음 코드에서는 이러한 예를 보여 줍니다.
HRESULT IWebBrowser2::get_StatusText( BSTR FAR* pbstr );
//...
BSTR bstrStatus;
pBrowser->get_StatusText( &bstrStatus );
// shows using the Win32 function
// to freee the memory for the string:
::SysFreeString( bstrStatus );
- BSTR을 반환하는 함수를 구현할 때는 문자열을 할당만 하고 해제하지는 않습니다. 해당 함수를 받는 개체에서 메모리를 해제합니다. 다음 코드에서는 이러한 예를 보여 줍니다.
// Example shows using MFC's
// CString::AllocSysString
//...
HRESULT CMyClass::get_StatusText( BSTR * pbstr )
{
try
{
//m_str is a CString in your class
*pbstr = m_str.AllocSysString( );
}
catch (...)
{
return E_OUTOFMEMORY;
}
// The client is now responsible for freeing pbstr.
return( S_OK );
}
//...
No | Writer | Date | Cnt. | Title | File(s) |
11895 | 정성태 | 5/12/2019 | 22555 | .NET Framework: 832. ML.NET Model Builder - 회귀(Regression), 다중 분류(Multi-class classification) 예제 | 1 |
11894 | 정성태 | 5/10/2019 | 24517 | VS.NET IDE: 135. Visual Studio - ML.NET Model Builder 소개 [5] | |
11893 | 정성태 | 5/10/2019 | 20584 | 오류 유형: 535. C# 6.0 이상의 문법을 컴파일 시 오류가 발생한다면? | |
11892 | 정성태 | 5/10/2019 | 20494 | 웹: 38. HTTP Cookie의 expires 시간 형식(RFC7231) | |
11891 | 정성태 | 5/9/2019 | 23621 | .NET Framework: 831. (번역글) .NET Internals Cookbook Part 12 - Memory structure, attributes, handles | |
11890 | 정성태 | 5/8/2019 | 19279 | 개발 환경 구성: 439. "Visual Studio Enterprise is required to execute the test." 메시지와 관련된 코드 기록 | |
11889 | 정성태 | 5/8/2019 | 19320 | 개발 환경 구성: 438. mstest, QTAgent의 로그 파일 설정 방법 | |
11888 | 정성태 | 5/8/2019 | 37230 | .NET Framework: 830. C# - 비동기 호출을 취소하는 CancellationToken의 간단한 예제 코드 [1] | 1 |
11887 | 정성태 | 5/8/2019 | 23069 | .NET Framework: 829. C# - yield 문을 사용할 수 있는 메서드의 조건 | |
11886 | 정성태 | 5/7/2019 | 20124 | 오류 유형: 534. mstest.exe 실행 시 "Visual Studio Enterprise is required to execute the test." 오류 [2] | |
11885 | 정성태 | 5/7/2019 | 17599 | 오류 유형: 533. mstest.exe 실행 시 "File extension specified '.loadtest' is not a valid test extension." 오류 발생 | |
11884 | 정성태 | 5/5/2019 | 22513 | .NET Framework: 828. C# DLL에서 Win32 C/C++처럼 dllexport 함수를 제공하는 방법 - 두 번째 이야기 | |
11883 | 정성태 | 5/3/2019 | 27516 | .NET Framework: 827. C# - 인터넷 시간 서버로부터 받은 시간을 윈도우에 적용하는 방법 | 1 |
11882 | 정성태 | 5/2/2019 | 23977 | .NET Framework: 826. (번역글) .NET Internals Cookbook Part 11 - Various C# riddles | 1 |
11881 | 정성태 | 4/28/2019 | 24081 | 오류 유형: 532. .NET Core 프로젝트로 마이그레이션 시 "CS0579 Duplicate 'System.Reflection.AssemblyCompanyAttribute' attribute" 오류 발생 | |
11880 | 정성태 | 4/25/2019 | 19832 | 오류 유형: 531. 이벤트 로그 오류 - Task Scheduling Error: m->NextScheduledSPRetry 1547, m->NextScheduledEvent 1547 | |
11879 | 정성태 | 4/24/2019 | 28747 | .NET Framework: 825. (번역글) .NET Internals Cookbook Part 10 - Threads, Tasks, asynchronous code and others | 2 |
11878 | 정성태 | 4/22/2019 | 24006 | .NET Framework: 824. (번역글) .NET Internals Cookbook Part 9 - Finalizers, queues, card tables and other GC stuff | 1 |
11877 | 정성태 | 4/22/2019 | 24221 | .NET Framework: 823. (번역글) .NET Internals Cookbook Part 8 - C# gotchas | 1 |
11876 | 정성태 | 4/21/2019 | 22496 | .NET Framework: 822. (번역글) .NET Internals Cookbook Part 7 - Word tearing, locking and others | 1 |
11875 | 정성태 | 4/21/2019 | 24315 | 오류 유형: 530. Visual Studo에서 .NET Core 프로젝트를 열 때 "One or more errors occurred." 오류 발생 | |
11874 | 정성태 | 4/20/2019 | 24339 | .NET Framework: 821. (번역글) .NET Internals Cookbook Part 6 - Object internals | 1 |
11873 | 정성태 | 4/19/2019 | 23013 | .NET Framework: 820. (번역글) .NET Internals Cookbook Part 5 - Methods, parameters, modifiers | 1 |
11872 | 정성태 | 4/17/2019 | 23900 | .NET Framework: 819. (번역글) .NET Internals Cookbook Part 4 - Type members | 1 |
11871 | 정성태 | 4/16/2019 | 21793 | .NET Framework: 818. (번역글) .NET Internals Cookbook Part 3 - Initialization tricks [3] | 1 |
11870 | 정성태 | 4/16/2019 | 20681 | .NET Framework: 817. Process.Start로 실행한 콘솔 프로그램의 출력 결과를 얻는 방법 | 1 |