You may experience an access violation when you access an STL object through a pointer or reference in a different DLL or EXE
Symptoms
Cause
Resolution
- Export accessor methods from the executable image that created the STL object. These methods wrap the required functionality of the STL object. In this way, the STL object will only be directly accessed inside a single executable image. For example, suppose MyProgram.EXE needs to get the next element in deque<MyClass> that resides in MyLibrary.DLL. MyLibrary.DLL could export an accessor method, MyClass* DequeNextItem (/*...*/). Then MyProgram.EXE could execute this method to get the next item in the deque. See the code sample below for a more complete example.
This option works for STL objects that are either global, static, or static data members of a class that are not exported from a DLL. This option will not work for non-static data members of a class that are exported from a DLL or for automatic data.
- Export the template class instantiation from one executable image and import it into the other executable images. For example, if MyLibrary.DLL passes a pointer to vector<MyClass> back to a function in MyProgram.EXE, then export the classes MyClass and vector<MyClass> from MyLibrary.DLL. Then import these classes into MyProgram.EXE. By doing this, you will have one copy of the static class members residing in MyLibrary.DLL. For more information about exporting and importing STL, click the following article number to view the article in the Microsoft Knowledge Base:
168958How to export STL components inside and outside of a class
Status
More Information
Steps to reproduce the behavior
//---------------------------------------------------------
// AVEXE.CPP
// Compile options needed: /GX
#pragma warning (disable : 4786)
#include <map>
#include <string>
#include <stdio.h>
__declspec(dllimport)
std::map<int,std::string>* GiveMeAMap(int n);
__declspec(dllimport)
void ShowMeTheMap(std::map<int,std::string> *amap);
__declspec(dllexport)
const char* MapItemX (std::map<int,std::string> *m, int x);
int main () {
// Create the map in the DLL
int x = 6;
std::map<int,std::string> *p = GiveMeAMap(x);
// Display the contents of the map from the DLL
printf("Showing contents from the DLL\n");
ShowMeTheMap(p);
// Display the contents of the map from the EXE
// using the accessor function from the DLL so we
// aren't directly accessing the map
printf("Showing contents from the EXE using accessor\n");
int i = x;
while (i--) {
printf("%d = %s\n",i,MapItemX(p,i));
}
// Access Violation when accessing the map that
// was created in the DLL from the EXE
printf("Showing contents from the EXE directly\n");
while (x--) {
printf("%d = %s\n",x,(*p)[x].c_str());
}
return 0;
}
//---------------------------------------------------------
// AVDLL.CPP
// Compile options needed /GX
#pragma warning (disable : 4786)
#include <map>
#include <string>
#include <stdlib.h>
// Create the map here in the DLL
__declspec(dllexport)
std::map<int,std::string>* GiveMeAMap(int n) {
std::map<int,std::string> *m = new std::map<int,std::string>;
while(n--) {
char b[33];
itoa(n,b,2);
(*m)[n] = std::string(b);
}
return m;
}
// We can access the map without error from the executable
// image where the map was created
__declspec(dllexport)
void ShowMeTheMap(std::map<int,std::string> *p) {
int x = p->size();
while (x--) {
printf("%d = %s\n",x,(*p)[x].c_str());
}
}
// An accessor method to return the associated C string
// for key x
__declspec(dllexport)
const char* MapItemX (std::map<int,std::string> *m, int x) {
return (*m)[x].c_str();
}
Properties
Article ID: 172396 - Last Review: Sep 2, 2005 - Revision: 1
You may experience an access violation when you access an STL object through a pointer or reference in a different DLL or EXE的更多相关文章
- 如何捕获access violation异常
文章目录 access violation的由来 access violation的实例 Win32 exception SEH异常与C++标准异常 捕获方法 1.access violation的由 ...
- 动态调用DLL函数有时正常,有时报Access violation的异常
动态调用DLL函数有时正常,有时报Access violation的异常 typedef int (add *)(int a,int b); void test() { hInst=LoadL ...
- 解决 “access violation at address xxxxxxxxx”错误
在进行磁盘整理的时候,打开Foxmail的时候出现了“access violation at address32383137”错误 和“access violation at address00000 ...
- STM32 KEIL不能输入仿真引脚端口error 65: access violation at 0x40021000 : no 'read' permission
使用MDK自己创建一个STM32F103ZE核的项目 加入源码后编译,正常,在线仿真单步执行出现如下问题 error 65: access violation at 0x40021000 : no ' ...
- QDomDocument Access violation writing location
今天犯了一个非常2的错误! 为了将面板参数保存起来,选择用QDomDocument构造Dom树,然后用doc.toString()方法返回符合xml格式的QString.如: QString CutF ...
- Access Violation at address 00000000.Read of address 00000000 解决办法
是数组越标或没有初始化某个对象之类的问题,搂住细细检查一下代码, 使用指针前未做检查,而这个指针未初始化. 可能是new后没有delete,这样出现溢出的可能性比较大 检查代码或者跟踪试试 使 ...
- u-boot TFTP: 'Access violation' (2)
今天做tftp下载时间会遇到以下问题. --->8--- Load address: 0x20000000 Loading: * TFTP error: 'Access violation' ( ...
- laravel migrate时报错:Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
今天在学习laravel的路由模型绑定时,在按照文档执行php artisan migrate时报错. In Connection.php line 664: SQLSTATE[42000]: Syn ...
- std::vector push_back报错Access violation
C/C++ code ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include < ...
随机推荐
- java基础之基础语法详录
[前言] java的语法先从基础语法学,Java语言是由类和对象组成的,其对象和类又是由方法和变量组成,而方法,又包含了语句和表达式. 对象:(几乎)一切都是对象,比如:一只熊猫,他的外观,颜色,他在 ...
- [转]linux文件批量转码
linux系统里提供的文件转化编码的命令iconv,例如: iconv -t utf- -f gb2312 -c test.xml > text_UTF8.xml -f 源编码-t 目标编码 ...
- gRPC 的route_guide例子
本文的例子代码在: https://github.com/grpc/grpc-go/tree/master/examples/route_guide 功能就类似目前LBS一样,在每个位置上报一些文 ...
- fontforge制作自定义字体及在手机上应用举例——张鑫旭
一.看似无关紧要的事件背景 之所以花时间折腾fontforge这个软件,去制作什么自定义的字体是有原因滴. 之前提过,最近我抽空将公司的手机软件HTML5网页化.期间碰到这么一个问题,页面低栏上的电话 ...
- 阿里云服务器被他人通过SSH暴力破解后的安全加固
背景说明:我登录阿里云服务器控制台时,收到几条安全警告信息. 从图中可以知道,对方的IP地址是47.97.68.118,通过SSH登录方式,登录时用我服务器里的admin用户,然后用穷举法暴力破解ad ...
- asp.ne如何使用javascript去验证客户端信息,如果验证成功则送往服务器端处理,否则在客户端提示用户(不返回到服务器端处理)
一.问题 在网站一般都有很多地方需要用户去填写一些信息,然后用户点击提交,将信息送往后台储存到数据库中.在这一个过程我以前做法直接在button的click事件中来判断用户输入的数据是否完整和合法,虽 ...
- 常用数据库validationQuery语句
validationQuery是用来验证数据库连接的查询语句,这个查询语句必须是至少返回一条数据的SELECT语句.每种数据库都有各自的验证语句,下表中收集了几种常见数据库的validationQue ...
- redis事务报错No ongoing transaction. Did you forget to call multi?
场景:需要存两条数据到redis中,并且两条要么都存要么都不存,需要事务来控制 Spring Data Redis的RedisTemplate提供了MULTI.EXEC命令进行封装,远看可以解决问题, ...
- HTML存储详解
和大家一起先来了解一下H5之前的存储方式: cookies的诞生: http请求头上带着数据 大小只能为4K 主Domain的污染 下面是百度的一些Cookies HTTP中带√的表示,只能被服务器端 ...
- 如何发挥ERP系统中的财务监控职能?
ERP系统的管理理念与特点 ERP,是整合了企业管理理念.业务流程.基础数据.人力物力.计算机硬件和软件于一体的企业资源管理系统.ERP系统运用信息技术将企业的资金流.物资流.信息流进行有效的集成,使 ...