Advanced usage: GtkSpell Reference Manual

## Advanced usage

For convenience, `gtk_spell_checker_new()` is created as `GInitiallyUnowned` and `gtk_spell_checker_attach()` will sink the floating reference. `gtk_spell_checker_attach()` also connects the `destroy` signal of the passed-in `GObject` to `g_object_unref()` the `GtkSpellChecker`, so in the most common use-case, you need not worry about cleaning it up. However, if you want to detach and later reattach the `GtkSpellChecker` to a (possibly different) `GtkTextView`, you must get a pointer to it with `gtk_spell_checker_get_from_text_view()`, call `g_object_ref()` on the resulting pointer, call `gtk_spell_checker_detach()` on it, call `gtk_spell_checker_attach()` with the new `GtkTextView`, and finally call `g_object_unref()` to release the reference that you took at the beginning, like this:

    GtkTextView* view = gtk_text_view_new ();
    GtkSpellChecker* spell = gtk_spell_checker_new ();
    gtk_spell_checker_set_language (spell, "en_US", NULL);
    gtk_spell_checker_attach (GTK_TEXT_VIEW (view));
    /* ... */
    // Detach
    g_object_ref (spell);
    gtk_spell_checker_detach (spell);
    /* ... */
    // Reattach
    gtk_spell_checker_attach (spell, GTK_TEXT_VIEW (view));
    g_object_unref (spell);

Alternatively, you can sink the `GtkSpellChecker` immediately upon construction with `g_object_ref_sink()`, in which case you will retain the ownership of the `GtkSpellChecker` throughout the lifetime of the program, and you must remember to call `g_object_unref()` when you don't need it any more, like this:

    GtkTextView* view = gtk_text_view_new ();
    GtkSpellChecker* spell = gtk_spell_checker_new ();
    g_object_ref_sink (spell);
    gtk_spell_checker_set_language (spell, "en_US", NULL);
    gtk_spell_checker_attach (GTK_TEXT_VIEW (view));
    /* ... */
    // Detach
    gtk_spell_checker_detach (spell);
    /* ... */
    // Reattach
    gtk_spell_checker_attach (spell, GTK_TEXT_VIEW (view));
    /* ... */
    // End of life time
    g_object_unref (spell);