성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>DLL에서 STL 객체를 인자/반환값으로 갖는 함수를 제공할 때, 그 함수를 외부에서 사용하는 경우 비정상 종료한다면?</h1> <p> 제목이 복잡하니, 이해를 돕기 위해 예제를 한번 들어볼까요? ^^<br /> <br /> DLL에서 다음과 같은 함수를 export하고 있다고 가정해 보겠습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > #include <string> using namespace std; __declspec(dllexport) int GetLength1(wstring text); int GetLength1(wstring text) { return text.length(); } </pre> <br /> 이 함수를 EXE 또는 다른 DLL 프로젝트에서 이렇게 사용할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > int main() { wstring txt = L"1.0"; GetLength1(txt); } </pre> <br /> 그런데, 다음과 같은 오류가 발생할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Debug Assertion Failed! Program: ...\test.exe File: f:\dd\vctools\crt\crtw32\misc\dbgdel.cpp Line: 52 Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) </pre> <br /> 또는 메시지가 이렇게도 나옵니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Debug Assertion Failed! Program: ...\test.dll File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp Line: 980 Expression: __acrt_first_block == header </pre> <br /> 실제로 위의 문제는 콘솔 프로젝트를 Debug 모드로, DLL 프로젝트를 Release 모드로 빌드했을 때(또는 그 반대로) 재현이 가능하고, Platform Toolset을 변경하는 경우, 구 버전의 Visual Studio로 빌드한 DLL을 사용하는 경우에도 발생할 수 있습니다.<br /> <br /> 이 문제의 근본적인 원인은 이전의 글에 자세하게 설명했습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > DLL에 정의된 C++ template 클래스의 복사 생성자 문제 ; <a target='tab' href='http://www.sysnet.pe.kr/2/0/11153'>http://www.sysnet.pe.kr/2/0/11153</a> </pre> <br /> 즉, STL 객체(이 글의 예제에서는 wstring)가 근본적으로 template 클래스이고 내부에 new/delete 대상이 되는 멤버를 가지고 있으므로 이것을 DLL 너머로 전달했을 때 MSVCRT DLL의 불일치로 발생하는 것입니다.<br /> <br /> (<a target='tab' href='https://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=1111&boardid=331301885'>첨부 파일은 이 글의 예제 코드를 포함</a>합니다.)<br /> <br /> <hr style='width: 50%' /><br /> <br /> 이 문제 관련해서 검색해 보니 다음과 같은 글이 나오는군요.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > DLL내부에서 메모리 할당하고 DLL외부에서 메모리 해제 ; <a target='tab' href='http://jglee.egloos.com/1015547'>http://jglee.egloos.com/1015547</a> </pre> <br /> 해결책은 맞는데, STL 객체 내의 static data 멤버로 발생한다는 원인 분석이 살짝 아쉽군요. ^^ 참고로 위의 블로그에 소개된 마이크로소프트의 KB 자료가 재미있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > How to export an instantiation of a Standard Template Library (STL) class and a class that contains a data member that is an STL object ; <a target='tab' href='http://support.microsoft.com/kb/168958/'>http://support.microsoft.com/kb/168958/</a> You may experience an access violation when you access an STL object through a pointer or reference in a different DLL or EXE ; <a target='tab' href='http://support.microsoft.com/kb/q172396/'>http://support.microsoft.com/kb/q172396/</a> </pre> <br /> 어지간히 많이도 문의가 왔던 것이 아닌가 싶습니다. ^^<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
2082
(왼쪽의 숫자를 입력해야 합니다.)