【发布时间】:2021-05-18 06:30:09
【问题描述】:
我正在创建一个 MEX,以使用 A* 算法查找两个节点之间的路径。代码按预期工作,检索到正确的结果,一切似乎都很好,但是当我从调用 MEX 的方法返回时,Matlab 只是自行关闭。
据我所知,有时当用户尝试创建变量的副本时,Matlab 会创建一个指向相同内存地址的指针(IE A = 3;B = A,然后 A 和 B 指向相同的内存地址,即使尽管 Matlab 将它们显示为 2 个自变量)我使用了一个旧技巧,即直接对变量的副本执行操作,即使它是一个愚蠢的操作,Matlab 也会认为这两个变量不再相同并且会创建它的副本(即 A = 3;B = A;B = B+0,现在 A 和 B 存储为不同的独立变量)。
因此,我发现解决此问题的唯一方法是执行以下函数 getWP 中显示的操作:
function testAStarC()
% load the map of the area
load('epsp0_2_nav.mat');
% Set the initial node
initNode = '16x21';
% Set the target node
finalNode = '-15x54';
% Select Heuristic
heuristic = 'Manhattan';
% Create a target location (targetX, targetY, targetAngle)
targetCoords = [-15*110 54*110 0.15];
% Function that hosts the call to the MEX
wp = getWP(map, initNode, finalNode, targetCoords, heuristic);
disp('If you remove the line cellNodes{keyID}.x = cellNodes{keyID}.x; from getWP ...
I wont reach this line');
disp(['Route with ',num2str(length(wp)),' wp found']);
disp('done');
function waypointsList = getWP(map, initNode, finalNode, targetCoords, heuristic)
% HashMap containing the nodes (this is a Java hashmap)
nodesHash = map.navMap.nodes.nodes;
keys = nodesHash.keys();
numNodes = length(keys);
cellNodes = cell(1,numNodes);
% Parse the nodes from the HashMap to Cells as the MEX seems to be
% unable to read directly from the Java HashMap
for keyID=1:numNodes
cellNodes{keyID} = nodesHash(keys{keyID});
%---------------------------------------------------------
% WITHOUTH THIS MATLAB CRASHES WHEN RETURNING FROM GETWP
%---------------------------------------------------------
% We need this to force Matlab to create a new copy of the content,
% otherwise will send a pointer aiming to the HashMap and crash when
% returning from getWP.
cellNodes{keyID}.x = cellNodes{keyID}.x;
end
waypointsList = AStar(cellNodes, initNode, finalNode, targetCoords, heuristic, 1);
disp('I am not crashing here if you remove cellNodes{keyID}.x = cellNodes{keyID}.x');
我的第一个想法是我在 MEX 中对“cellNodes”做错了,这导致 Matlab 崩溃,但我没有直接使用输入参数执行任何操作。这是 Node 类的构造函数:
节点.cpp
Node::Node(mxArray *cellElement)
{
double *xIn;
double *yIn;
char strIn[15];
double *posXIn;
double *posYIn;
double *weightIn;
double *tempVal;
size_t numCellElms;
mxArray *cellElement2;
numCellElms = mxGetNumberOfFields(cellElement);
size_t size;
for (int cellItem = 0; cellItem < numCellElms; cellItem++)
{
cellElement2 = mxGetCell(cellElement,cellItem);
if (cellItem == 0)
{
xIn = mxGetPr(cellElement2);
memcpy(tempVal,xIn,sizeof(double));
gridX = int(*tempVal);
}
if (cellItem == 1)
{
yIn = mxGetPr(cellElement2);
memcpy(tempVal,yIn,sizeof(double));
gridY = int(*tempVal);
}
if (cellItem >= 2 && cellItem < 10)
{
mwSize buflen = mxGetN(cellElement2)*sizeof(mxChar)+1;
if (buflen <= 1)
{
connections[cellItem-2][0] = '\0';
}
else
{
mxGetString(cellElement2, strIn, buflen);
memcpy(&connections[cellItem-2][0], strIn, buflen);
}
}
if (cellItem == 10)
{
posXIn = mxGetPr(cellElement2);
memcpy(&posX,posXIn,sizeof(double));
}
if (cellItem == 11)
{
posYIn = mxGetPr(cellElement2);
memcpy(&posY,posYIn,sizeof(double));
}
if (cellItem == 12)
{
posXIn = mxGetPr(cellElement2);
memcpy(&wpX,posXIn,sizeof(double));
}
if (cellItem == 13)
{
posYIn = mxGetPr(cellElement2);
memcpy(&wpY,posYIn,sizeof(double));
}
if (cellItem == 14)
{
weightIn = mxGetPr(cellElement2);
memcpy(&weight,weightIn,sizeof(double));
}
}
sprintf(xStr,"%i",gridX);
sprintf(yStr,"%i",gridY);
sprintf(key,"%ix%i",gridX,gridY);
}
这就是我在 AStar.cpp 中初始化节点列表的方式
// Create nodes in the nodes hash
mxArray *cellElement;
std::map<std::string, Node> nodesHash;
for (int nodeID=0; nodeID < numberOfNodes; nodeID++)
{
cellElement = mxGetCell(prhs[0], nodeID);
Node n(cellElement);
nodesHash[n.key] = n;
}
Form now on nothing 不再使用 prhs[0],因为没有改变 prhs[0](包含指向 Matlab 变量 cellNodes 的指针的变量)的内容,这个变量在离开 MEX 后应该完全相同。
从这里我有两个问题:
- 如果没有改变第一个参数的内容,为什么从 getWP 返回时会崩溃?
- 是否有更优雅的方式强制 Matlab 创建变量的真实副本?
谢谢!
*Edit1:在 Windows10 64 位中使用 Matlab 2015b。
【问题讨论】:
-
您不需要创建变量的副本。您的 MEX 文件不应该直接写入其输入。 API 明确禁止这样做。只是不要这样做。在 MEX 函数中使用
mxDuplicateArray将输入复制到输出,然后修改输出。或者干脆创建一个新的输出数组。让您的 MEX 文件使用简单的数据,因此复制成本更低。 -
等等,放弃那个。看起来您的 C++ 代码正在 memcpy-ing 到未分配的内存中?
-
写入 prhs[ ] 变量不会使 MATLAB 崩溃。它将潜在地做的是产生改变多个变量的不良副作用,甚至可能是 MATLAB 保留在一边的文字变量,例如 1:3。我同意 Chris 的观点,即 tempVal 是一个未初始化的指针,用于 memcpy() 和取消引用。由于这显然是用于双精度标量,因此一个简单的解决方法是将 tempVar 定义为大小为 1 的数组。即 double tempVar[1];您应该检查例程中使用的所有指针,以确保它们已分配或分配。
-
MATLAB 没有公布他们的变量共享规则,所以没有官方方法来取消共享它们。即使在 mex 例程中侵入 mxArray 也无法检测到这一点。你能做的最好的就是尝试你的分配技巧。例如,A(1) = A(1)。在 mex 例程中就地修改 prhs[ ] 变量当然是不鼓励并且违反规则的,但是当使用非常大的变量时,有时由于资源限制可能是必要的。
-
如果您只是从 mxArray 变量中复制标量双精度,也许可以考虑使用 mxGetScalar( ) 而不是做所有指针和 memcpy 的事情。
标签: c++ matlab memory-management crash mex