Raspberry Pi 1 B 上的 c++ bass 和 rapidXml
c++ bass and rapidXml on Raspberry Pi 1 B
我在为我的孩子做的一个项目中有一个奇怪的行为。我想用 rapidXml 解析一个 xml 并在我的 RasPi1B 上播放带低音的 mp3。如果我按一次 main-function,一切正常。但是一旦我将代码放入另一个 class,我的 xml-data 就被破坏了。
我只在私人时间使用 C++ 并使用托管语言,所以我不喜欢 C++ 中的 memory-management。所以我希望你们能帮助我(和我的孩子)解决这个问题。当然,我可以在一个 class 中完成所有事情,但大脑会试图让我窒息而死 ;-)
首先在一个主函数中:
// 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;
}
然后(几乎)与主函数和 class/header 相同:
//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>
抱歉这么久post!我希望有一个人可以帮助我。我真的觉得很失落...
提前致谢!
xmlFile
在 Player::loadXml()
超出范围时被销毁。这允许将被 xmlFile
的数据占用的内存分配给其他东西(在这种情况下 BASE_Init
似乎正在向它写入数据)。
doc
仍在使用 xmlFile
以前的数据。这会导致未定义的行为。
请将 xmlFile
存储在 Player
class 中,就像您对 doc
所做的那样。
也不要忘记 delete
你 new
的一切,比如 _pPlayer ;-)
我在为我的孩子做的一个项目中有一个奇怪的行为。我想用 rapidXml 解析一个 xml 并在我的 RasPi1B 上播放带低音的 mp3。如果我按一次 main-function,一切正常。但是一旦我将代码放入另一个 class,我的 xml-data 就被破坏了。 我只在私人时间使用 C++ 并使用托管语言,所以我不喜欢 C++ 中的 memory-management。所以我希望你们能帮助我(和我的孩子)解决这个问题。当然,我可以在一个 class 中完成所有事情,但大脑会试图让我窒息而死 ;-)
首先在一个主函数中:
// 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;
}
然后(几乎)与主函数和 class/header 相同:
//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>
抱歉这么久post!我希望有一个人可以帮助我。我真的觉得很失落... 提前致谢!
xmlFile
在 Player::loadXml()
超出范围时被销毁。这允许将被 xmlFile
的数据占用的内存分配给其他东西(在这种情况下 BASE_Init
似乎正在向它写入数据)。
doc
仍在使用 xmlFile
以前的数据。这会导致未定义的行为。
请将 xmlFile
存储在 Player
class 中,就像您对 doc
所做的那样。
也不要忘记 delete
你 new
的一切,比如 _pPlayer ;-)