Thread: implement gcc's cleanup attribute for msvc

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    14

    implement gcc's cleanup attribute for msvc

    hello.

    how can one implement gcc's cleanup attribute for msvc?

    how destructors are implemented on top of msvc?
    In addition I want the behavior to be identical to the cleanup attribute so atexit is not an option.



  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    There are no destructors in C. You are on the wrong forum.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    14

    implement gcc's cleanup attribute for msvc

    Quote Originally Posted by claudiu View Post
    There are no destructors in C. You are on the wrong forum.
    hello.

    I'm well aware of it. however in the extensions provided to c by gcc there is a cleanup attribute attribute which may be used to implement destructors on top of it as well as an attribute destructor.

    my goal is to be able to implement the cleanup attribute functionality for msvc compiler.

    thanks any way.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    cleanup (cleanup_function)
    The cleanup attribute runs a function when the variable goes out of scope. This attribute can only be applied to auto function scope variables; it may not be applied to parameters or variables with static storage duration. The function must take one parameter, a pointer to a type compatible with the variable. The return value of the function (if any) is ignored.

    If -fexceptions is enabled, then cleanup_function will be run during the stack unwinding that happens during the processing of the exception. Note that the cleanup attribute does not allow the exception to be caught, only to perform an action. It is undefined what happens if cleanup_function does not return normally.
    You would probably get a lot further if you just accepted that C was C and no amount of compiler specific hackery would make it taste like C++.

    If you want this behaviour, you're better off writing C++ (or learning to write C properly). Attributes like this only lead to "Oh no, my safety net has gone" when you switch from one compiler to another.

    Either stick with GCC, or write portable code to begin with.

    You're even more hamstrung with MS, since the Microsoft 'C' compiler will never be updated from C89 standard (we've had C99 and C11 since then).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    14
    alright. but again is there any mechanism availiable in any of msvc versions that can be used in order to implement the cleanup attribute in c.
    I'm not intrested in portablity here, nor in using c++ or gcc.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Salem View Post
    Either stick with GCC, or write portable code to begin with.
    I definitely agree with you here. I'm personally an advocate of writing portable code, and not using any compiler specific extensions or even pragmas at all, where possible.

    It is also usually easier to write portable code, than it is to work out how to map particular compiler-specific attributes or extensions to other compilers. Generally, I only use compiler specific attributes or extensions (and look askance at pragmas) unless I am happy to be locked into one particular compiler for life. Which, generally, I am not.


    Quote Originally Posted by Salem View Post
    You're even more hamstrung with MS, since the Microsoft 'C' compiler will never be updated from C89 standard (we've had C99 and C11 since then).
    Never say never, although I agree it probably won't happen for a while.

    Herb Sutter, who is lead architect of the Visual Studio development team, happens to also be chair of ISO (or ANSI's) standardisation committee for C++. It's hardly surprising that he might encourage Microsoft compilers to lean towards C++ rather than C.

    It's a matter of opinion whether that is a good thing or not (with the dividing line being whether one prefers C or C++). Certainly, Microsoft have gone ahead in leaps and bounds with improving the quality and standard compliance of their C++ compiler offering, although support of C has languished.

    There are published suggestions of internal dissent with Microsoft over the lack of support of C. So, it remains to be seen if that will change.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Quote Originally Posted by cprogrammer1122 View Post
    alright. but again is there any mechanism availiable in any of msvc versions that can be used in order to implement the cleanup attribute in c.
    I'm not intrested in portablity here, nor in using c++ or gcc.
    Structured Exception Handling? The __try/__finally keywords can force clean-up before exiting a block of code

    Structured Exception Handling Basics - General Programming - Articles - Articles - GameDev.net

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    14

    implement gcc's cleanup attribute for msvc

    Quote Originally Posted by Fordy View Post
    Structured Exception Handling? The __try/__finally keywords can force clean-up before exiting a block of code

    Structured Exception Handling Basics - General Programming - Articles - Articles - GameDev.net
    I've already came across exceptions in msvc c ,however, I can't imagine a use of it for my purpose. maybee, you might provide me with some insights.

    thanks.

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Quote Originally Posted by cprogrammer1122 View Post
    I've already came across exceptions in msvc c ,however, I can't imagine a use of it for my purpose. maybee, you might provide me with some insights.

    thanks.
    Or you could read the link?

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    14
    I've read the link, however, it doesn't provide the answer for my problem.
    for example:
    Code:
    int main()
    {
       char *ptr;
        ptr = malloc(10);
        myFunc(&ptr);
        strcpy(ptr,"string");
        printf("%s\n",ptr);
        return 0;
    }


    Assume that myFunc(char **ptr) is a function you can implement as you wish.
    But the main code is fixed and you can't change it at all.
    here the only way to go is using the cleanup attribute.
    the case I've presented is the case I'm trying to solve.

    any suggestions?

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    you would have to do some stack hackery where myFunc saves the return address from the stack, and replaces it with a jump to your cleanup function, where myfunc does what you want then executes a return to the original address. will be tricky to implement in a general way.

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    the case I've presented is the case I'm trying to solve
    The case you've presented here can't be solved.

    The GCC "cleanup" attribute certainly can't help. The `ptr' variable in `main' would have to be marked with the cleanup attribute; marking that `ptr' variable with the "cleanup" attribute would be changing the code and require a recompile. If you can change the code, you may as well make a proper portable fix.

    If you are truly not allowed to change buggy code you'll have to live with a bug. You may be able to bandage over it, but that's probably the absolute best that you can do, and every situation will probably require you to use a different kind of bandage.

    That said, let's face the reality here. You've constructed a situation where you believe you must have a feature to write correct code. The "cleanup" attribute is extremely useful, but it isn't required to write correct code.

    If you want scoped destruction you are limited to: using C++, using vendor specific extensions, or writing an awful mess of code with `setjmp' and `longjmp'.

    Soma

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > But the main code is fixed and you can't change it at all.
    Mmm - okay....

    > here the only way to go is using the cleanup attribute.
    But you're already changing the code if you plan to try something like
    char __attribute__ ((cleanup (myFunc))) *ptr;

    Not to mention actually implementing myFunc() as well.

    So why can't you change it to just call free() at the end, like any normal person would?

    TBH, it seems like you're searching for some overly elaborate solution that might take months to find, whereas just editing the code to fix the problem in whatever small number of places memory is being leaked (which would take a couple of days).

    Remember, you have to FIND these variables which need cleanup to begin with (this will take all the time), and then decide what to do about them. Blindly adding "cleanup" to everything you find would add as many bugs as you remove (like cleaning up a pointer that was actually returned or copied elsewhere). Having found the source of a leak, fixing it is usually pretty trivial - if you need to free it in the SAME function that allocated it.

    Leaks that occur because memory is allocated in one function, and (supposedly) freed in another function will not be fixed by this cleanup magic.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Also posted in devshed forum.

  15. #15
    Registered User
    Join Date
    May 2012
    Posts
    14
    tank you for the quick replys.
    some further clarifications:

    1. when I've written I can't change the code, I only meant I can't change the main function.
    2. this is a riddle\question I was asked ,so I must follow the rules.
    3. what I seek is a direction for the answer. I already know that the cleanup attribute solves the problem, therefore, my task is to
    implement an equivalent for msvc.
    4. I can't see how setjmp and longjmp can help since the memory allocated is still being used after the call to myFunc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cleanup of the application...
    By Petike in forum Windows Programming
    Replies: 1
    Last Post: 08-16-2008, 05:23 PM
  2. Cleanup Sequence
    By Hunter2 in forum Windows Programming
    Replies: 2
    Last Post: 07-20-2008, 08:33 PM
  3. Replies: 9
    Last Post: 11-12-2007, 03:29 PM
  4. Disk Cleanup Software
    By BestGameMovie in forum Tech Board
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  5. Order of cleanup
    By Hunter2 in forum Windows Programming
    Replies: 4
    Last Post: 01-08-2004, 09:19 AM