C++断言错误,求高手~~~

mhsjfly 2012-03-08 10:50:58
我在写一个max的插件小程序来导出场景中的信息,由于要将场景信息导出到一个txt文件。所以用了BeginWriting()、Write()和EndWriting()函数,编译通过,但是在启动max之后导出信息时出现断言错误。给出的错误信息显示在BeginWriting()函数里的断言错误。下边是部分代码:求高手帮助!!!!
int maxProject5::DoExport(const TCHAR *name,ExpInterface *ei,Interface *i, BOOL suppressPrompts, DWORD options)
{

if (BeginWriting(name))
{

INode* pRootNode = i->GetRootNode();

//Export tree.
Export(pRootNode,0);

EndWriting();

return TRUE; // We have successfully exported to our file!
}

// If we are here, something in the export failed.
return FALSE;
}
BOOL maxProject5::BeginWriting(const char *pPath)
{
// Check we are not currently writing
DbgAssert(m_pFile == NULL);//断言

errno_t lErr = fopen_s(&m_pFile, pPath, "w");
return lErr == 0;
}

void maxProject5::EndWriting()
{
DbgAssert(m_pFile != NULL);

fclose(m_pFile);
m_pFile = NULL;
}

void maxProject5::Write(int indent, const char* pMsg, ...)
{
DbgAssert(m_pFile != NULL);

// first, write in the indent
for (int i = 0; i < indent; i++)
fprintf_s(m_pFile, "\t");//C++库函数

// Write the message, passing in our variable
// parameters to a function that wraps printf
va_list vargs;
va_start(vargs, pMsg);//fprintf()的封装
vfprintf_s(m_pFile, pMsg, vargs);
va_end(vargs);//http://www.cnblogs.com/leaven/archive/2010/06/29/1767374.html

// Finish the line
fprintf_s(m_pFile, "\n");
}
m_pFile是在类中定义的私有变量。其他代码没有涉及,只有贴出部分用到,各位多帮忙啊~
...全文
1120 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
lagaisy 2014-05-05
  • 打赏
  • 举报
回复
谢谢,受教了
千劫炼 2012-08-28
  • 打赏
  • 举报
回复
受教了,值得借鉴。
viktor 2012-03-08
  • 打赏
  • 举报
回复
用_T("w")试试
Aist-memory 2012-03-08
  • 打赏
  • 举报
回复
我看把这个断言注销也不影响你代码
mhsjfly 2012-03-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 luciferisnotsatan 的回复:]
如果m_pFile是你自己定义的变量,那么构造函数中有没有初始化为NULL?
[/Quote]
在相关文件中的代码:
#else /* POSITIONAL_PARAMETERS */
if (state == ST_INVALID)
_VALIDATE_RETURN(("Incorrect format specifier", 0), EINVAL, -1);
#endif /* POSITIONAL_PARAMETERS */
mhsjfly 2012-03-08
  • 打赏
  • 举报
回复
我在构造函数中将其初始化之后在运行时,还是出现了一个错误:
Debug Assertion Failed!
……
File:f:\dd\vctools\crt_bld\self_x86\crt\src\output.c
Line:1120

Expression:("Incorrect format specifier",0)
……
mhsjfly 2012-03-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 luciferisnotsatan 的回复:]
如果m_pFile是你自己定义的变量,那么构造函数中有没有初始化为NULL?
[/Quote]
不好意思,我刚开始没有初始化,初始化之后运行又出现了这个错误

Debug Assertion Failed!
Program:E:\learning\3dsMax2010\install\3dsmax.exe
File:f:\dd\vctools\crt_bld\self_x86\crt\src\output.c
Line:1120

Expression:("Incorrect format specifier",0)
luciferisnotsatan 2012-03-08
  • 打赏
  • 举报
回复
m_pFile此时不是NULL,至于为啥不是NULL,单步调试。从对象创建开始,跟到BeginWriting被调用,看看是什么导致m_pFile不是NULL
mhsjfly 2012-03-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 luciferisnotsatan 的回复:]

DbgAssert(m_pFile == NULL);//断言
看看为啥这个断言会失败。为啥m_pFile == NULL失败了。
[/Quote]
我也不知道为啥失败了……新手哈~见谅,能不能给点建议之类的?
luciferisnotsatan 2012-03-08
  • 打赏
  • 举报
回复
如果m_pFile是你自己定义的变量,那么构造函数中有没有初始化为NULL?
luciferisnotsatan 2012-03-08
  • 打赏
  • 举报
回复
DbgAssert(m_pFile == NULL);//断言
看看为啥这个断言会失败。为啥m_pFile == NULL失败了。
mhsjfly 2012-03-08
  • 打赏
  • 举报
回复
好了,问题已经解决了。谢谢各位!断言失败,是因为在构造函数中没有初始化变量m_pFile。后来出现的错误是格式错误。单步调试之后发现write()函数调用vfprintf_s()函数时出错,变量格式错误,改正之后就可以正常运行了~希望遇到同样问题的童鞋们可以以此解决问题,再次谢谢各位!!
luciferisnotsatan 2012-03-08
  • 打赏
  • 举报
回复
单步调试,断言已经说了
Expression: ("Incorrect format specifier",0)
接下来就看到时你代码里是怎么回事了。
mhsjfly 2012-03-08
  • 打赏
  • 举报
回复
Debug Assertion Failed!
Program:
...rs\n\Desktop\aaa\aaa\Debug\MH.exe
File:f:\dd\vctools\crt_bld\self_x86\crt\src\output.c
Line:1120
Expression: ("Incorrect format specifier",0)
For information on how your program can cause an assertion failure, see the Visual C++ Documentation om asserts.
(Press Retry to debug the application)
经过这种改之后(包括重建项目),启动max之后导出时还是出现这个错误,路过的大神帮帮忙啊~~不胜感激
mhsjfly 2012-03-08
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 xulai1001 的回复:]

用_T("w")试试
[/Quote]
用这个语句来代替断言吗?还是?
mhsjfly 2012-03-08
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 focuslight 的回复:]

我看把这个断言注销也不影响你代码
[/Quote]
我注释过还是不行

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧