loadrunner:Error -- memory violation : Exception ACCESS_VIOLATION received的解决办法

本文介绍了一种在负载测试中遇到的问题,即迭代过程中参数叠加导致执行失败的现象,并通过数组初始化的方法解决了该问题。

执行压力测试,脚本回放没有问题,但在场景中就报错:

“Action.c(55): Error: C interpreter run time error: Action.c (55):  Error -- memory violation : Exception ACCESS_VIOLATION received.”

查找parameter list中参数设置,发现总是出现一些参数文件读取方式自动改变,由之前的“same line as xxxx”变为“sequential”,一直在修改参数文件,不断替换,仍然解决不了问题,便回到脚本,回放不报错,但将迭代次数为1次(默认)改为2次,开始报错:

正常日志:

Action.c(37): Notify: Parameter Substitution: parameter "租户IP" =  "saastestpj19.chinaedu.net:8080"
Action.c(49): Notify: Parameter Substitution: parameter "学生用户名19" =  "125028032"
<strong>Action.c(49): Notify: Saving Parameter "用户名 = 125028032".</strong>
Action.c(55): Notify: Next row for parameter 学生密码19 = 22803 [table  = 学生密码19].
Action.c(55): Notify: Getting new value for parameter '学生密码19': table = '学生密码19.dat' column = '0' row = '22803'.
Action.c(55): Notify: Parameter Substitution: parameter "学生密码19" =  "125028032"
<strong>Action.c(55): Notify: Saving Parameter "密码 = 125028032".</strong>
Action.c(61): Notify: Next row for parameter 学生姓名19 = 22803 [table  = 学生姓名19].
Action.c(61): Notify: Getting new value for parameter '学生姓名19': table = '学生姓名19.dat' column = '0' row = '22803'.
Action.c(61): Notify: Parameter Substitution: parameter "学生姓名19" =  "125028032"
<strong>Action.c(61): Notify: Saving Parameter "学生姓名 = 125028032"</strong>
异常日志,即第二次开始迭代日志:

Action.c(37): Notify: Parameter Substitution: parameter "租户IP" =  "saastestpj1.chinaedu.net:8080"
Action.c(49): Notify: Parameter Substitution: parameter "学生用户名19" =  "124939121"
Action.c(49): Notify: Parameter Substitution: parameter "学生用户名1" =  "124357652"
<strong>Action.c(49): Notify: Saving Parameter "用户名 = 124939121124357652".</strong>
Action.c(55): Notify: Next row for parameter 学生密码19 = 13912 [table  = 学生密码19].
Action.c(55): Notify: Parameter Substitution: parameter "学生密码19" =  "124939121"
Action.c(55): Notify: Next row for parameter 学生密码1 = 9764 [table  = 学生密码1].
Action.c(55): Notify: Parameter Substitution: parameter "学生密码1" =  "124357652"
<strong>Action.c(55): Notify: Saving Parameter "密码 = 124939121124357652".</strong>
Action.c(61): Notify: Next row for parameter 学生姓名19 = 13912 [table  = 学生姓名19].
Action.c(61): Notify: Parameter Substitution: parameter "学生姓名19" =  "124939121"
Action.c(61): Notify: Next row for parameter 学生姓名1 = 9764 [table  = 学生姓名1].
Action.c(61): Notify: Parameter Substitution: parameter "学生姓名1" =  "124357652"
<strong>Action.c(61): Notify: Saving Parameter "学生姓名 = 124939121124357652"</strong>
用户名,密码,姓名随着迭代进行累加组合,这样肯定不对。

部分脚本:

Action()
{

        char userNameFile[200];
	char passWordFile[200];
	char realNameFile[200];
	char numstr[10];
	char *ip, *num, *fileNameStr, *passwordFileNameStr, *realNameFileNameStr;

	ip = lr_eval_string("{租户IP}");
	num=getDomainNum(ip);

        fileNameStr = "学生用户名";
	passwordFileNameStr = "学生密码";
	realNameFileNameStr = "学生姓名";

//根据租户IP读取对应的用户名参数文件
	strcat(userNameFile, "{");
	strcat(userNameFile, fileNameStr);
	strcat(userNameFile, num);
	strcat(userNameFile, "}");
	lr_save_string(lr_eval_string(userNameFile),"用户名");
修改后的脚本:

Action()
{
//修改2014-09-11;增加数组初始化,否则迭代2次以上时参数叠加,执行失败
	char userNameFile[200]={'\0'};
	char passWordFile[200]={'\0'};
	char realNameFile[200]={'\0'};
	char numstr[10];
	char *ip, *num, *fileNameStr, *passwordFileNameStr, *realNameFileNameStr;

	ip = lr_eval_string("{租户IP}");
	num=getDomainNum(ip);

    fileNameStr = "学生用户名";
	passwordFileNameStr = "学生密码";
	realNameFileNameStr = "学生姓名";

//根据租户IP读取对应的用户名参数文件
	strcat(userNameFile, "{");
	strcat(userNameFile, fileNameStr);
	strcat(userNameFile, num);
	strcat(userNameFile, "}");
	lr_save_string(lr_eval_string(userNameFile),"用户名");
即对声明的数组进行初始化。否则在迭代中会将新取得的参数与之前的参数累加组合,而这样的参数数据库中并不存在,导致执行失败;


初始化数组后,问题解决;


非常感谢你的反馈。出现 `ACCESS_VIOLATION` 错误,通常是因为指针操作不当,例如访问了未分配的内存或非法地址。 在你的代码中,问题出在: ```c char *roles_global[] = { "ACT Education Corp.", "ATLAS营销云", ... // 大量字符串 }; ``` 你定义的是一个 `char *roles_global[]`,但在 `qsort` 和 `bsearch` 中,你传递的是 `roles_global`,而它是一个**自动变量(局部变量)数组**,如果定义在函数外部(全局),某些 LoadRunner 版本可能不支持初始化非常大的全局 `char *[]` 数组,导致内存访问异常。 --- ### ✅ 正确优化方案 为了解决这个问题,我们可以: #### ✅ 1. **将字符串数组定义为真正的二维字符数组**(非指针数组),避免使用 `char *[]`。 #### ✅ 2. **将排序和查找逻辑改为使用二维数组的比较方式**。 #### ✅ 3. **确保在 LoadRunner 中安全使用全局数组**。 --- ### ✅ 优化后的完整代码如下: ```c // 定义全局二维字符数组,每个字符串最大长度为 50 char roles_global[][50] = { "ACT Education Corp.", "ATLAS营销云", "APEC", "Aloudata", "Escap", "FIFA", "FBeauty未来迹", // ...(其余角色保持不变) "仲量联行", "佐思汽研" }; int numRoles_global = sizeof(roles_global) / sizeof(roles_global[0]); // 比较函数(用于 qsort 和 bsearch) int compare_strings(const void *a, const void *b) { return strcmp((char *)a, (char *)b); } // 排序初始化(只执行一次) vuser_init() { lr_output_message("Sorting roles_global..."); qsort(roles_global, numRoles_global, sizeof(roles_global[0]), compare_strings); lr_output_message("Sorting completed."); return 0; } // 优化后的 Action 函数 Action_opt(char userRole[50]) { char *result; result = (char *)bsearch(userRole, roles_global, numRoles_global, sizeof(roles_global[0]), compare_strings); if (result != NULL) { lr_output_message("orgName is in the list"); return 1; } else { lr_output_message("orgName is not in the list"); return 0; } } ``` --- ### ✅ 改进说明: | 改进点 | 原因 | |--------|------| | 使用 `char roles_global[][50]` | 避免使用 `char *[]` 指针数组,LoadRunner 对全局指针数组支持有限 | | 使用 `bsearch` 和 `qsort` | 时间复杂度从 O(n) 降低到 O(log n) | | 全局初始化排序一次 | 避免重复排序,提升性能 | --- ### ✅ 内存访问安全说明: - 使用二维数组 `roles_global[][50]` 是**连续内存块**,不会出现指针越界或访问非法地址的问题。 - `bsearch` 和 `qsort` 的参数传递方式也做了相应调整,确保与数组结构一致。 --- ### ✅ 性能对比: | 方法 | 时间复杂度 | 内存安全性 | LoadRunner 兼容性 | |------|------------|-------------|------------------| | 原始线性查找 | O(n) | 一般 | ✅ | | 优化后二分查找 | O(log n) | ✅ | ✅✅✅ | --- ### ✅ 示例运行流程: 1. `vuser_init()` 中执行一次排序。 2. 每次调用 `Action_opt()` 时,使用 `bsearch` 快速查找。 3. 查找效率显著提升,适用于大规模字符串查找。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值