我在Unity论坛上交叉发布了这个内容;如果这违反了行为,我会道歉。
嗨伙计
当我尝试通过插件在Unity中使用C ++中的std :: map时,我遇到了崩溃。 我没有从插件中将地图内容传递给Unity;我只是调用成员函数,例如find()函数(我的应用程序崩溃的地方)
当我将插件加载到C ++控制台应用程序中但代码在Unity中崩溃时,代码执行正常。 我怀疑有一个编组/内存分配问题,Unity不允许我分配某种类型的内存
附件是Player.log输出和调用Unity脚本以及被调用者C ++代码。 任何帮助将不胜感激。
此致
Lancophone
调用C#代码:
public string filename; // The name of the audio file this behaviour corresponds to
[DllImport ("Unity-Audio-Plugin")]
private static extern void addSoundToRendererBank(string filename);
[DllImport ("Unity-Audio-Plugin")]
private static extern void addSoundToRendererBankAtPosition(string filename, float xPos, float zPos);
[DllImport ("Unity-Audio-Plugin")]
private static extern bool setSoundWithNameToPosition (string filename, float xPos, float zPos);
[DllImport ("Unity-Audio-Plugin")]
private static extern bool deactivateSoundInRendererBank (string filename);
// Use this for initialization
public int incr;
void Start () {
incr = 1;
addSoundToRendererBank (filename);
InvokeRepeating ("moveMe", 0, 0.3F);
}
.....
Callee C ++代码:
void addSoundToRendererBank(char *filename)
{
printf("Calling the addSoundToRendererBank function with %s as a filename\n", filename);
copyStringFromManagedCaller(filename);
std::string fileNameStr(&buffer[0]);
int soundLoc = soundBank->getIDForName(fileNameStr);
if (soundLoc > 0) { // Sound is already in the bank
// If its just been deactivated, reactivate it
std::string key(&buffer[0]);
soundBank->activateSoundSource(key);
}
...
...
int SoundBank::getIDForName(std::string &filePath)
{
// Filepath -> sourceIDMap -> source -> ID
std::string key(filePath);
int location;
std::cerr << "About to enter the try-catch block in the SoundBank::getIDForName() function using " << key << std::endl;
// Print the current elements inside the map
std::cerr << "The contents of the sourceIDMap:" << std::endl;
SoundSourceMapIterator map_it;
// for (map_it = sourceIDMap.begin(); map_it != sourceIDMap.end(); ++map_it) {
// std::cerr << "Key: " << map_it->first << "\tValue: " << map_it->second << std::endl;
// }
std::cerr << "I'm about to die.... :-(" << std::endl;
// Throws randomers; using find instead
// try
// {
// location = sourceIDMap.at(key);
// }
// catch (const std::out_of_range& oorEx)
// {
// location = -1;
// }
SoundSourceMapIterator it = sourceIDMap.find(key);
if (it == sourceIDMap.end())
location = -1;
else
location = it->second;
.......
控制台输出:
.....
sourceIDMap的内容:
我快死了...... :-(
接收未处理的NULL异常
获得27个堆叠帧 #0 0x00000111a00b91在std :: __ 1 :: __ tree_iterator,std :: __ 1 :: allocator&gt;,int&gt;,std :: __ 1 :: __ tree_node,std :: __ 1 :: allocator&gt;,int&gt;,void *&gt; *,长&gt; std :: __ 1 :: __ tree,std :: __ 1 :: allocator&gt;,int&gt;,std :: __ 1 :: __ map_value_compare,std :: __ 1 :: allocator&gt;,std :: __ 1 :: __ value_type,std :: __1 :: allocator&gt;,int&gt ;,std :: __ 1 :: less,std :: __ 1 :: allocator&gt; &gt;,true&gt;,std :: __ 1 :: allocator,std :: __ 1 :: allocator&gt;,int&gt; &GT; &gt; :: find,std :: __ 1 :: allocator&gt; &gt;(std :: __ 1 :: basic_string,std :: __ 1 :: allocator&gt; const&amp;)
#1 0x000001119fdb21 in uap :: SoundBank :: getIDForName(std :: __ 1 :: basic_string,std :: __ 1 :: allocator&gt;&amp;)
#2 0x00000111a06891在addSoundToRendererBank中 ......
答案 0 :(得分:0)