- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在编写一些 C 代码,并尝试使用列表。出于某种原因,以下代码为我提供了本书的作者、年份和 ISBN,但缺少标题。我怀疑这是“insert_at_begin”函数某处的内存泄漏。虽然我真的不确定在那里做什么。该程序从包含书名、作者等的文件中读取,并应将其作为动态列表返回。
任何提示或某种帮助将不胜感激。我确实环顾了互联网和堆栈溢出,但找不到解决此内存泄漏的方法。我添加了相关代码,虽然很长,但我不确定泄漏在哪里,所以我认为这样会更好。
typedef struct _element element;
typedef struct _list {
element *first;
int count;
} list;
struct _element {
char* title;
char* author;
int year;
long long int isbn;
element *next;
};
element *insert_at_begin(element *first, element *new_elem) {
if(first) {
new_elem->next = first;
first = new_elem;
} else {
first = new_elem;
}
return first;
}
element *construct_element(char *title, char* author, int year, long long int isbn) {
element* book = malloc(sizeof(element));
book->title = title;
book->author = author;
book->year = year;
book->isbn = isbn;
book->next = NULL;
return book;
}
void free_list(list *alist) {
//Only when there is 2 elements in the list start this loops
for (int i=1; i<alist->count; i++) {
free(alist->first->next);
}
free(alist->first);
free(alist);
}
void read_list(char* filename, list *alist) {
element* new_elem;
char* title;
char* author;
int year;
long long int isbn;
read_line_context ctx;
open_file(&ctx, filename);
while(read_line(&ctx, &title, &author, &year, &isbn) == 0) {
new_elem = construct_element(title, author, year, isbn);
alist->first = insert_at_begin(alist->first, new_elem);
alist->count++;
}
}
list* construct_list() {
list *alist = malloc(sizeof(list));
alist->first = NULL;
alist->count = 0;
return alist;
}
void print_list(list *alist) {
printf("My Books\n================\n\n");
int counter = 1;
element *elem = alist->first;
while (elem != NULL) {
printf("Book %d\n", counter);
printf("\tTitel: %s\n", elem->title);
printf("\tAuthor: %s\n", elem->author);
printf("\tYear: %d\n", elem->year);
printf("\tISBN: %lld\n", elem->isbn);
elem = elem->next;
counter++;
}
}
int main(int argc, char** argv) {
list *alist = construct_list();
read_list(argc>1?argv[1]:"buecherliste.txt", alist);
print_list(alist);
free_list(alist);
return 0;
}
这就是将要打印的内容,您可以看到标题丢失了,一些信息也被截断了。我确实尝试过 X Code Leaks 工具,但在那里找不到任何有用的东西。我在 read_list 的 while 循环中添加了一个用于调试的 printf 函数,它在那里工作得很好。所以它必须与 insert_at_begin 函数有关。
My Books
================
Book 1
Titel:
Author: Phillip K. Dick
Year: 1973
ISBN: 9780547572178
Book 2
Titel:
Author: nner Darkly
Year: 1949
ISBN: 9783548267456
更新:这是 read_line 函数:
int read_line(read_line_context *ctx, char **name, char **author, int *year, long long int *isbn) {
if (ctx->filepointer == NULL){
perror(ctx->filename);
exit(1);
}
char *name_s;
char *author_s;
char *year_s;
char *isbn_s;
char *delim = ";";
ssize_t len;
if ((len = getline(&(ctx->line), &(ctx->linecapp), ctx->filepointer)) != -1)
{
/* remove tailing newline */
char *pos;
if ((pos = strchr(ctx->line, '\n')) != NULL)
*pos = '\0';
/* read individual fields */
name_s = strtok(ctx->line, delim);
author_s = strtok(NULL, delim);
year_s = strtok(NULL, delim);
isbn_s = strtok(NULL, delim);
if(name_s != NULL && author_s != NULL && year_s != NULL && isbn_s != NULL ) {
*name = name_s;
*author = author_s;
*year = atoi(year_s);
*isbn = atoll(isbn_s);
return 0;
}
}
fclose(ctx->filepointer);
ctx->filepointer = NULL;
ctx->linecapp = 0;
if (ctx->line != NULL ) {
free(ctx->line);
}
return -1;
}
最佳答案
在我看来,您似乎还没有为 author
和 title
分配任何内存。
struct _element {
char title[100];
char author[100];
int year;
long long int isbn;
element *next;
};
会更好,但会将您的标题/作者字符数限制为 100...
所以你真的应该使用 malloc
或 calloc
来分配你需要的内存 - 我建议上面的代码只是为了让事情在快速和肮脏的基础上工作。 ..
关于c - C链表中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40947034/
IntentReceiver 正在泄漏 由于 onDetachedFromWindow 在某些情况下未被调用。 @Override protected void onDetachedFromWind
好吧,我很难追踪这个内存泄漏。运行此脚本时,我没有看到任何内存泄漏,但我的 objectalloc 正在攀升。 Instruments 指向 CGBitmapContextCreateImage >
我编写了一个测试代码来检查如何使用 Instrument(Leaks)。我创建了一个单一 View 应用程序,单击按钮后我加载了一个像这样的新 View ... - (IBAction)btn_clk
我正在使用这个简单的代码并观察单调增加的内存使用量。我正在使用这个小模块将内容转储到磁盘。我观察到它发生在 unicode 字符串上而不是整数上,我做错了什么吗? 当我这样做时: >>> from u
我有以下泄漏的代码。 Instruments 表示,泄漏的是 rssParser 对象。我“刷新”了 XML 提要,它运行了该 block 并且发生了泄漏...... 文件.h @interface
我在我编写的以下代码片段中发现了内存泄漏 NSFileManager *fileManager=[[NSFileManager alloc] init]; fileList=[[fileManager
因此,我正在开发HTML5 / javascript rts游戏。观察一直有几种声音在播放。因此,对我来说,是一段时间后声音听起来像是“崩溃”,并且此浏览器选项卡上的所有声音都停止了工作。我只能通过重
下面是我正在使用的一段代码及其输出。 my $handle; my $enterCount = Devel::Leak::NoteSV($handle); print "$date entry $en
在这篇关于 go-routines 泄漏的帖子之后,https://www.ardanlabs.com/blog/2018/11/goroutine-leaks-the-forgotten-sende
我想知道为什么在执行 ./a.out 后随机得到以下结果。有什么想法我做错了吗?谢谢 http://img710.imageshack.us/img710/8708/trasht.png 最佳答案 正
我正在 Swift 中开发一个应用程序,在呈现捕获我放在一起的二维码的自定义 ViewController 后,我注意到出现了巨大的内存跳跃。 该代码本质上基于以下示例:http://www.appc
下面是我的 javascript 代码片段。它没有按预期运行,请帮我解决这个问题。 function getCurrentLocation() { console.log("insi
我们在生产环境中部署了 3 个代理 Kafka 0.10.1.0。有些应用程序嵌入了 Kafka Producer,它们将应用程序日志发送到某个主题。该主题有 10 个分区,复制因子为 3。 我们观察
我正在使用仪器来检测一些泄漏,但有一些泄漏我无法解决; NSMutableString *textedetails = [[NSMutableString alloc] init];
如果我使用性能工具测试我的代码 - 泄漏,它没有检测到任何泄漏。这是否意味着代码没有泄漏任何内存? 我有一个越狱的 iPhone,我可以监控可用内存。如果有人知道,那就是 SBSettings。我测试
我在从 AddressBook 中获取图像时遇到了很大的问题,下面我粘贴了我的代码。此 imageData 从未被释放,在我的 Allocations Instruments 上它看起来总是在内存中它
- (NSMutableArray *)getArrayValue:(NSArray *)array{ NSMutableArray *valueArray = [NSMutableArra
Instruments 工具说这是一个泄漏,有什么想法吗? 我在 for 循环结束时释放变量对象 在上述方法的开头,这就是我设置变量对象的方式,即自动释放; NSMutableArray *varia
我正在跟踪我的 iOS 应用程序的内存泄漏,我有一个奇怪的泄漏导致我的应用程序崩溃......负责的框架是:CGImageMergeXMPPropsWhithLegacyProps。在某些时候,我的应
我正在尝试使用 NSOperationQueue 在后台线程中执行一个方法,如下所示: NSOperationQueue *queue = [NSOperationQueue new]; NS
我是一名优秀的程序员,十分优秀!