Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 根据Valgrind,临时std::管柱泄漏导致的共享ptr?_C++_Memory Leaks_Valgrind_Shared Ptr - Fatal编程技术网

C++ 根据Valgrind,临时std::管柱泄漏导致的共享ptr?

C++ 根据Valgrind,临时std::管柱泄漏导致的共享ptr?,c++,memory-leaks,valgrind,shared-ptr,C++,Memory Leaks,Valgrind,Shared Ptr,Valgrind告诉我这条线肯定在漏水 std::shared_ptr<std::string> pName(new string); Edit2(已解决):事实证明,问题出现在我的PriorityQueue.h文件中。Valgrind的错误有些误导。我错误地使用了std::allocator。使用分配器销毁以前分配的数组中的项时,必须调用分配器。销毁(myArray+i)for0您的程序是否正确终止(即没有中止)?如果您提供更多上下文,例如声明在哪里,这会有所帮助?它是如何使用的

Valgrind告诉我这条线肯定在漏水

std::shared_ptr<std::string> pName(new string);

Edit2(已解决):事实证明,问题出现在我的
PriorityQueue.h
文件中。Valgrind的错误有些误导。我错误地使用了
std::allocator
。使用分配器销毁以前分配的数组中的项时,必须调用分配器。销毁(myArray+i)for
0您的程序是否正确终止(即没有中止)?如果您提供更多上下文,例如声明在哪里,这会有所帮助?它是如何使用的?声明它的类是如何使用的(它是在类中声明的)?您最好创建一个有您的问题的,并向我们展示它。@user1520427它确实正确终止。您的
语句是在自找麻烦。我同意Joachim的观点。有时,您需要动态分配,当这种情况发生时,智能指针就是猫的胡子。然而,在大多数情况下,保持它的简单性并充分利用它。C++是复杂的,但它具有java不具备的主要特性;母语的破坏是可预测的。这正是RAII如此强大的原因,我再怎么强调它有多好也不为过。开发利用它对你有利的技能。还在看你的代码。
==14376== 313 (120 direct, 193 indirect) bytes in 5 blocks are definitely lost in loss record 3 of 3
==14376==    at 0x4C2A879: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14376==    by 0x40429D: std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<std::string*>(std::string*) (shared_ptr_base.h:452)
==14376==    by 0x403DB7: std::__shared_ptr<std::string, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<std::string>(std::string*) (shared_ptr_base.h:768)
==14376==    by 0x40371C: std::shared_ptr<std::string>::shared_ptr<std::string>(std::string*) (shared_ptr.h:113)
==14376==    by 0x402145: sportsball::playBall(std::string, unsigned long, unsigned long) (sportsball.cpp:101)
==14376==    by 0x402AA3: main (sportsball.cpp:215)
PriorityQueue<shared_ptr<string> >* playerQueue = 
    new PriorityQueue<shared_ptr<string> >(initialCapacity, stepSize);
string line, priorityString;
int priority = 0;
int lineNumber = 0;
istringstream* lineStream = new istringstream();    

// For each line in the file
while (getline(infile, line)) {
//...

   lineStream->str(line); // replace the current string
   lineStream->clear(); // reset flags

   std::shared_ptr<std::string> pName(new string);

   // We can use getLine to parse up to our delimiter
   getline(*lineStream, *pName, INLINE_DELIMITER);
   // Then extract the rest of the line normally
   getline(*lineStream, priorityString);

   // Attempt to convert `priorityString` to an int
   // (we catch exceptions further down)
   priority = stoi(priorityString);

   if(sportsball::DEBUG) {
      cout << "Inserting " << *pName << "/"
           << priority << "." << endl;
   }

   // Cool. That worked. Now queue the player.
   playerQueue->insert(pName, priority);

   // probably not necessary
   pName.reset();
   priorityString.clear();

   //...
}

// Cleanup
delete lineStream;
delete playerQueue;