Getting assertion failed error in gtk. - c

I'm new to using GTK. Here is a small section of my code. The aim is to copy the entire current line. The contents are stored in "line". "start" and "end" are textiter at start and end of line.
gtk_text_iter_set_line_offset (start, 0);
gtk_text_iter_forward_to_line_end (end);
line = gtk_text_iter_get_text (start, end);
gtk_clipboard_set_text (clipboard, line, -1);
And upon execution, Im getting the following error messages.
Gtk[27786]: CRITICAL: gtk_text_iter_set_line_offset: assertion 'iter != NULL' failed
Gtk[27786]: CRITICAL: gtk_text_iter_forward_to_line_end: assertion 'iter != NULL' failed
Gtk[27786]: CRITICAL: gtk_text_iter_get_text: assertion 'start != NULL' failed
Gtk[27786]: CRITICAL: gtk_clipboard_set_text: assertion 'text != NULL' failed
What is wrong with the code block? How can i resolve it?
Thanks all :)

You have probably got your iterators declared like GtkTextIter *start. Instead, according to the text widget conceptual overview in GTK's documentation, "GtkTextIter is a struct designed to be allocated on the stack; it's guaranteed to be copiable by value and never contain any heap-allocated data." This means you should not declare them as pointers:
GtkTextIter start, end;
// ...
gtk_text_iter_set_line_offset (&start, 0);
gtk_text_iter_forward_to_line_end (&end);
line = gtk_text_iter_get_text (&start, &end);

Related

calloc implementaion in tlsf assertion failed

after executing the below function on an embedded system (esp32), the assertion in heap_tlsf.c fails, my code is:
remained_data = (char *)calloc(lbws - where_to_insert + 2, sizeof(char));
the lbws - where_to_insert + 2 evaluates around 1100, this call will panic and gives the below assertion error:
assert failed: insert_free_block heap_tlsf.c:239 (current && "free list cannot have a null entry")
I can't figure out what is the meaning of this assertion, and what is the cause for it, so as the solution for it.
so what should I do to fix this? thank you.

glib g_malloc0() wants to allocate way too much memory

Upon calling addrtab = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, NULL); the execution aborts and the following is reported: GLib-ERROR **: 15:23:58.535: ../../../glib/gmem.c:138: failed to allocate 11755944670652 bytes. What puzzles me is the amount of memory requested.
Now a bit of details: I'm writing in C using glib-2.66 on Ubuntu 20.04. Commenting out the previously reported line solves the error entirely. g_hash_table_new_full() is used elsewhere in the program without causing any error.
For completeness I'm posting the function that calls that g_hash_table_new_full()
void addr_advertise (hash_node_t* alice, hash_node_t* bob){
GHashTable* addrtab = NULL;
if ((alice->data->key != bob->data->key) && !(alice->data->attackerid == 1 && bob->data->attackerid == 1)) {
addrtab = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, NULL);
if(alice->data->attackerid == -1){
//populate_honest_addr_table(addrtab, alice->data->known_peers);
}
else{
//add_malicious_to_table(addrtab);
}
execute_addr(simclock + FLIGHT_TIME, alice, bob, 5, simclock, alice->data->key, addrtab);
}
}
addr_advertise itself is called multiple times before causing the error.
As reported in gmem.c the error is caused by a g_malloc0() (line 138) that requests too much memory. How is it possible that g_malloc asks for so many bytes? Why previous executions of the same function work perfectly and most of all what could be the origin of the error?

Iterators do not belong to their assigned text buffer

This is a bare bones save_as() function:
gint save_as(GtkWidget *parent, struct buffers B)
{
GtkWidget *file_chooser = gtk_file_chooser_dialog_new("Save As", GTK_WINDOW(parent), GTK_FILE_CHOOSER_ACTION_SAVE, "Cancel", GTK_RESPONSE_CANCEL, "Save", GTK_RESPONSE_OK, NULL);
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(file_chooser), "Untitled");
gint response = gtk_dialog_run(GTK_DIALOG(file_chooser));
switch(response)
{
case GTK_RESPONSE_OK:
GFile *file = g_file_new_for_path(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_chooser)));
GtkTextIter *start;
gtk_text_buffer_get_start(B.buffer0, &start);
GtkTextIter *end;
gtk_text_buffer_get_end(B.buffer0, &end);
// program abnormally terminates on the following line
gchar *contents = gtk_text_buffer_get_text(B.buffer0, &start, &end, FALSE);
g_file_replace_contents(file, contents, strlen(contents), NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, NULL);
g_free(contents);
gtk_widget_destroy(file_chooser);
return GTK_RESPONSE_OK;
break;
case GTK_RESPONSE_CANCEL:
gtk_widget_destroy(file_chooser);
return GTK_RESPONSE_CANCEL;
}
// user pressed X
gtk_widget_destroy(file_chooser);
return GTK_RESPONSE_CANCEL;
}
This is pretty much all we need to diagnose and fix the problem.
Here is the full message I get when I try to click on File -> Save As... and then on Save:
(test:4478): Gtk-WARNING **: 11:56:20.184: Invalid text buffer iterator: either the iterator is uninitialized, or the characters/pixbufs/widgets in the buffer have been modified since the iterator was created.
You must use marks, character numbers, or line numbers to preserve a position across buffer modifications.
You can apply tags and insert marks without invalidating your iterators, but any mutation that affects 'indexable' buffer contents (contents that can be referred to by character offset) will invalidate all outstanding iterators
(test:4478): Gtk-CRITICAL **: 11:56:20.184: gtk_text_buffer_get_text: assertion 'gtk_text_iter_get_buffer (start) == buffer' failed
Segmentation fault (core dumped)
For some reason, it seems that the iterators start and end do not belong to B.buffer0. Apart from GTK+3 documentation, I also followed answers to this question as a guide.
Why is this happening and how it can be fixed?
I also tried changing the following lines:
gtk_text_buffer_get_start_iter(B.buffer0, &start); to gtk_text_buffer_get_start_iter(B.buffer0, start);
gtk_text_buffer_get_start_iter(B.buffer0, &end); to gtk_text_buffer_get_start_iter(B.buffer0, end);
gchar *contents = gtk_text_buffer_get_text(B.buffer0, &start, &end, FALSE); to gchar *contents = gtk_text_buffer_get_text(B.buffer0, start, end, FALSE);
The only error I get is:
Segmentation fault (core dumped)
This is also supposed to be the correct way to send arguments according to the documentation.
Also, I tried replacing contents = gtk_text_buffer_get_text(B.buffer0, start, end, FALSE); with contents = gtk_text_iter_get_text(start, end); but I get the same error.
I also noticed that I get two warnings during compiling after applying those changes:
src/save.c:96:5: warning: ‘start’ may be used uninitialized in this function [-Wmaybe-uninitialized]
gtk_text_buffer_get_start_iter(B.buffer0, start);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/save.c:98:5: warning: ‘end’ may be used uninitialized in this function [-Wmaybe-uninitialized]
gtk_text_buffer_get_end_iter(B.buffer0, end);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This obviously means that gtk_text_buffer_get_start/end_iter() fails to associate start and end with B.buffer0, but why? It says that start and end are passed uninitialized, but isn't that normal since I'm initializing them now? Or I should do something before that?
Definition of struct buffer is in a file called buffer.h. Here it is (not the whole file, just the struct definition):
struct buffers
{
GtkTextBuffer *buffer0;
GtkTextBuffer *buffer1;
GtkTextBuffer *buffer2;
};
The instance I use is:
struct buffers Buffer;
This is a global variable, so buffer.h is included in every file that uses Buffer. And for the same purpose (globality), of course it is declared extern struct buffers Buffer; in the header file associated with the source file in which it is defined.
This buffer is passed to a function save_as() like this:
gint response = save_as(main_window, Buffer);
In case you're wondering why I just don't use global variable instead of passing it as a parameter, it is because I can just pass any variable of type struct buffers to a save_file() and let it do the job, instead of having another function for each struct buffers variable.
I tried initializing iterators upon their definition:
GtkTextIter *start = NULL;
GtkTextIter *end = NULL;
Everything compiles without any warnings. Unfortunately, this doesn't solve the problem. Upon clicking File -> Save As and then Save, the program exits and I get messages:
(test:4081): Gtk-CRITICAL **: 19:48:29.843: gtk_text_buffer_get_start_iter: assertion 'iter != NULL' failed
(test:4081): Gtk-CRITICAL **: 19:48:29.844: gtk_text_buffer_get_end_iter: assertion 'iter != NULL' failed
(test:4081): Gtk-CRITICAL **: 19:48:29.844: gtk_text_buffer_get_text: assertion 'start != NULL' failed
Why is it even important that my start and end iterators are NULL at least in the first two cases? Aren't those two functions supposed to set start and end iterators to the start and the end of a file, respectively? Therefore, why does it matter to what iterators are set prior to that?
Finally solved it. It looks like that GtkTextIter is a little bit different than most GTK types.
Here is how it should be done:
gint save_as(GtkWidget *parent, struct buffers B)
{
GtkWidget *file_chooser = gtk_file_chooser_dialog_new("Save As", GTK_WINDOW(parent), GTK_FILE_CHOOSER_ACTION_SAVE, "Cancel", GTK_RESPONSE_CANCEL, "Save", GTK_RESPONSE_OK, NULL);
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(file_chooser), "Untitled");
gint response = gtk_dialog_run(GTK_DIALOG(file_chooser));
switch(response)
{
case GTK_RESPONSE_OK:
GFile *file = g_file_new_for_path(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(file_chooser)));
GtkTextIter start;
gtk_text_buffer_get_start(B.buffer0, &start);
GtkTextIter end;
gtk_text_buffer_get_end(B.buffer0, &end);
// program abnormally terminates on the following line
gchar *contents = gtk_text_buffer_get_text(B.buffer0, &start, &end, FALSE);
g_file_replace_contents(file, contents, strlen(contents), NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL, NULL);
g_free(contents);
gtk_widget_destroy(file_chooser);
return GTK_RESPONSE_OK;
break;
case GTK_RESPONSE_CANCEL:
gtk_widget_destroy(file_chooser);
return GTK_RESPONSE_CANCEL;
}
// user pressed X
gtk_widget_destroy(file_chooser);
return GTK_RESPONSE_CANCEL;
}
As you can see, the only difference in this code and the code from my question are the following two lines:
Wrong
GtkTextIter *start;
GtkTextIter *end;
Right
GtkTextIter start;
GtkTextIter end;
Most of the GTK types want you to make them pointers, but this one is an exception.

Expression: (L "String is not null terminated" & & 0)

I am fiddling around with mailslots and now I've run into a problem.
Whenever I try to run, I get the error message in the title, but I don't know how I should go about fixing it.
What I am trying to do is "fixing" the full path of the mailslot, but it seems to not like the strcat_s-part.
HANDLE mailslotCreate (char *name) {
char fullName[50] = "\\\\.\\mailslot\\";
strcat_s(fullName, strlen(fullName), name);
return CreateMailslot(fullName, 0, TIME_OUT, NULL);
}
Imgur link to error
EDIT: Changing the strlen to sizeof merely changed the error to "Buffer size too small" instead.
See documentation on strcat_s. It says that second parameter should be the size of destination buffer. As you pass strlen(fullName), there is no room for terminating \0.
Change it to be sizeof(fullName) and your error should disappear.

gtk+ text editor, open python script aborts complaining about invalid utf-8 text

I'm in the debugging phase of writing my text editor using gtk+ 2.0 & gtksourceview 2.0. When I open certain files (previously edited in geany, and usually python files) the editor crashes with the following output:
(ledit:23515): Gtk-CRITICAL **: IA__gtk_text_buffer_set_text: assertion `GTK_IS_TEXT_BUFFER (buffer)' failed
**
GLib:ERROR:gutf8.c:1915:_g_utf8_make_valid: assertion failed: (g_utf8_validate (string->str, -1, NULL))
Aborted
I've tried to trap this error as follows:
char *path,*string;
GtkTextBuffer *tbuffer;
gsize length = -1;
path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
g_file_get_contents(path,&string,&length,NULL);
if (g_utf8_validate(string,length,NULL))
{
...
gtk_text_buffer_set_text(tbuffer,string,-1);
...
}
else
{
printf("invalid utf-8 data\n");
}
but this fails to work. I have two questions:
why did this fail to trap the error?
what else can I do to make the string valid utf-8 on the fly?
It turns out I was trying to trap the error on the wrong function. The function that was throwing the error was g_file_get_contents. I modified the code block as shown below, and the problem files are now opening fine, which I am kind of puzzled by, since it is still perfoming the same function that was throwing the error, but now it is in the condition of the if statement, and it works fine???
char *path,*string;
GtkTextBuffer *tbuffer;
gsize length = -1;
path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
if ( g_file_get_contents(path,&string,&length,NULL) )
{
...
gtk_text_buffer_set_text(tbuffer,string,-1);
...
}
else
{
printf("file did not open\n");
}

Resources