简体   繁体   English

C++ 打印奇怪的行为(潜在的内存泄漏?)

[英]C++ Print Weird Behavior (Potential Memory Leak?)

I'm running some C++ code and have been noticing some weird behavior.我正在运行一些 C++ 代码并注意到一些奇怪的行为。 For instance, my std::cout prints are only printing out a part of the string that I tell it to print.例如,我的 std::cout 打印仅打印出我告诉它打印的字符串的一部分。

Here is a small section of my code (this code gets called repeatedly):这是我的代码的一小部分(此代码被重复调用):

std::ofstream file;
file.open("cout_img.txt", std::ofstream::out | std::ofstream::app);
std::streambuf* sbuf = std::cout.rdbuf();
std::cout.rdbuf(file.rdbuf());
std::cout << "Reached Display Function NOW";
std::string frame_file_name = std::string("demo") + std::to_string(saveImgNum) + std::string(".bmp");
std::cout << frame_file_name + '\n';

For instance, in this section I'm only printing out "splay Function NOW" each time instead of the full string "Reached Display Function NOW".例如,在本节中,我每次只打印“splay Function NOW”,而不是完整的字符串“Reached Display Function NOW”。 I'm also not even printing out the frame_file_name variable.我什至没有打印出 frame_file_name 变量。

Could this mean I'm experiencing a memory leak somewhere?这是否意味着我在某处遇到了内存泄漏? If so, does the section of code I posted look suspicious at all?如果是这样,我发布的代码部分是否看起来很可疑? Is it because I have to deallocate variables such as the std::string variable?是因为我必须释放诸如 std::string 变量之类的变量吗?

What else can I look for?我还能寻找什么? I'm using CPython API (Python embedded in C++) if that makes a difference.如果这有所不同,我正在使用 CPython API(嵌入在 C++ 中的 Python)。

Thanks so much!非常感谢!

If you just want to write to a file (as a console replacement / log), then there is no reason to redirect your output through std::cout .如果您只想写入文件(作为控制台替换/日志),则没有理由通过std::cout重定向输出。

I took your snippet and cut it down.我把你的片段剪下来了。 This:这个:

int main()
{
    std::ofstream file;
    file.open("cout_img.txt", std::ofstream::out | std::ofstream::app);

    file << "Reached Display Function NOW\n";
    std::string frame_file_name = std::string("demo") + std::to_string(17 /*saveImgNum*/) + std::string(".bmp");
    file << frame_file_name + '\n';

    return 0;
}

Produces a file cout_img.txt containing:生成一个文件cout_img.txt其中包含:

Reached Display Function NOW
demo17.bmp

EDIT: Additionally, you can also enable the console in your windows app.编辑:此外,您还可以在 Windows 应用程序中启用控制台。 A quick search lead to this answer .快速搜索导致此答案 Add the following code to your WinMain , and the console will work:将以下代码添加到您的WinMain ,控制台将工作:

    AllocConsole();
    #pragma warning( disable : 4996 )
    freopen("conin$", "r", stdin);
    freopen("conout$", "w", stdout);
    freopen("conout$", "w", stderr);
    printf("Debugging Window:\n");
    std::cout << "Testing the console 1 2 3" << std::endl;

If you do a more thorough search, you can probably even find a method that doesn't use a deprecated method ( freopen ).如果您进行更彻底的搜索,您甚至可能会找到一种不使用已弃用方法 ( freopen ) 的方法。 But as shown there it did add a functioning console to the windows app that I just created to test with.但如图所示,它确实向我刚刚创建的用于测试的 Windows 应用程序添加了一个正常运行的控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM