Like Tree2Likes
  • 1 Post By Caligulaminus
  • 1 Post By Caligulaminus

memory exception using _aligned_malloc and _aligned_free

This is a discussion on memory exception using _aligned_malloc and _aligned_free within the C++ Programming forums, part of the General Programming Boards category; Code: //i am allocating cache-aligned buffers using: Matrix::Matrix(const unsigned int _rows, const unsigned int _columns): //... data((double*)_aligned_malloc(sizeof(double)*(rows*columns+1),32)) //... {} //and ...

  1. #1
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839

    memory exception using _aligned_malloc and _aligned_free

    Code:
    //i am allocating cache-aligned buffers using:
    Matrix::Matrix(const unsigned int _rows, const unsigned int _columns):
    //...
      data((double*)_aligned_malloc(sizeof(double)*(rows*columns+1),32))
    //...
    {}
    //and freeing them like so:
    Matrix::~Matrix()
    {
        if(data)_aligned_free(data);
        data=0;
    }
    data is aligned on 0x20 boundaries, just like i wanted, but problem is, i'm getting a heap corruption exception inside _aligned_free at location 0x10 LESS than where my buffer starts.

    what gives? am i doing something wrong or is this something evil about aligned malloc/free?

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    25
    I'd say the error is somewhere else in your code.
    Some out of bounds memory access probably.
    m37h0d likes this.

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    don't think so. here's the stack trace:

    Code:
    >	msvcr100d.dll!_free_dbg_nolock(void * pUserData=0x031e6770, int nBlockUse=1)  Line 1376 + 0x3b bytes	C++
     	msvcr100d.dll!_free_dbg(void * pUserData=0x031e6770, int nBlockUse=1)  Line 1265 + 0xd bytes	C++
     	msvcr100d.dll!_aligned_free_dbg(void * memblock=0x031e6780)  Line 3129 + 0xd bytes	C++
     	msvcr100d.dll!_aligned_free(void * memblock=0x031e6780)  Line 3087 + 0x9 bytes	C++
     	Math.NET.dll!LS::Math::Native::Matrix::~Matrix()  Line 60 + 0x16 bytes	C++
     	Math.NET.dll!LS::Math::Native::Matrix::QRFactorize(LS::Math::Native::Matrix & Q={...}, LS::Math::Native::Matrix & R={...})  Line 535 + 0xf bytes	C++

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    25
    Oh I do think so.
    _aligned_free() (or rather _free_dbg_nolock()) only finds the problem and throws an exception. The actual memory corruption occurred somewhere else before.

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    yep you got it. i had ++ when i should have had --. odd the stack track took me to that location instead of where the out-of-bounds access was actually occurring...

    thanks

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    25
    Not odd.
    If the memory right before/after your array belongs to your process you can write there unhindered(is that correct english?). My guess is the debug version of your runtime lib marks the area before/after your allocated chunk of memory with some magic number to achieve exactly what happened. Uncover hidden out of boundary writes.
    iMalc likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL and C++ - Interleaved VBO Memory Exception
    By MyNameIsNathan in forum Game Programming
    Replies: 6
    Last Post: 01-03-2012, 03:30 PM
  2. exception handling ( memory allocation)
    By manzoor in forum C++ Programming
    Replies: 5
    Last Post: 09-15-2008, 06:43 AM
  3. exception handling, function call and memory
    By George2 in forum C++ Programming
    Replies: 21
    Last Post: 01-30-2008, 07:00 AM
  4. Handle C++ exception and structured exception together
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2008, 08:21 PM
  5. memory allocation exception
    By Micko in forum C++ Programming
    Replies: 8
    Last Post: 09-19-2004, 01:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21