我有一个让我完全目瞪口呆的问题,我希望有人能指出我正确的方向。
我有一个 DLL,其中链接了一个静态库。在 DLL 中,我有一个具有以下签名和开头的函数:
CellMatrix BasisSwapFlows(double spread, const std::string & convention, int startDate, int endDate, bool forceEOM, const CellMatrix & returnSide, bool explode)
CashFlow::Vector flows(basisSwapFlows(spread, getBasisSwapConvention(convention), XLDate(startDate), XLDate(endDate), forceEOM));
....
在那里,我从带有签名的静态库中调用一个函数:
CashFlow::Vector basisSwapFlows(double spread, const BasisSwapConvention & convention, const XLDate & startDate, const XLDate & endDate, bool forceEOM)
当我在发布模式下编译并运行它时,然后在调用静态库期间,第一个参数(spread)似乎未初始化。但是,在调用站点(在 DLL 中)显然是这样。这在调试模式下不会发生。此外,如果在调用静态库之前我制作了参数的副本,即:
double spread_loc(spread);
CashFlow::Vector flows(basisSwapFlows(spread_loc, getBasisSwapConvention(convention), XLDate(startDate), XLDate(endDate), forceEOM));
....
并通过,问题不会发生。最后,如果我将静态库函数的名称修改为 basicSwapFlows_v2,问题再次消失。但是,对参数重新排序没有任何作用。
我正在使用 VS2010 C++ 编译器。如果我可以提供任何其他信息,请告诉我。
编辑:我还发现,当我在两个库的发布版本中关闭优化时,这个问题就消失了。事实上,只要禁用 DLL 中的优化,问题就会消失。
编辑2:还发现只是关闭整个程序优化但离开每个项目的完全优化可以解决问题。
编辑 3:保留所有优化但通过 const ref 获取参数也可以解决问题。