一个从来没有执行过的代码中的微小变化如何能产生影响呢?

时间:2021-01-07 04:50:42

I have a very strange problem and since it is not possible for me to send code I will try to explain.

我有一个很奇怪的问题,因为我不可能发送代码,所以我会试着解释。

This is more like philosophical problem - I hope someone has time/knowledge to think about this.

这更像是哲学问题——我希望有人有时间/知识去思考这个问题。

1) I have project.cpp looking exactly like this:

1)项目。cpp是这样的:

#include <pthread.h>
#include <unistd.h>
pthread_t pplayer_thread;
void *play(void*);

int main(int argc, char **argv) {
    pthread_create(&pplayer_thread, NULL, play_cb, NULL);
    usleep(5000000);
    return 0;
}

2) pplayer.cpp looking something like this:

2)pplayer。cpp看起来是这样的:

...
void *play_cb(void *arg) {
    // this starts movie using gstreamer and exits thread
}   
...

3) not_executed_from_main.cpp looking something like this:

3)not_executed_from_main。cpp看起来是这样的:

...
extern MyClass *myObj; // this is included from .h file
...
MyClass *myObj = NULL;
...
some_function() {
    ...
    myObj = MyClass::createNew(args);
    ...
}
...

This is all linked together with various other libraries and ton of garbage, but this is basically what is important.

这些都与其他的库和大量的垃圾链接在一起,但这基本上是重要的。

--> Problem:

- - >问题:

When I run this, I should see window playing movie clip using gstreamer for 5 seconds -> BUT I only hear sound!

当我运行这个,我应该看到窗口播放电影剪辑使用gstreamer 5秒->但我只听到声音!

--> Strange thing:

- - >奇怪:

When I comment the line:

当我评论这句话时:

myObj = MyClass::createNew(args);

and run again --> I see gstreamer window also (everything is fine)

再次运行——>我也看到了gstreamer窗口(一切正常)

--> Notes:

- - >指出:

this may have something to do with:

这可能与以下因素有关:

  • linking process and nature of MyClass and it's parent class (my best guess)
  • 链接MyClass的进程和性质,以及它的父类(我最好的猜测)
  • "static" keyword
  • “静态”关键字
  • "external" keyword
  • “外部”关键字
  • C and C++ mixing
  • C和c++混合

--> I ask once more:

——>我再问一次:

How can a small change in a code which is never executed make a difference?

一个从来没有执行过的代码中的微小变化如何能产生影响呢?

(please help)

(请帮助)

4 个解决方案

#1


7  

Seems like you need to get familiar with chaos theory. In a sufficiently complex system, the slightest change can propagate through any inherent instability to the point of causing a massive difference.

似乎你需要熟悉混沌理论。在一个足够复杂的系统中,最微小的变化可以通过任何固有的不稳定性传播到导致巨大差异的程度。

In your case, it can be anything from implicit side-effects of that method, to a memory-related error becoming visible when the layout of the executable code changes.

在您的例子中,它可以是任何东西,从该方法的隐式副作用,到当可执行代码的布局改变时,与内存相关的错误变得可见。

You should use a debugger to trace your code. Make sure nothing from the supposedly not-executed code is actually executed. Your code may be entering code paths that you mistakenly think are inaccessible, or some other part of your program (e.g. a static initilizer) may be acting up.

您应该使用调试器来跟踪您的代码。确保从假定未执行的代码中没有执行任何内容。您的代码可能正在输入您错误地认为不可访问的代码路径,或者程序的其他部分(例如,静态初始化程序)可能出现故障。

Valgrind can also be useful if it is available for your platform - it will detect a multitude of memory-related errors, like the one I suspect you have at your hands. Unfortunately it is not very good at detecting errors in the stack - your compiler may be able to help there, though.

如果Valgrind适用于您的平台,那么它也很有用——它将检测到大量与内存相关的错误,就像我怀疑您正在处理的错误。不幸的是,它并不能很好地检测堆栈中的错误——尽管如此,您的编译器可能可以在那里提供帮助。

#2


3  

Most probably it has to do with *. You have something that does some bad thing, addressing things out of bounds or some other undefined behavior and this only triggers (or doesn't) in just some specific configuration. Adding or deleting a variable declaration can be such a thing.

很可能与*有关。你有一些东西做一些坏事,处理一些超出界限或其他未定义的行为,这只会在某些特定的配置中触发(或不触发)。添加或删除变量声明可以是这样的事情。

#3


2  

give your example code is not the real code with the problem....

给你的示例代码不是真正的代码与问题....

main is not the only entry point where code can start executing, any global objects will execute their constructors which can trigger off all kinds of code. So perhaps thats some how biting you.

main不是代码开始执行的唯一入口点,任何全局对象都将执行它们的构造函数,构造函数可以触发所有类型的代码。所以,也许这就是咬你的原因。

Either debug it, or, perhaps, put messages out to the console to see what paths are executing.

要么调试它,要么把消息发送到控制台以查看正在执行的路径。

#4


0  

IS your thread using myObj ? If so there could be a race condition between setting myObj to NULL and allocating it again.

你的线程使用myObj吗?如果是这样,在将myObj设置为NULL和再次分配它之间可能存在竞争条件。

#1


7  

Seems like you need to get familiar with chaos theory. In a sufficiently complex system, the slightest change can propagate through any inherent instability to the point of causing a massive difference.

似乎你需要熟悉混沌理论。在一个足够复杂的系统中,最微小的变化可以通过任何固有的不稳定性传播到导致巨大差异的程度。

In your case, it can be anything from implicit side-effects of that method, to a memory-related error becoming visible when the layout of the executable code changes.

在您的例子中,它可以是任何东西,从该方法的隐式副作用,到当可执行代码的布局改变时,与内存相关的错误变得可见。

You should use a debugger to trace your code. Make sure nothing from the supposedly not-executed code is actually executed. Your code may be entering code paths that you mistakenly think are inaccessible, or some other part of your program (e.g. a static initilizer) may be acting up.

您应该使用调试器来跟踪您的代码。确保从假定未执行的代码中没有执行任何内容。您的代码可能正在输入您错误地认为不可访问的代码路径,或者程序的其他部分(例如,静态初始化程序)可能出现故障。

Valgrind can also be useful if it is available for your platform - it will detect a multitude of memory-related errors, like the one I suspect you have at your hands. Unfortunately it is not very good at detecting errors in the stack - your compiler may be able to help there, though.

如果Valgrind适用于您的平台,那么它也很有用——它将检测到大量与内存相关的错误,就像我怀疑您正在处理的错误。不幸的是,它并不能很好地检测堆栈中的错误——尽管如此,您的编译器可能可以在那里提供帮助。

#2


3  

Most probably it has to do with *. You have something that does some bad thing, addressing things out of bounds or some other undefined behavior and this only triggers (or doesn't) in just some specific configuration. Adding or deleting a variable declaration can be such a thing.

很可能与*有关。你有一些东西做一些坏事,处理一些超出界限或其他未定义的行为,这只会在某些特定的配置中触发(或不触发)。添加或删除变量声明可以是这样的事情。

#3


2  

give your example code is not the real code with the problem....

给你的示例代码不是真正的代码与问题....

main is not the only entry point where code can start executing, any global objects will execute their constructors which can trigger off all kinds of code. So perhaps thats some how biting you.

main不是代码开始执行的唯一入口点,任何全局对象都将执行它们的构造函数,构造函数可以触发所有类型的代码。所以,也许这就是咬你的原因。

Either debug it, or, perhaps, put messages out to the console to see what paths are executing.

要么调试它,要么把消息发送到控制台以查看正在执行的路径。

#4


0  

IS your thread using myObj ? If so there could be a race condition between setting myObj to NULL and allocating it again.

你的线程使用myObj吗?如果是这样,在将myObj设置为NULL和再次分配它之间可能存在竞争条件。