JoshClose / CsvHelper

Library to help reading and writing CSV files
http://joshclose.github.io/CsvHelper/
Other
4.63k stars 1.05k forks source link

Getting problems with release build of my app using COM #2244

Open ajtruckle opened 3 months ago

ajtruckle commented 3 months ago

Describe the bug I have decided to re-work the ticket and simply my example data.

My CSV functions used to work but now I am having critical problems. When I run my Visual C++ project in Debug mode then the DLL works ok. But, when I run it in Release mode all my CSV functions complain now.

I do not know when this broke down nor how to fix it. Spent a day and a half on it so far.

To Reproduce Here is a sample C# method:

public void ParseCsvFile(string fileToParse)
{
    List<string> listTokens = new List<string>();

    try
    {
        using (var streamReader = new StreamReader(fileToParse, Encoding.UTF8))
        {
            using (var reader = new CsvReader(streamReader,
                new CsvConfiguration(CultureInfo.CurrentCulture) { Delimiter = ",", Encoding = Encoding.UTF8 }))
            {
                while (reader.Read())
                {
                    for (int column = 0; column < reader.ColumnCount; column++)
                    {
                        listTokens.Add(reader.GetField(column));
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        SimpleLog.Log(ex);
    }
}

This method is exposed to the associated interface class. And, here is the C++ wrapper:

bool CMSATools::ParseCsvFile(const CString fileToParse)
{
    if (m_pInterface != nullptr)
    {
        // CComBSTR bstrTextToParse = textToParse.AllocSysString();
        const auto hr = m_pInterface->ParseCsvFile(fileToParse.AllocSysString());
        if (SUCCEEDED(hr))
            AfxMessageBox(L"Read");
        //  ConvertSAFEARRAY<BSTR, CStringArray>(saTokens, listTokens);
        else
            throw_if_fail(hr);
    }

    return true;
}

Expected behavior I expect this to parse the file correctly and display the Read message.

image

Additional context I have tried downgrading the Csv NuGet packages. No joy. At the moment this is application breaking behaviour.

What I can tell you is that if I comment out all the CsvHelper code in my function then my function executes. So it was at the moment it went to do the Csv stuff that it all goes wrong without any exceptions in C# being thrown.

Is this a CsvHelper issue? Something that doesn't cater for the context I am using (eg. DLL COM Interop)?

ajtruckle commented 3 months ago

Morning! I am currently working on a workaround. I am implementing a .NET8 console application and slowly transferring functionality related to CsvHelper into it.

Running the console app in release build (AnyCPU) is working. So it will be straightforward to adjust my parent C++ application to spawn the console application with relevant command line switches.

Whilst I am happy to make these changes and adjustments, I am still curious as to the root cause of my DLL. 🤔