CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2009
    Posts
    12

    Sinhronisation of allocation and freeing of memory

    I've got a problem with sinhronisation of a function which is called from several threads. The function do no use static variables. And there is no connection variables between threads. In debuger the aplication crashes on new or delete operators inside the function. In internet I've read that these operators are already synhronized. Please help me to find solution of the problem.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Sinhronisation of allocation and freeing of memory

    Something you did elsewhere probably corrupted the heap.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Sinhronisation of allocation and freeing of memory

    If the functions access data other than local variables within the function, then access to this data must be synchronized. This data could be global data, or data within a dll, or any other form of shared data.

  4. #4
    Join Date
    Feb 2009
    Posts
    12

    Re: Sinhronisation of allocation and freeing of memory

    Quote Originally Posted by Arjay View Post
    If the functions access data other than local variables within the function, then access to this data must be synchronized. This data could be global data, or data within a dll, or any other form of shared data.
    There is on shared variables, functions working separately. The heap allocation data is not synchronized. But as I read in Internet - operator new and delete should be sinhronized.

  5. #5
    Join Date
    Nov 2006
    Posts
    1,611

    Re: Sinhronisation of allocation and freeing of memory

    And there is no connection variables between threads. In debuger the aplication crashes on new or delete operators inside the function.
    What is the OS and the compiler?

    Are you certain you've selected the appropriate multi-threaded runtime library? (I'd assume you'd have trouble launching threads otherwise, but I suppose it is still possible).

    You're correct that new/delete are synchronized (one might go so far as to say serialized).

    If you're certain without doubt that not one single byte of data is shared between threads, there's not problem running without further synchronization against mutexes/critical sections/etc.

    Does you build crash on the very first new?

    Care to post any code for more specific help than generalizations?
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  6. #6
    Join Date
    Feb 2009
    Posts
    12

    Re: Sinhronisation of allocation and freeing of memory

    Quote Originally Posted by JVene View Post
    What is the OS and the compiler?
    I uses VC++ 6.0 on Windows XP x32

    Quote Originally Posted by JVene View Post
    Are you certain you've selected the appropriate multi-threaded runtime library? (I'd assume you'd have trouble launching threads otherwise, but I suppose it is still possible).
    I have defined in settings page "Code generation" "Use run-time library" as "Debug Multithreaded DLL" for debug and for realize "Multithreaded DLL". Do you mean this?

    Quote Originally Posted by JVene View Post
    Does you build crash on the very first new?
    No, I have crash in many places of allocation or deallocation of memory.

    Quote Originally Posted by JVene View Post
    Care to post any code for more specific help than generalizations?
    I can't post any part of sources, because I develop comercial product.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Sinhronisation of allocation and freeing of memory

    Without posting code, it doesn't leave much for us to go on.

    Try looking for unitialized variables.

  8. #8
    Join Date
    Jul 2005
    Posts
    185

    Re: Sinhronisation of allocation and freeing of memory

    Or, check for buffer overrun - maybe you do something like this:

    char *szName = new char[100];
    sprintf(szName, ....);

    And the sprintf() function writes more than 100 chars. That's also why you get random crashes.

  9. #9
    Join Date
    Feb 2009
    Posts
    12

    Re: Sinhronisation of allocation and freeing of memory

    I found the problem, there was "static" variables inside functions.

  10. #10
    Join Date
    Nov 2006
    Posts
    1,611

    Re: Sinhronisation of allocation and freeing of memory

    Ah, glad you found that.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured