×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Registered User
    Join Date
    May 2006
    Location
    Budapest
    Posts
    21

    Open C and cleanup safety?

    Hello,

    I'm intermixing Symbian C++ and Open C code and have some concerns regarding using cleanup stack with Open C types. What is the official statement on using Open C types and cleanup stack? Is it allowed to put Open C types to the cleanup stack (using TCleanupItem)? Or does Open C has some sort of built-in way to prevent memory leaks with its built-in types? It's noted in the SDK that "Open C APIs assume that a cleanup stack is created and there is a top-level TRAP for the current thread".

    For instance, lets see this code example which uses both Open C and Symbian C++ calls:

    Code:
    GString* string = g_string_new("something");
    DoSomeThingThatCanLeaveL(); // Symbian C++ method call, can leave
    g_string_free(string, TRUE);
    What happens if the called Symbian method leaves? We created a GString on the heap and didn't push it onto the cleanup stack. Do we get a memory leak or Open C did something in the background which ensured that it cannot happen?

    Can I put the GString to the cleanup stack (by means of a TCleanupItem)?
    Imre Kelényi

  2. #2
    Regular Contributor
    Join Date
    May 2007
    Posts
    463

    Re: Open C and cleanup safety?

    Yes, you get a memory leak.

    C, of course, has no such concept as exceptions. The nearest thing, non-local gotos, are highly discouraged. This means C libraries have no cleanup mechanism that applies to exceptions.

    Really, you shouldn't write code that could possibly generate exceptions in the middle of Open C code.

    You could, however, easily write cleanup items for the g_free family of operations if you really wanted to.
    Get Resolvr - The Zeroconf framework for Symbian OS free today. Make your IP networking applications fun and easy to use. http://www.novelinteractions.com/resolvr/
    Proud to be the only autorickshaw owner in Cambridge - http://blog.novelinteractions.com/images/tuktuk.jpg

  3. #3
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,673

    Re: Open C and cleanup safety?

    Imre I do not really understand what are you expecting from this thread. You, CDavies, and me are all here in Aalborg, so does Mr. Penttinen. If there was a really nice "official" answer, he would have told it to you on your question.

  4. #4
    Registered User
    Join Date
    May 2006
    Location
    Budapest
    Posts
    21

    Re: Open C and cleanup safety?

    Quote Originally Posted by wizard_hu_ View Post
    Imre I do not really understand what are you expecting from this thread. You, CDavies, and me are all here in Aalborg, so does Mr. Penttinen. If there was a really nice "official" answer, he would have told it to you on your question.
    Do you think that we should start using this forum as a channel for exchanging our personal messages? (good luck on acquiring the poster of the month title ) Anyway, I just wonder why these things are not covered in the SDK documentation since they don't seem as trivial. Thanks for the answer Cdavies.
    Imre Kelényi

  5. #5
    Registered User
    Join Date
    Aug 2003
    Location
    Oulu, Finland
    Posts
    1,122

    Re: Open C and cleanup safety?

    Quote Originally Posted by kenley View Post
    Can I put the GString to the cleanup stack (by means of a TCleanupItem)?
    Yes, you can put practically any items on the cleanup stack. Example:
    Code:
    GString* string = g_string_new("something");
    CleanupStack::PushL(TCleanupItem(g_string_free, string));
    // ... some L calls
    CleanupStack::PopAndDestroy(string);
    Note that the "free" operation needs to be a TCleanupOperation, i.e. taking a single void* parameter and returning nothing.

    Lauri

  6. #6
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,673

    Re: Open C and cleanup safety?

    Laa-laa: it is not that easy
    Quote Originally Posted by gstring.h
    IMPORT_C gchar* g_string_free (GString *string, gboolean free_segment);
    Code:
    void g_s_f(void *s)
    {
        g_string_free((GString*)s,TRUE);
    }
    
    CleanupStack::PushL(TCleanupItem(g_s_f,somegstring));
    is better. But it is hard to call this solution nice actually.
    Last edited by wizard_hu_; 2007-09-02 at 18:08. Reason: Completely rewritten

  7. #7
    Registered User
    Join Date
    Aug 2003
    Location
    Oulu, Finland
    Posts
    1,122

    Re: Open C and cleanup safety?

    wizard_hu: Yes, it's not always straightforward.

    A solution like that soon starts to look like wrapping items to be cleaned up in CBase-derived objects and freeing the resources in the destructor.

    If you know what you're doing and what the compiler is doing, you can cast the "free" function to a TCleanupOperation and deliberately remove some type safety. This assumes the "free" function takes in a pointer to whatever. It is a relatively safe assumption since almost all "destructors" in C take in just the pointer to the memory block/struct/whatever.

    To make things a little simpler, it could be wrapped in a preprocessor macro:
    Code:
    #define CleanupOpPushL(op,ptr) \
      CleanupStack::PushL(TCleanupItem(reinterpret_cast<TCleanupOperation>(op), ptr))
    Lauri

  8. #8
    Registered User
    Join Date
    May 2006
    Location
    Budapest
    Posts
    21

    Re: Open C and cleanup safety?

    Quote Originally Posted by laa-laa View Post
    If you know what you're doing and what the compiler is doing, you can cast the "free" function to a TCleanupOperation and deliberately remove some type safety. This assumes the "free" function takes in a pointer to whatever. It is a relatively safe assumption since almost all "destructors" in C take in just the pointer to the memory block/struct/whatever.
    Lauri
    Although this approach will definitely not work with glib for almost all of its deallocation functions take two parameters...

    Code:
    guint8* g_byte_array_free (GByteArray *array, gboolean free_segment);
    gchar* g_string_free (GString *string, gboolean free_segment);
    etc.
    Imre Kelényi

  9. #9
    Regular Contributor
    Join Date
    May 2007
    Posts
    463

    Re: Open C and cleanup safety?

    If you want to do this, generally you'll write helper functions like the built in CleanupClosePushL/CleanupResetPushL that push the pointer to the object and the cleanup method on to the stack for you.
    Get Resolvr - The Zeroconf framework for Symbian OS free today. Make your IP networking applications fun and easy to use. http://www.novelinteractions.com/resolvr/
    Proud to be the only autorickshaw owner in Cambridge - http://blog.novelinteractions.com/images/tuktuk.jpg

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    12

    Re: Open C and cleanup safety?

    Hi, i have same trouble about memory leak when use open C/C++ when code Symbian app. Are there any mechanism to manage allocation fail when use open C/C++ when make Symbian application.
    thks for advice

Similar Threads

  1. Replies: 7
    Last Post: 2007-02-18, 18:25
  2. the usage of cleanup Stack
    By qiuhuimin in forum Symbian
    Replies: 2
    Last Post: 2004-08-18, 02:22
  3. cleanup stack - function that leave
    By fcobsb in forum Symbian
    Replies: 0
    Last Post: 2004-07-23, 22:30
  4. Text Query Dialog Cleanup
    By koayst in forum Symbian
    Replies: 1
    Last Post: 2003-05-02, 09:06
  5. Series 60 documentation error regarding R classes, cleanup stack
    By rtillitt in forum Symbian Tools & SDKs
    Replies: 1
    Last Post: 2002-12-06, 02:08

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×