什么可能导致返回函数崩溃? C++

发布于 2024-10-12 07:13:37 字数 285 浏览 0 评论 0原文

所以我已经调试这个错误几个小时了。我使用 Ogre3d 编写一个程序只是因为它不加载符号,所以它不允许我进行堆栈跟踪,这使得查找崩溃的位置变得更加困难。因此,在调用特定函数之前编写,我打印出“开始”,然后调用该函数,并在打印“停止”后立即写入。在整个函数中,我打印出字母 AF,其中 F 在函数返回之前打印(最后一个“}”上方一行)奇怪的是,当崩溃发生时,它是在打印“F”之后,但没有“停止” '。这是否意味着崩溃发生在某处?我唯一能想到的是在函数期间分配的一些内存的释放过程中出了问题。我从来没有发生过这样的事情,我会继续检查以确保我认为的地方出了问题。

So I have been debugging this error for hours now. I writing a program using Ogre3d relevant only because it doesn't load symbols so it doesn't let me stack trace which made finding the location of the crash even harder. So, write before I call a specific function I print out "Starting" then I call the function and immediately after I print "Stopping". Throughout the function I print out letters A-F where F is printed right before the function returns (one line above the last '}') The weird thing is when the crash occurs it is after the 'F' is printed but there is no 'Stopping'. Does this mean that the crash is happening in between somewhere? The only thing I can think of is something going wrong during the deallocation of some of the memory allocated during the function. I've never had anything happen like this, I will keep checking to make sure it's going wrong where I think it is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你是我的挚爱i 2024-10-19 07:13:37

大多数时候,当一些奇怪和难以理解的事情发生时,都是因为其他原因。

您的代码中可能有一些悬空指针(即使在远离该函数的地方)指向一些随机存储单元。

您可能使用过这种悬空指针,并且它可能会导致覆盖您需要的一些内存单元。这样做的结果是,您通过更改其他地方定义的某些变量、某些常量甚至某些代码来更改程序的行为!

我建议您使用一些能够检查和报告错误内存访问的工具来调试应用程序,例如 Valgrind


无论如何,如果您能够定位崩溃的根源并编写一小段代码,将崩溃的代码发布在此处 - 这可能只是您的函数中的一个简单错误,尽管从您的描述来看这听起来不太可能。

Most of the times when something weird and un-understandable happens, it's because of something else.

You could have some dangling pointers in your code (even in a place far away from that function) pointing to some random memory cells.

You might have used such dangling pointer, and it might have resulted in overwriting some memory cells you need. The result of this is that you changed the behavior of your program by changing some variable defined elsewhere, some constants, or even some code!

I'd suggest you to debug your application using some tool able to check and report erroneous memory accesses, like Valgrind.


Anyway if you are able to localize the source of your crash and to write a really small piece of code that will crash post it here -- it could be just a simple error in your function, although it sounds unlikely, from your description.

你的往事 2024-10-19 07:13:37

可能意味着当函数返回并且某些析构函数被触发时发生错误。很可能您有一些析构函数试图释放它不拥有的内存,或者注销日志中某些缓冲区的末尾等。

如果您不刷新输出流,可能会出现另一种需要注意的可能性。可能会打印“Stopping”,但在命中 stdout 之前正在缓冲。一定要检查一下这一点,因为如果发生了这种情况,你就找错了对象。

This probably means that the error is happening when the function returns and some destructor is firing. Chances are that you have some destructor trying to free memory it doesn't own, or writing off the end of some buffer in a log, etc.

Another possibility to be aware of might come up if you aren't flushing the output stream. It's possible that "Stopping" is getting printed, but is being buffered before hitting stdout. Make sure to check for this, since if that's what's going on you'll be barking up the wrong tree.

葬シ愛 2024-10-19 07:13:37

我遇到了类似的问题,事实证明,当签名期望返回类型为 std::shared_ptr 时,我的函数没有返回任何内容,即使我没有在任何地方使用返回值。

该函数具有以下签名:

std::shared_ptr<blDataNode> blConditionBasedDataSelectionUI::selectData(std::shared_ptr<blDataNode> inputData)
{
    // My error was due to the function
    // not returning anything
}

I had a similar problem, and it turned out that my function was not returning anything when the signature expected a return type of std::shared_ptr, even though I was not using the return anywhere.

The function had the following signature:

std::shared_ptr<blDataNode> blConditionBasedDataSelectionUI::selectData(std::shared_ptr<blDataNode> inputData)
{
    // My error was due to the function
    // not returning anything
}
誰ツ都不明白 2024-10-19 07:13:37

我遇到了同样的问题,结果发现我忘记在附加新项目之前初始化我的向量,这在我的函数将向量与其他列表进行比较时导致错误。

std::vector<cv::Point> lefteyeCV;
void Init() {
    // I need to add "lefteyeCV.clear();" here!
    for (int i = 0; i < 8; i++) {
        lefteyeCV.push_back(cv::Point(0, 0));
    }
}

// the following comparison will crash after "return 0" 
// because cl_ is of size 8, but if I run "Init()" twice, lefteyeCV.size() = 16
// then the comparison is out of range.
int irisTrack(){
    for (int i = 0; i < lefteyeCV.size(); i++) {
        cl_[i] = cv::Point(lefteyeCV[order[i]].x - leftRect.x, lefteyeCV[order[i]].y - leftRect.y);
    }
    return 0;
}

令人困惑的是,我正在使用 Xcode,并且应用程序在“return 0”之后立即崩溃,并显示难以辨认的消息“thread 13:signal SIGABRT”。但是,使用 Visual Studio 向我显示了索引超出范围的行。

I encountered the same problem and it turned out I forgot to init my vector before appending new items, which cause error when my function was comparing the vector with other list.

std::vector<cv::Point> lefteyeCV;
void Init() {
    // I need to add "lefteyeCV.clear();" here!
    for (int i = 0; i < 8; i++) {
        lefteyeCV.push_back(cv::Point(0, 0));
    }
}

// the following comparison will crash after "return 0" 
// because cl_ is of size 8, but if I run "Init()" twice, lefteyeCV.size() = 16
// then the comparison is out of range.
int irisTrack(){
    for (int i = 0; i < lefteyeCV.size(); i++) {
        cl_[i] = cv::Point(lefteyeCV[order[i]].x - leftRect.x, lefteyeCV[order[i]].y - leftRect.y);
    }
    return 0;
}

What's confusing is that, I'm using Xcode and the app crash right after "return 0" with the indecipherable message "thread 13: signal SIGABRT". However, using Visual Studio instead showed me the line where index is out of range.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文