Std::vector Pass DLL parameter exception-length overrun

Source: Internet
Author: User

(Owed by: Spring Night rain Http://blog.csdn.net/chunyexiyu reprint please indicate the source)

If you call a function in a DLL and use a vector to pass a parameter, error "vector size exception, length overrun " code location " _throw(length_error,"vector<t> too Long") "

Then you may have encountered vector debug version and release version mismatch problem.

In the case of non-conditional compilation, if :

1. The calling program uses the debug version of the vector

2. use release version vector in Dll

Then pass the empty vector, this problem will occur ( of course, the transfer of non-empty vectors should also be problematic )

The problem arises because :

The debug version of the STL in VS is different from the release version vector in memory allocation, resulting in inconsistent data structures on both sides. A memory overflow-related issue occurred while assigning a value. It is mistaken for the vector length overrun problem.

A detailed reason for the problem can be seen by looking at the code of vector in VS :

1. Vector inherits from _vector_val

typedef _vector_val<_ty, _ax> _mybase;

Explicit vector (const _alloc& _al)

: _mybase (_al)

2. _vector_val inherits from _container_base_aux_alloc

_vector_val (_alloc _al = _alloc ())

: _container_base_aux_alloc<_alloc> (_al), _alval (_al)

3. Follow-up See, at Debug and release, the separate branches are gone

#if!_has_iterator_debugging && (defined (_DEBUG) | | _secure_scl)

//We have an Aux object.

#define _CONTAINER_BASE_AUX_ALLOC _container_base_aux_alloc_real

#else

//We have an Aux object.

#define _CONTAINER_BASE_AUX_ALLOC _container_base_aux_alloc_empty

#endif

in _container_base_aux_alloc_real ( inherited from _container_base_aux), there is a variable

_aux_cont * _myownedaux;

But there is no such variable in _container_base_aux_alloc_empty ( inherited from _container_base).

To view the macro definition:

#if!defined (_has_iterator_debugging)

#if defined (_DEBUG)

#define _HAS_ITERATOR_DEBUGGING 1/* for range checks, etc. */

#else

#define _has_iterator_debugging 0

#endif/* Defined (_DEBUG) */

#else

#if!defined (_DEBUG) && _has_iterator_debugging! = 0

#include <crtwrn.h>

#pragma _crt_warning (_no_iterator_debugging)

#undef _has_iterator_debugging

#define _has_iterator_debugging 0

#endif

#endif/*!defined (_has_iterator_debugging) */

#if!defined (_SECURE_SCL)

#define _SECURE_SCL 1

#endif

When I debug this side I see:

Release version went the branch _container_base_aux_alloc_real.

DEUBG version went the branch _container_base_aux_alloc_empty.

As can be seen from the above, this is actually very much related to the STL implementation in VS, and the correlation of the debug mechanism in VS, which makes the debug/release version of the data structure is different.

(Owed by: Spring Night rain Http://blog.csdn.net/chunyexiyu reprint please indicate the source)

Std::vector Pass DLL parameter exception-length overrun

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.