Andrew Nguyen
Andrew Nguyen

Reputation: 62

Debug Assertion Failed error when accessing function in DLL

I'm currently learning how to create a C++ library to be referenced in other projects, and I am running into an issue with a "Debug Assertion Failed" error: is_block_type_valid(header-> _block_use). I followed the walkthrough shown here: Create and use your own Dynamic Link Library. Oddly, I am getting the expected answer if I just ignore the error.

My DLL currently only has one function:

cpp:

int calculate_crc(std::string msg)
{
    std::vector<std::string> msg_vector = [](std::string& msg1) {
        std::string next;
        std::vector<std::string> result;

        // for each char in string
        for (std::string::const_iterator it = msg1.begin(); it != msg1.end(); it++)
        {
            // if we hit a terminal char
            if (*it == ' ')
            {
                if (!next.empty())
                {
                    // add them to the result vector
                    result.push_back(next);
                    next.clear();
                }
            }
            else
            {
                next += *it;
            }
        }
        if (!next.empty())
        {
            result.push_back(next);
        }

        return result;
    } (msg);

    int crcReg = 0xFFFF;

    // iterate through each element in msgVector
    for (auto&& element : msg_vector)
    {
        // step 2: xor operation performed on byte of msg and CRC register
        crcReg ^= [](std::string hex) {
            std::map<char, int> map;

            map['0'] = 0;
            map['1'] = 1;
            map['2'] = 2;
            map['3'] = 3;
            map['4'] = 4;
            map['5'] = 5;
            map['6'] = 6;
            map['7'] = 7;
            map['8'] = 8;
            map['9'] = 9;
            map['a'] = 10;
            map['b'] = 11;
            map['c'] = 12;
            map['d'] = 13;
            map['e'] = 14;
            map['f'] = 15;

            return map[hex[1]] + (map[hex[0]] * 16);
        } (element);

        // step 3-5 are repeated until 8 bit shifts
        for (int i = 0; i < 8; i++)
        {
            int crcCopy = crcReg;
            crcReg >>= 1;

            if ((crcCopy & 1) == 0)
                continue;
            else
                crcReg ^= 0xA001;
        }
    }

    return crcReg;
}

h:

#pragma once

#ifdef OMRONLIBRARY_EXPORTS
#define OMRONLIBRARY_API __declspec(dllexport)
#else
#define OMRONLIBRARY_API __declspec(dllimport)
#endif

#include <iostream>

extern "C" OMRONLIBRARY_API int calculate_crc(const std::string msg);

Upvotes: 0

Views: 875

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595422

std::string is not a safe type to use in a DLL function parameter. Non-POD types should never be passed over a DLL boundary, unless they are type-erased (such as by using a void* pointer) and are only ever accessed directly by code on one side of the boundary and not the other side.

Assuming the caller is even using C++ at all (C-style DLLs can be used in non-C/C++ languages), it may be using a different std::string implementation. Or it may be using a different C++ compiler, or a different version of the same C++ compiler, or even just different settings for alignment, optimizations, etc. And even if all of that matches the DLL, it will likely be using a different instance of the memory manager that the DLL uses for its std::string implementation.

If you want to pass a string to a DLL function safely, use a C-style char* string instead. You can use std::string inside the DLL, if you want to, eg:

int calculate_crc(const char* msg)
{
    use msg as-is ...

    or

    std::string s_msg = msg;
    use s_msg as needed ...
}
extern "C" OMRONLIBRARY_API int calculate_crc(const char* msg);

Upvotes: 1

Related Questions