C语言调用函数发生段错误,发生段错误的函数是没有执行就段错误了

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言调用函数发生段错误,发生段错误的函数是没有执行就段错误了相关的知识,希望对你有一定的参考价值。

fun1(int)

printf("22222");
........
........

main()

init_config();
printf(“11111”);
fun1(100);
free();


终端打印情况是11111会打印,2222不会打印,说明fun1并没有执行,然后就提示段错误,问可能是什么情况
调用顺序aud_toohot_config_create();aud_too_hot(80);aud_clipping_config_free();就是aud_too_hot() 那个函数进不去不会打印1111111,就直接段错误了config_create()确定有执行的

init_config()函数中可能存在多线程调用已经释放的资源!
#include <stdio.h>
void fun1(int)
printf("22222");


int main()

//init_config();
printf("111111");
fun1(100);

return 0;
追问

init哪里只有malloc跟参数初始化没线程呢,我修改了提问,放上了图片
动态库会不会可能导致段错误的情况呀

追答

你在temp_config->...前面加一句输出看看
if(temp_config==NULL)
printf("33333333");
return -1;

追问

不行呢,没有打印 333333,效果还是跟以前一样,感觉malloc没有分配到内存的话,aud_too_hot()的第一句应该还能打印才对

追答

你的调用代码是什么样的?怎么组织的?

追问

测试代码:


终端   ”toohot_config” 是会打印的

还有一点是很奇妙的,我的源码还有实现其他的功能接口,其他的接口功能测试的时候也是有类似的段错误现象

追答

你确定aud_toohot_config_create();的返回值是0吗?temp_config指针的作用域有没有问题?
把所有的编译生成的.o清除,重新编译一下!
你这是在什么系统下开发的程序?c语言的应用场景比较少,你在做什么呢?

参考技术A C语言要用英文的标点符号,你代码里的那些看起来就像是空格连着的标点是中文的,改过来追问

我简化写嘛,我放上图片了,大神有空看看,只是调用接口测试而已,这bug已经折磨我快10多天了

参考技术B 你很多标点都是中文符号,要改成英文的。追问

我简化写嘛,我放上图片了,大神有空看看,只是调用接口测试而已,这bug已经折磨我快10多天了

参考技术C fun1(int)怎么没有形参??这能编译通过吗?
init_config();
该函数做了什么?
free();
free了什么?怎么没有参数??追问

我简化写嘛,我放上图片了,有空看看

GTK Glade C - Treeview选择奇怪的段错误

我正在学习GTK和Glade。我正在一点一点地尝试制作一个小型的会计程序。现在,我发生了一个奇怪的段错误,我无法理解为什么。我只想使用gtk_tree_selection_unselect_all取消选择所有GtkTreeSelection。请注意,如果尝试全选,它也会崩溃。

奇怪的是,如果我注释掉我的on_treeview_selection1_changed函数,就不会发生段错误。所以看起来这个功能在某种程度上与另一个混淆了..它有可能吗?

main.c:

#include <gtk/gtk.h>

typedef struct {
    GtkTreeView *treeview;
    GtkWidget *ent_date;
    GtkWidget *ent_lib;
    GtkWidget *ent_mont;
    GtkWidget *but_unset;
} app_widgets;

enum
{
    DATE_COLUMN,
    LIBELLE_COLUMN,
    MONTANT_COLUMN,
    N_COLUMN
};

void on_window_main_destroy() {
    gtk_main_quit();
}

void on_button_unset_clicked (GtkButton *button, app_widgets *app_wid) {

    GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(app_wid->treeview));

    gtk_tree_selection_unselect_all(selection);
    //gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
    //gtk_tree_selection_select_all(selection);

}

void on_treeview_selection1_changed (GtkTreeSelection *treeselection, app_widgets *app_wid) {

    gchar *a,*b,*c;
    GtkTreeIter iter;
    GtkTreeModel *model;

    if (gtk_tree_selection_get_selected(treeselection, &model, &iter))
    {
        gtk_tree_model_get(model, &iter, DATE_COLUMN, &a, LIBELLE_COLUMN, &b,MONTANT_COLUMN, &c, -1);

    }

    gtk_entry_set_text(GTK_ENTRY(app_wid->ent_date), a);
    gtk_entry_set_text(GTK_ENTRY(app_wid->ent_lib), b);
    gtk_entry_set_text(GTK_ENTRY(app_wid->ent_mont), c);

    g_free(a);
    g_free(b);
    g_free(c);

}

int main(int argc, char *argv[])
{
    GtkBuilder *builder;

    app_widgets *widgets = g_slice_new(app_widgets);

    gtk_init(&argc, &argv);

    builder = gtk_builder_new();
    gtk_builder_add_from_file (builder, "test.glade", NULL);

    widgets->treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeview1"));
    widgets->ent_date  = GTK_WIDGET(gtk_builder_get_object(builder, "entry_date"));
    widgets->ent_lib = GTK_WIDGET(gtk_builder_get_object(builder, "entry_lib"));
    widgets->ent_mont = GTK_WIDGET(gtk_builder_get_object(builder, "entry_mont"));
    widgets->but_unset = GTK_WIDGET(gtk_builder_get_object(builder, "button_unset"));

    gtk_builder_connect_signals(builder, widgets);

    gtk_widget_show(GTK_WIDGET(gtk_builder_get_object(builder, "window1")));

    g_object_unref(builder);

    gtk_main();

    g_slice_free(app_widgets, widgets);
    return 0;
}

和test.glade文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
  <requires lib="gtk+" version="3.12"/>
  <object class="GtkListStore" id="liststore1">
    <columns>
      <!-- column-name Date -->
      <column type="gchararray"/>
      <!-- column-name Libellé -->
      <column type="gchararray"/>
      <!-- column-name Montant -->
      <column type="gchararray"/>
    </columns>
    <data>
      <row>
        <col id="0" translatable="yes">20.05.2017</col>
        <col id="1" translatable="yes">Something - here</col>
        <col id="2" translatable="yes">30.20</col>
      </row>
      <row>
        <col id="0" translatable="yes">25.06.2017</col>
        <col id="1" translatable="yes">Something else - overthere</col>
        <col id="2" translatable="yes">24.90</col>
      </row>
      <row>
        <col id="0" translatable="yes">11.08.2017</col>
        <col id="1" translatable="yes">Third thing - lala</col>
        <col id="2" translatable="yes">5.15</col>
      </row>
    </data>
  </object>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <property name="margin_left">5</property>
    <property name="margin_right">5</property>
    <property name="margin_top">5</property>
    <property name="margin_bottom">5</property>
    <property name="window_position">center</property>
    <child>
      <object class="GtkBox" id="box1">
        <property name="visible">True</property>
        <property name="can_focus">False</property> 
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkScrolledWindow" id="scrolledwindow1">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="shadow_type">in</property>
            <child>
              <object class="GtkTreeView" id="treeview1">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="model">liststore1</property>
                <child internal-child="selection">
                  <object class="GtkTreeSelection" id="treeview-selection1">
                    <signal name="changed" handler="on_treeview_selection1_changed" swapped="no"/>
                 </object>
                </child>
                <child>
                  <object class="GtkTreeViewColumn" id="treeviewcolumn1">
                    <property name="resizable">True</property>
                    <property name="fixed_width">70</property>
                    <property name="title" translatable="yes">Date</property>
                    <child>
                      <object class="GtkCellRendererText" id="cellrenderertext1"/>
                      <attributes>
                        <attribute name="text">0</attribute>
                      </attributes>
                    </child>
                  </object>
                </child>
                <child>
                  <object class="GtkTreeViewColumn" id="treeviewcolumn2">
                    <property name="resizable">True</property>
                    <property name="title" translatable="yes">What</property>
                    <child>
                      <object class="GtkCellRendererText" id="cellrenderertext2"/>
                      <attributes>
                        <attribute name="text">1</attribute>
                      </attributes>
                    </child>
                  </object>
                </child>
                <child>
                  <object class="GtkTreeViewColumn" id="treeviewcolumn3">
                    <property name="resizable">True</property>
                    <property name="title" translatable="yes">Amount</property>
                    <child>
                      <object class="GtkCellRendererText" id="cellrenderertext3"/>
                      <attributes>
                        <attribute name="text">2</attribute>
                      </attributes>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkBox" id="box2">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkBox" id="box_date">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="orientation">vertical</property>
                <child>
                  <object class="GtkLabel" id="label1">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="label" translatable="yes">Date</property>
                  </object>
                 <packing>
                    <property name="expand">False</property>
                    <property name="fill">True</property>
                    <property name="position">0</property>
                  </packing>
                </child>
                <child>
                  <object class="GtkEntry" id="entry_date">
                    <property name="visible">True</property>
                    <property name="can_focus">True</property>
                  </object>
                  <packing>
                    <property name="expand">False</property>
                    <property name="fill">True</property>
                    <property name="position">1</property>
                  </packing>
                </child>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkBox" id="box_lib">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="orientation">vertical</property>
            <child>
              <object class="GtkLabel" id="label2">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">What</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkEntry" id="entry_lib">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">1</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkBox" id="box_mont">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="orientation">vertical</property>
            <child>
              <object class="GtkLabel" id="label3">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">Amount</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkEntry" id="entry_mont">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">True</property>
                <property name="position">1</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
      </object>
      <packing>
        <property name="expand">False</property>
        <property name="fill">True</property>
        <property name="position">1</property>
      </packing>
    </child>
    <child>
      <object class="GtkFixed" id="fixed1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkButton" id="button_unset">
            <property name="label" translatable="yes">Unselect</property>
            <property name="width_request">100</property>
            <property name="height_request">23</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <property name="xalign">0.47999998927116394</property>
            <property name="yalign">0.44999998807907104</property>
            <signal name="clicked" handler="on_button_unset_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="x">370</property>
            <property name="y">68</property>
          </packing>
        </child>
      </object>
      <packing>
        <property name="expand">False</property>
        <property name="fill">True</property>
        <property name="position">2</property>
      </packing>
      </child>
      </object>
    </child>
  </object>
</interface>
答案

on_treeview_selection1_changed()中,即使gtk_tree_selection_get_selected(treeselection, &model, &iter)返回FALSE,您也会尝试设置输入字段,因为目前没有选择。在这种情况下,这意味着变量a,b和c尚未由gtk_tree_model_get(model, &iter, DATE_COLUMN, &a, LIBELLE_COLUMN, &b,MONTANT_COLUMN, &c, -1)设置,并且调用三个gtk_entry_set_text指令将因此导致段错误(出于同样的原因,g_free指令也不起作用)。 A,b和c必须指向字符串而不是某些随机值。

如果您想要删除条目字段的内容以防没有选择,您可能只想使用gtk_entry_set_text(GTK_ENTRY(app_wid->ent_date), "");等。

以上是关于C语言调用函数发生段错误,发生段错误的函数是没有执行就段错误了的主要内容,如果未能解决你的问题,请参考以下文章

GDB调试之二栈溢出

数组值发生变化

GTK Glade C - Treeview选择奇怪的段错误

C语言 pthread_cleanup_push()和pthread_cleanup_pop()函数(用于临界资源程序段中发生终止动作后的资源清理任务,以免造成死锁,临界区资源一般上锁)

C基础知识(11):错误处理

C 错误处理