Tag: 内存泄漏

strdup()导致内存泄漏?

我已经实现了一个返回字符串的函数。 它将整数作为参数( age ),并返回格式化的字符串。 一切都运作良好,除了我有一些疯狂的内存泄漏。 我知道strdup()是导致这种情况的原因,但我试图研究一些修复无济于事。 我的代码是: const char * returnName(int age) { char string[30]; sprintf( string, “You are %d years old”, age); return strdup(string); } Valgrind的输出是: ==15414== LEAK SUMMARY: ==15414== definitely lost: 6,192 bytes in 516 blocks ==15414== indirectly lost: 0 bytes in 0 blocks ==15414== possibly lost: 0 bytes in 0 blocks ==15414== […]

指针指针的内存泄漏

我在这个网页上使用Dijkstra算法和邻接列表。 由于我想多次构建不同的图形并计算最短路径,因此我添加了一些“自由”命令来释放内存。 但是,在多次迭代后,内存使用量仍会增加 这是我修改过的代码: // C / C++ program for Dijkstra’s shortest path algorithm for adjacency // list representation of graph #include #include #include // A structure to represent a node in adjacency list struct AdjListNode { int dest; int weight; struct AdjListNode* next; }; // A structure to represent an adjacency liat struct AdjList […]

对传递引用感到困惑

考虑以下示例,我尝试以C方式传递引用: // Function prototypes void increment(unsigned* number); int main() { unsigned* thing; increment(thing); cout << *thing; return 0; } void increment(unsigned* number) { number = (unsigned*) malloc(sizeof(unsigned)); *number = 1; } 我在行cout << *thing遇到程序崩溃。 是的,我在这里使用C ++但是我想尝试C版本的pass-by-reference,因为我的主要项目是在C. 我通过更改代码修复它,如下所示: // Function prototypes void increment(unsigned** number); int main() { unsigned* thing; increment(&thing); cout << *thing; return 0; } […]

CFString内存泄漏

我已将内存泄漏缩小到以下代码 CFStringRef CFDataToString(CFDataRef data) { UInt8* buf = malloc(CFDataGetLength(data)); CFDataGetBytes(data, CFRangeMake(0, CFDataGetLength(data)), buf); CFMutableStringRef output = CFStringCreateMutable(kCFAllocatorDefault, CFDataGetLength(data) * 2); for(int i = 0; i < CFDataGetLength(data); i++) { CFStringAppendFormat(output, NULL, CFSTR("%02x"), buf[i]); } free(buf); CFRelease(data); return output; } 下面是上下文中使用的代码,一些方法已经过简化以进行演示。 Instruments报告了CFStringCreateMutable和CFStringAppendFormat的内存泄漏。 CFStringRef CFDataToString(CFDataRef data) { UInt8* buf = malloc(CFDataGetLength(data)); CFDataGetBytes(data, CFRangeMake(0, CFDataGetLength(data)), buf); CFMutableStringRef output […]

内存泄漏在代码中的某处

此代码适用于包含1000个单词的txt,但是当我使用10000个单词时,它会停止响应。 另外,当我使用动态数组而不是二叉树时,main.c使用10000个单词。 所以我认为问题出在tree.c代码中… tree.h中 #ifndef TREE_H_ #define TREE_H_ typedef struct Item{ char* key; int no; } TItem; typedef struct No{ TItem item; struct No* pLeft; struct No* pRight; } TNo; void TTree_Insert (TNo**, char[]); void TTree_Print (TNo*); #endif tree.c #include #include #include #include “tree.h” TNo* TNo_Create (char* c){ TNo* pNo = malloc(sizeof(TNo)); pNo->item.key = malloc(sizeof(char)*strlen(c)); […]

使用libavfilter过滤video时出现大量内存泄漏

我有一个相对简单的FFMPEG C程序,video帧被馈送,通过滤波器图处理并发送到帧渲染器。 以下是一些代码段: /* Filter graph here */ char args[512]; enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_RGB32 }; AVFilterGraph *filter_graph; avfilter_register_all(); AVFilter *buffersrc = avfilter_get_by_name(“buffer”); AVFilter *buffersink = avfilter_get_by_name(“ffbuffersink”); AVBufferSinkParams *buffersink_params; AVFilterInOut *outputs = avfilter_inout_alloc(); AVFilterInOut *inputs = avfilter_inout_alloc(); filter_graph = avfilter_graph_alloc(); snprintf(args, sizeof(args), “video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d”, av->codec_ctx->width, av->codec_ctx->height, av->codec_ctx->pix_fmt, av->codec_ctx->time_base.num, av->codec_ctx->time_base.den, av->codec_ctx->sample_aspect_ratio.num, av->codec_ctx->sample_aspect_ratio.den); if(avfilter_graph_create_filter(&av->buffersrc_ctx, buffersrc, “in”,args, NULL, filter_graph) […]

我的程序输出没有在C中更新

我有下面的c代码,它工作正常一次,再次调用该函数后,它给出了相同的结果。 缓冲区未更新。 #define _XOPEN_SOURCE #include #include #include #include char *EpochToMMDDYYYY(long ); int main() { long epoch = 1492151737; printf(“date of %ld is %s\n”,epoch,EpochToMMDDYYYY(epoch)); long ep = 1492222737; printf(“date of %ld is %s\n”,epoch,EpochToMMDDYYYY(ep)); long epc = 1491111737; printf(“date of %ld is %s\n”,epoch,EpochToMMDDYYYY(epc)); return 0; } char *EpochToMMDDYYYY(long ep) { struct tm tm; char b[25]; memset(b,0,sizeof(b)); //setenv(“TZ”, […]

p编程中的pthread内存泄漏

我有下面的代码。 void *timer1_function(void * eit); pthread_t timer1; int thread_check1 = 0; line72: thread_check1 = pthread_create( &timer1, NULL, timer1_function, NULL); Valgrind显示下面的输出,并表示line 72存在问题。 上面的pthread_create用法有什么问题? 272 bytes in 1 blocks are possibly lost in loss record 2 of 5 in main in main.c:72 1: calloc in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so 2: _dl_allocate_tls in /build/buildd/eglibc-2.15/elf/dl-tls.c:297 3: pthread_create@@GLIBC_2.2.5 in /build/buildd/eglibc-2.15/nptl/allocatestack.c:571 4: main in […]

json_decref没有释放内存?

此问题与用于C的libjansson JSON API有关json_decref函数用于跟踪对json_t对象的引用数,当引用数达到0 ,应释放已分配的内存。 那为什么这个程序会导致内存泄漏? 我错过了什么? 是不是没有垃圾收集? int main() { json_t *obj; long int i; while(1) { // Create a new json_object obj = json_object(); // Add one key-value pair json_object_set(obj, “Key”, json_integer(42)); // Reduce reference count and because there is only one reference so // far, this should free the memory. json_decref(obj); } return […]

glib2实际上是否通过ALWAYS-MALLOC泄漏内存?

这个问题与其他许多问题不重复,因为我确实使用了G_DEBUG=gc-friendly和G_SLICE=always-malloc这是源代码: #include int main (int argc, char *argv[]) { GHashTable *ht; ht=g_hash_table_new(g_str_hash,g_str_equal); g_hash_table_insert(ht,”foo”,”bar”); g_hash_table_destroy(ht); return 0; } 以下是valgrind在此代码上的输出: # G_DEBUG=gc-friendly G_SLICE=always-malloc valgrind –leak-check=full –show-reachable=yes ./test_vg ==1880== Memcheck, a memory error detector ==1880== Copyright (C) 2002-2010, and GNU GPL’d, by Julian Seward et al. ==1880== Using Valgrind-3.6.0 and LibVEX; rerun with -h for copyright info ==1880== […]