我在为 children 做的项目中有一个奇怪的行为。我想用RapidXml解析一个xml,并在我的RasPi1B上用低音播放mp3。如果我仅在一个主要功能中按下它,则一切正常。但是,一旦我将代码放入另一个类中,我的xml数据就会被破坏。
我只在私有(private)时间使用c++并使用托管语言,因此我对c++的内存管理不那么感兴趣。因此,我希望你们能帮助我(和我的 children )解决这个问题。当然,我可以在一个类上做所有的事情,但是大脑会试图把我cho死;-)
首先在一个主要功能中:
// test.cpp
#include <iostream>
#include <rapidxml.hpp>
#include <rapidxml_utils.hpp>
#include <rapidxml_print.hpp>
#include <bass.h>
using namespace std;
using namespace rapidxml;
int main(int argc, char **argv)
{
file<> xmlFile("bin/test.xml");
xml_document<> doc;
doc.parse<0>(xmlFile.data());
print(std::cout, doc, 0); // everything is fine
bool init = BASS_Init(-1, 44100, 0, 0, NULL);
cout << "BASS_Init " << (init ? "true" : "false") << " [error: " << BASS_ErrorGetCode() << "]" << endl;
print(std::cout, doc, 0); // everything is fine
return 0;
}
然后(几乎)使用主函数和类/标题:
//test2.cpp
#include <iostream>
#include "player.hpp"
using namespace std;
using namespace player;
int main(int argc, char **argv)
{
Player* _pPlayer = new Player();
_pPlayer->loadXml();
_pPlayer->printXml();
_pPlayer->initPlayer();
_pPlayer->printXml();
return 0;
}
// player.cpp
#include "player.hpp"
using namespace std;
using namespace rapidxml;
namespace player {
void Player::loadXml(void)
{
file<> xmlFile("bin/test.xml");
doc.parse<0>(xmlFile.data());
}
void Player::printXml(void)
{
print(std::cout, doc, 0);
}
void Player::initPlayer(void)
{
bool init = BASS_Init(-1, 44100, 0, 0, NULL);
cout << "BASS_Init " << (init ? "true" : "false") << " [error: " << BASS_ErrorGetCode() << "]" << endl;
}
}
// player.hpp
#include <iostream>
#include <rapidxml.hpp>
#include <rapidxml_utils.hpp>
#include <rapidxml_print.hpp>
#include <bass.h>
using namespace std;
using namespace rapidxml;
namespace player {
class Player
{
public:
void loadXml(void);
void printXml(void);
void initPlayer(void);
private:
xml_document<> doc;
};
}
编译:
BASS_PATH = ../libraries/bass_arm
BASS_INCLUDE_PATH = -I$(BASS_PATH)
BASS_LIBRARY_PATH = -L$(BASS_PATH) -L$(BASS_PATH)/hardfp
BASS_LIBS = -lbass -lm
BASS_LINKER_FLAGS = -Wl,-rpath,$(BASS_PATH):$(BASS_PATH)/hardfp
RAPIDXML_PATH = ../libraries/rapidxml-1.13
RAPIDXML_INCLUDE_PATH = -I$(RAPIDXML_PATH)
RAPIDXML_LIBRARY_PATH = -L$(RAPIDXML_PATH)
test:
g++ -std=c++11 src_test/test.cpp -o bin/test $(BASS_INCLUDE_PATH) $(RAPIDXML_INCLUDE_PATH) $(BASS_LIBRARY_PATH) $(RAPIDXML_LIBRARY_PATH) $(BASS_LIBS) $(BASS_LINKER_FLAGS)
test2:
g++ -std=c++11 -g -c src_test/test2.cpp -o src_test/test2.o $(BASS_INCLUDE_PATH) $(RAPIDXML_INCLUDE_PATH)
g++ -std=c++11 -g -c src_test/player.cpp -o src_test/player.o $(BASS_INCLUDE_PATH) $(RAPIDXML_INCLUDE_PATH)
g++ -std=c++11 -g src_test/test2.o src_test/player.o -o bin/test2 $(BASS_LIBRARY_PATH) $(RAPIDXML_LIBRARY_PATH) $(BASS_LIBS) $(BASS_LINKER_FLAGS)
我的test.xml看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<library>
<series>
<title>Lorem ipsum A</title>
<episode>
<title>dolor sit amet 1</title>
<cover></cover>
<files rfid="x1" current_file="1" timestamp="0">
<file>consetetur/sadipscing_elitr_11.mp3</file>
<file>consetetur/sadipscing_elitr_12.mp3</file>
<file>consetetur/sadipscing_elitr_13.mp3</file>
<file>consetetur/sadipscing_elitr_14.mp3</file>
</files>
</episode>
<episode>
<title>dolor sit amet 2</title>
<cover></cover>
<files rfid="x2" current_file="1" timestamp="0">
<file>consetetur/sadipscing_elitr_21.mp3</file>
<file>consetetur/sadipscing_elitr_22.mp3</file>
<file>consetetur/sadipscing_elitr_23.mp3</file>
<file>consetetur/sadipscing_elitr_24.mp3</file>
</files>
</episode>
</series>
</library>
这是 sudo bin / test 的输出
<library>
<series>
<title>Lorem ipsum A</title>
<episode>
<title>dolor sit amet 1</title>
<cover/>
<files rfid="x1" current_file="1" timestamp="0">
<file>consetetur/sadipscing_elitr_11.mp3</file>
<file>consetetur/sadipscing_elitr_12.mp3</file>
<file>consetetur/sadipscing_elitr_13.mp3</file>
<file>consetetur/sadipscing_elitr_14.mp3</file>
</files>
</episode>
<episode>
<title>dolor sit amet 2</title>
<cover/>
<files rfid="x2" current_file="1" timestamp="0">
<file>consetetur/sadipscing_elitr_21.mp3</file>
<file>consetetur/sadipscing_elitr_22.mp3</file>
<file>consetetur/sadipscing_elitr_23.mp3</file>
<file>consetetur/sadipscing_elitr_24.mp3</file>
</files>
</episode>
</series>
</library>
BASS_Init true [error: 0]
<library>
<series>
<title>Lorem ipsum A</title>
<episode>
<title>dolor sit amet 1</title>
<cover/>
<files rfid="x1" current_file="1" timestamp="0">
<file>consetetur/sadipscing_elitr_11.mp3</file>
<file>consetetur/sadipscing_elitr_12.mp3</file>
<file>consetetur/sadipscing_elitr_13.mp3</file>
<file>consetetur/sadipscing_elitr_14.mp3</file>
</files>
</episode>
<episode>
<title>dolor sit amet 2</title>
<cover/>
<files rfid="x2" current_file="1" timestamp="0">
<file>consetetur/sadipscing_elitr_21.mp3</file>
<file>consetetur/sadipscing_elitr_22.mp3</file>
<file>consetetur/sadipscing_elitr_23.mp3</file>
<file>consetetur/sadipscing_elitr_24.mp3</file>
</files>
</episode>
</series>
</library>
以及 sudo bin / test2 的输出:
<library>
<series>
<title>Lorem ipsum A</title>
<episode>
<title>dolor sit amet 1</title>
<cover/>
<files rfid="x1" current_file="1" timestamp="0">
<file>consetetur/sadipscing_elitr_11.mp3</file>
<file>consetetur/sadipscing_elitr_12.mp3</file>
<file>consetetur/sadipscing_elitr_13.mp3</file>
<file>consetetur/sadipscing_elitr_14.mp3</file>
</files>
</episode>
<episode>
<title>dolor sit amet 2</title>
<cover/>
<files rfid="x2" current_file="1" timestamp="0">
<file>consetetur/sadipscing_elitr_21.mp3</file>
<file>consetetur/sadipscing_elitr_22.mp3</file>
<file>consetetur/sadipscing_elitr_23.mp3</file>
<file>consetetur/sadipscing_elitr_24.mp3</file>
</files>
</episode>
</series>
</library>
BASS_Init true [error: 0]
<l1▒B>
CJ>
<>▒CJ▒BJ▒BJ</>
<device>
<de>aults.pcm.device</de>
<`CJ/>
<8CJ BJ="" iec958="1" 1▒CJ="">
<>▒DJ▒BJ6Jcardp3</>
<ults>pcm.cardr/1▒CJ▒CJ</ults>
<▒CJ>devicesg_edefault</▒CJ>
<1>DJPDJ▒CJ(DJ▒CJ</1>
</8CJ>
</device>
<EJpE>
<▒>J▒CJ6J</▒>
<cover/>
<cm.ca did="" J▒DJ="" pEJ="J">
<>devicesdipdefaults.pcm.d</>
<>@EJ▒DJEJ▒DJ</>
<rmat>rawur/sadip1▒EJ▒EJ</rmat>
<▒DJ>6Jtrueltstr_fi</▒DJ>
</cm.ca>
</EJpE>
<CJ>
</l1▒B>
抱歉,很长的帖子!我希望有一个人可以帮助我。我真的很迷路...
提前致谢!
最佳答案
当xmlFile
超出范围时,Player::loadXml()
被销毁。这样就可以将xmlFile
数据所占用的内存分配给其他对象(在这种情况下BASE_Init
似乎正在向其中写入数据)。doc
仍在使用xmlFile
过去的数据。这导致未定义的行为。
请像使用xmlFile
一样将Player
存储在doc
类中。
也不要忘记对所有delete
进行new
编码,例如_pPlayer ;-)
关于c++ - Raspberry Pi 1 B上的c++ bass和RapidXml,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34404351/