gpt4 book ai didi

c++ - VST 主机 - 泄漏的对象 - Juce/C++

转载 作者:搜寻专家 更新时间:2023-10-31 01:18:13 25 4
gpt4 key购买 nike

我是一名 PHP 程序员,在构建 VST 主机时学习 C++。我可能已经咬得太多了,但我正在取得一些进步(我认为)!

我在 Visual Studio 2010 中使用 Steinberg VST SDK 和 JUCE 库。我遇到了对象泄漏错误,我不太了解在搜索错误时找到的解决方案我已收到。

这是“输出”选项卡中的错误。我的程序吐出 JUCE Assets 错误:

*** Leaked objects detected: 44 instance(s) of class MidiEventHolder
score.exe has triggered a breakpoint

我在 juce_amalgamated.h 文件中看到这条消息:

~LeakCounter()
{
if (numObjects.value > 0)
{
DBG ("*** Leaked objects detected: " << numObjects.value << " instance(s) of class " << getLeakedObjectClassName());

/** If you hit this, then you've leaked one or more objects of the type specified by
the 'OwnerClass' template parameter - the name should have been printed by the line above.

If you're leaking, it's probably because you're using old-fashioned, non-RAII techniques for
your object management. Tut, tut. Always, always use ScopedPointers, OwnedArrays,
ReferenceCountedObjects, etc, and avoid the 'delete' operator at all costs!
*/
jassertfalse;
}
}

这是我认为错误指的是我编写的代码:

const wchar_t* midiPath = L"C:\\creative\\midi\\m1.mid";

File* fileHard;
FileInputStream* fileInputStream;

fileHard = new File (T("C:\\creative\\midi\\m1.mid"));
fileInputStream = fileHard->createInputStream();

MidiFile * midFile;
midFile = new MidiFile();
midFile->readFrom(*fileInputStream);
midFile->getNumTracks();
midFile->getTrack(0);

也许我更像是在接近 PHP 的语法?我不太明白什么是 RAII 技术。

感谢任何让我朝着正确方向前进的提示。

最佳答案

几件事:

  1. 您正在混合使用宽字符串和 Microsoft ("T") 字符串。选择一个(适合您的 API 的那个)。

  2. 不要在 C++ 中说 new。它几乎总是不是你需要的。相反,只需使用自动对象:

File fileHard("C:\\creative\\midi\\m1.mid");
FileInputStream * fileInputStream = fileHard.createInputStream();

MidiFile midFile;
midFile.readFrom(*fileInputStream);
midFile.getNumTracks();
midFile.getTrack(0);

这应该可以消除大部分内存泄漏。您仍然需要按照文档中描述的方式释放 fileInputStream

关于c++ - VST 主机 - 泄漏的对象 - Juce/C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7395479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com