Thread: Freeing dynamic allocated memory of structure

  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.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Is it C or C++? If it's C, use printf, not cout and remove the cast from malloc().
    Use a debugger. You can use valgrind also.

  3. #3
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    GTK is done in C, and has C++ bindings. What are you doing exactly? Looks like a mix of C/C++.
    Last edited by \007; 01-09-2011 at 10:35 AM.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    std::cout was just for test because I use Visual C++ Compilator because I got used to it and I need to use cast on malloc because it's c++ compilator. And I use C version of GTK.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So 0xfffffff9 is, what, -7? Somehow along the way, the pointer you malloc and the pointer you try to free are not the same. I don't know enough about GTK to know what the parameters to pass in to a destroy handler are supposed to be. (EDIT: My point there, which I appear to have forgotten to mention, is that if the number/type of things your function expects don't match what a destroy handler is supposed to take, then you can wind up in bad places, since you aren't calling the function yourself -- the main loop is calling it for you, and it will pass in whatever it thinks a destroy handler is supposed to need.)

    Not that it matters here, since destroy should probably only be called once, but you do realize that data = NULL has no effect? If you're expecting Wsk to be changed in the calling function, it won't.
    Last edited by tabstop; 01-09-2011 at 11:16 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by darekg11
    std::cout was just for test because I use Visual C++ Compilator because I got used to it and I need to use cast on malloc because it's c++ compilator. And I use C version of GTK.
    That still leaves the question unanswered: is this C or C++? If you really want to code in C, then you should also test using C specific constructs, compiling with a C compiler (in this case this means configuring your compiler to compile the source code as C).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    I code in C, I know that I use C++ compiler.

    I will go deeper into GTK documentations about main lopp of it and see what I can find.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you're coding in C, why not use a C compiler? There's one in VS, somewhere.

    Anyway, another thing I thought of as you were looking at your code, related to the data=NULL line above: you are passing your pointer to the function by value. So, based on my limited understanding of how the gtk library works, the registration you do in the g_signal_connect function would then be based on the value Wsk holds at the instant you register the callback. If you do the malloc later, then that doesn't help, since that won't change the value that the callback function has. In this case, you would actually want
    Code:
    void Wyjscie(GtkWindow* window, GdkEvent* event, StrukturaDrawing** data)
    {
    	gtk_main_quit();
    	free(*data);
    	*data = NULL;
    }
    and pass in &Wsk to your g_connect_signal thing.

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    I must say that I tried that before and with that I get:
    0xC0000005: Access violation reading location 0x00000001.

    EDIT: While debugging I have looked at data structure at the point where it crashed and members of structure have that error:
    The first one has: PoziomaLinijka CXX0017: Error: symbol "" not found
    And later the whole rest have: PionowaLinijka CXX0030: Error: expression cannot be evaluated
    Last edited by darekg11; 01-09-2011 at 02:39 PM.

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    It's fixed. Strucutre of callback function was wrong, now it runs without any violations but I have one question:

    Code:
    void Wyjscie(GtkObject* window, StrukturaDrawing* data)
    {
    	gtk_main_quit();
    	free(data);
    	data = NULL;
    }
    Data won't be set to NULL right? Adress will be set to NULL, that function should look like:
    Code:
    void Wyjscie(GtkObject* window, StrukturaDrawing** data)
    {
    	gtk_main_quit();
    	free(*data);
    	*data = NULL;
    }
    And I pass to function &Wsk instead of Wsk, am I right?

  11. #11
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    To tell VC you want it to compile like a C compiler, change the extension of the file to .c and you'll get an (almost) proper C compiler.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by darekg11 View Post
    It's fixed. Strucutre of callback function was wrong, now it runs without any violations but I have one question:

    Code:
    void Wyjscie(GtkObject* window, StrukturaDrawing* data)
    {
    	gtk_main_quit();
    	free(data);
    	data = NULL;
    }
    Data won't be set to NULL right? Adress will be set to NULL, that function should look like:
    Code:
    void Wyjscie(GtkObject* window, StrukturaDrawing** data)
    {
    	gtk_main_quit();
    	free(*data);
    	*data = NULL;
    }
    And I pass to function &Wsk instead of Wsk, am I right?
    That is how you get changes to appear in the calling function as well.

  13. #13
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Yeah so it's right way to do it?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    I guess I will leave it as it was before because when doing it with this new function:

    Code:
    void Wyjscie(GtkObject* window, StrukturaDrawing** data)
    {
    	gtk_main_quit();
    	free(*data);
    	*data = NULL;
    }
    I get again acces violation and when I do it in old way I don't get any warnings or errors.

    Anyway thanks for help.

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