Thread: Freeing dynamic allocated memory of structure

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    10

    Freeing dynamic allocated memory of structure

    Hello guys, I have qusetion, I have following structure:

    Code:
    typedef struct
    {
    	GtkWidget* PoziomaLinijka; <--GTK
    	GtkWidget* PionowaLinijka; <--GTK
    	GtkWidget* Label1; <--GTK
    	GtkWidget* Label2; <--GTK
    	gint x,y; <--GTK
    } StrukturaDrawing;
    And now I dynamicly allocate memory for it:
    Code:
    StrukturaDrawing* Wsk;
      Wsk = (StrukturaDrawing*)malloc(sizeof(StrukturaDrawing));
      if (!Wsk)
      {
    	std::cout << "Error" << std::endl; 
      }
    It works great, it allocates it. Now I use GTK callbacks to do stuff with it, which works great but when I want to free memory of it:
    Callback to function: Wyjscie:
    Code:
     g_signal_connect(G_OBJECT(Okno), "destroy", G_CALLBACK(Wyjscie), Wsk);
    Code:
    void Wyjscie(GtkWindow* window, GdkEvent* event, StrukturaDrawing* data)
    {
    	gtk_main_quit();
    	free(data);
    	data = NULL;
    }
    I get:
    First-chance exception at 0x7c96df51 in Ekseprymetnowanie.exe: 0xC0000005: Access violation reading location 0xfffffff9.

    But if I write data = NULL and then free(data) which is nonsense, right? I get no warnings, also if I comment out freeing memory I get no error but then I will get memory leaks.

    Thanks in advance guys.
    Last edited by darekg11; 01-09-2011 at 10:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. clearing a c-string vs. freeing the memory there
    By donthate in forum C Programming
    Replies: 10
    Last Post: 10-04-2010, 04:31 AM
  2. dynamic array and structure problem
    By bluetxxth in forum C Programming
    Replies: 3
    Last Post: 04-13-2010, 06:56 AM
  3. free memory in structure
    By franziss in forum C++ Programming
    Replies: 22
    Last Post: 01-08-2007, 05:16 PM
  4. Dynamic memory confusion
    By ripper079 in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2002, 06:15 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM