我正在尝试使用 C++20 范围和函数式编程加载目录中的所有 true-type 字体。但是,由于字体是一种资源,因此我在范围接口内分配内存。我想这就是 valgrind 认为我有泄漏的原因。我有一些std::views新分配的原始指针,最终会被丢弃 - 然而,这些原始指针被转换并复制到唯一指针的向量中。
有问题的代码:
// free a font resource
struct font_deleter {
void operator()(TTF_Font * font) { TTF_CloseFont(font); }
};
// aliases
using unique_font = std::unique_ptr<TTF_Font, font_deleter>;
using font_table = std::unordered_map<std::string, TTF_Font *>;
template<typename expected_t>
using result = tl::expected<expected_t, std::string>;
// determine if a path is a valid font file
auto _is_font_fxn(std::error_code & ec) {
return [&ec](fs::path const & path) {
return fs::is_regular_file(path, ec) and path.extension() == ".ttf";
};
} …Run Code Online (Sandbox Code Playgroud) 我在 C++ 中遇到内存泄漏,具体取决于字符串大小。在下面的代码中,如果字符串大小小于 15,valgrind 不会显示泄漏,但是如果字符串大小大于 15,则会出现内存泄漏:
#include <string>
#include <memory>
#include <iostream>
class AuthBase {
public:
virtual void foo() = 0;
};
class APIAuthetication : public AuthBase {
public:
APIAuthetication(const std::string api_key, const std::string api_secret) : m_api_key(api_key),
m_api_secret(api_secret) {}
virtual ~APIAuthetication() {}
void foo() {}
private:
const std::string m_api_key;
const std::string m_api_secret;
};
class ClientBase {
public:
virtual void bar() = 0;
};
class Client: public ClientBase {
public:
Client(AuthBase* auth) : m_auth(auth) {}
~Client() {}
void bar() {}
private: …Run Code Online (Sandbox Code Playgroud) 我在使用 valgrind 检查 C 中的内存泄漏时遇到了问题。它告诉我“valgrind:找不到命令。我使用的是 Windows,并且安装了 mingw。如果有人可以帮助我,我将不胜感激。
我试图释放 char** 数组中存在的 char* 指针,但 valgrind 确定此操作无效。
这是我正在做的一个简单示例:
struct building{
int propertyPrice;
int totalArea;
char** floors;
}
int main(){
struct building* B = malloc(sizeof(struct building));
for(size_t i=0;i<10;i++)
B->floors[i] = malloc(20 * sizeof(char*));
}
Run Code Online (Sandbox Code Playgroud)
这就是我感到困惑的地方 - 我可以通过以下循环访问元素:
for(size_t i=0;i<10;i++)
printf("%s", B->floors[i]); //assume that a valid string exists at each floors[i]!
Run Code Online (Sandbox Code Playgroud)
我尝试按如下方式释放分配的内存:
for(size_t i=0;i<10;i++)
free(B->floors[i]);
free(B);
Run Code Online (Sandbox Code Playgroud)
请原谅缺少对 NULL 的 malloc() 检查,为了简洁起见,我将其删除。
Valgrind 确定该操作free(B->floors[i])是Invalid free() / delete / delete[] / realloc()。然而,如果我可以通过 访问各个 char* 元素B-floors[i],我是否应该能够使用相同的语法通过将函数调用从 切换 …
编辑:有人可以解释为什么添加删除操作符不会更改Valgrind输出?请不要让我阅读C++书籍,我浏览了一对夫妇并且没有在那里找到答案.
我是C++的新手,来自java世界,我似乎无法理解如何使用new和delete运算符.
我有一个方法(myMethod())启动一个类,MyClass.在MyClass中,使用new和new []运算符创建了一些数组和很少的其他对象.最初我没有为这个类编写析构函数(我假设当控件从我的方法返回到main时,这些MyClass对象以及与它相关的所有内容都会自动"释放").
当我用Valgrind运行我的程序时,这是我得到的泄漏摘要:
==9446== LEAK SUMMARY:
==9446== definitely lost: 1,957,019 bytes in 38 blocks.
==9446== indirectly lost: 4,171,184 bytes in 3,040 blocks.
==9446== possibly lost: 0 bytes in 0 blocks.
==9446== still reachable: 91,984 bytes in 991 blocks.
==9446== suppressed: 0 bytes in 0 blocks.
==9446== Reachable blocks (those to which a pointer was found) are not shown.
==9446== To see them, rerun with: --show-reachable=yes
Run Code Online (Sandbox Code Playgroud)
所以我添加了一个析构函数,它删除所有数组并将对象设置为null.我没有在MyClass对象上显式调用delete.现在,当我使用Valgrind运行我的程序时,这是我得到的泄漏摘要.
LEAK SUMMARY:
==9223== definitely lost: 1,957,019 bytes in 38 blocks.
==9223== indirectly …Run Code Online (Sandbox Code Playgroud) 我正在为一个真正的应用项目做C++程序.
我用valgrind做内存泄漏检查.
我有 :
10个块中的160个字节肯定在丢失记录1中丢失2 == 14312 ==在0x4A0846F:malloc(vg_replace_malloc.c:236)
但是,我已经调用了free()来释放它.
怎么解决?为什么我有内存泄漏?
任何帮助将不胜感激.!
代码如下:
#include <stdio.h>
#include <stdlib.h>
int * GetSomeIDs() ;
/*-------- GetSomeIDs-----*/
typedef struct {
char* alias; /* '\0'-terminated C string */
int specific_id;
} aliasID;
int GetNumberOfAliases(void)
{
return 10;
}
aliasID *GetNextAlias(void)
{
aliasID *p = (aliasID *) malloc(sizeof(aliasID)) ;
p->alias = "test alias";
p->specific_id = rand();
return p;
}
int *GetSomeIDs2(aliasID ***a, int *aIDsize , int *idSize)
{
int *ids = (int *) malloc(sizeof(int) * 8) ; …Run Code Online (Sandbox Code Playgroud) 我从以下函数中获取内存泄漏:
int ReadWrite(int socket, char *readfile) {
FILE *rf = NULL;
rf = fopen(readfile, "rb");
fseek(rf, 0, SEEK_END);
int len = ftell(rf);
rewind(rf);
char readbuf[len + 1];
int res = fread(readbuf, len, 1, rf);
readbuf[len + 1] = '\0';
fclose(rf);
while (1) {
int wres = write(socket, readbuf, res);
if (wres == 0) {
cerr << "socket closed prematurely" << endl;
close(socket);
return EXIT_FAILURE;
}
if (res == -1) {
if (errno == EINTR)
continue;
cerr << "socket write failure: …Run Code Online (Sandbox Code Playgroud) 如何在嵌入式Linux机器上运行valgrind来查找主软件中的内存泄漏?
在rcS脚本中,我运行如下:
./main_app
Run Code Online (Sandbox Code Playgroud)
如何将./main_app程序与valgrind联系起来?main_app进程永远不会终止.
我想不断将数据记录到文件中.此外,我想访问日志文件而不终止该main_app过程.我可以做telnet并可以访问日志文件.但问题是,除非处理程序关闭,我怎么能打开文件,即我不太明白哪个valgrind参数控制如何将内存泄漏记录到文件中.请帮忙!
我试图运行一个使用函数的程序concat_str.它可以将多个参数作为字符串,参数的结尾表示为"quit".我的功能代码如下:
char *concat_str(char *str1, ...)
{
va_list pstr;
char *minion = NULL, *temp = NULL;
minion = (char*) malloc (sizeof(str1));
strcpy (minion,str1);
va_start (pstr, str1);
if ( strcmp ("quit",str1) == 0)
{
va_end (pstr);
return minion;
}
while (1)
{
temp = va_arg (pstr, char *);
if ( strcmp ("quit", temp) == 0)
{
break;
}
minion = (char*) realloc (minion, sizeof(temp));
strncat (minion,temp,sizeof(temp));
}
va_end (pstr);
return minion;
}
Run Code Online (Sandbox Code Playgroud)
对此的调用声明将是:
char *result;
result = concat_str("hello", …Run Code Online (Sandbox Code Playgroud) 我正在尝试调试一个给出错误的程序:Abort(core dumped).Valgrind检测到堆栈粉碎并给出一个LEAK SUMMARY,其中1个块仍然可以访问.它向函数downloadAndOpen的第12行发出信号,在那里我有一个我认为在main结束时关闭的fopen,但它似乎不是.我很感激这个bug的帮助.valgrind输出是:
*** stack smashing detected ***: ./mtg terminated
==9594==
==9594== HEAP SUMMARY:
==9594== in use at exit: 352 bytes in 1 blocks
==9594== total heap usage: 1 allocs, 0 frees, 352 bytes allocated
==9594==
==9594== 352 bytes in 1 blocks are still reachable in loss record 1 of 1
==9594== at 0x402A17C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==9594== by 0x40BE62B: __fopen_internal (iofopen.c:73)
==9594== by 0x40BE70A: fopen@@GLIBC_2.1 (iofopen.c:103)
==9594== by 0x8048729: downloadAndOpen (downloadAndOpen.c:12)
==9594== by 0x80485B5: main (mtg.c:15)
==9594==
==9594== LEAK …Run Code Online (Sandbox Code Playgroud)