xricht17 / twpp

TWAIN framework for C++11. Makes development of applications and data sources much easier.
MIT License
39 stars 17 forks source link

Memory Access Violation #36

Closed Awesome-Meng closed 1 year ago

Awesome-Meng commented 1 year ago

I use twpp as the base for my DS. For one specific App (QuickVision v6.04) periodically I will receive Memory Access Violation Exceptions leading to the program crashing.

The crash occurs within my custom definition of:

Result imageNativeXferGet(const Identity& origin, ImageNativeXfer& data)

Specifically at the assignment operator call on data to the new size version:

data = ImageNativeXfer(imgSize);
     ^
     Here

After a bit of digging, the likely culprit is the attempt to free data's m_handle. https://github.com/xricht17/twpp/blob/6b97c77967265c7dbc54ba2c7b1872439d4ab15d/twpp/memoryops.hpp#L420-L428

Where in other applications m_handle is null, this App appears to provide m_handle as not null. Furthermore, GlobalMemFuncs<void>::doNotFreeHandle which should be set the the same value is not equal at the time of the call to free() and therefore performs the invalid free operation.

Is it possible that doNotFreeHandle is not correctly set? I performed some logged and found calls to DoNotFreeHandle::~DoNotFreeHandle() happened prior to the call to free(). However, I could be wrong and this could be entirely the App's fault for incorrectly setting the data. Any assistance in this would be greatly appreciated. I'm running on Windows 10, building with Clang++ (version 15.0.6).

xricht17 commented 1 year ago

Hi. I guess native image transfer will need the same treatment as capabilities.

Could you try the following patch to see if it works? fix_ds_native_transfer.patch

Awesome-Meng commented 1 year ago

Hi, thank you for the advice. You're right, that fixed it. I just had to make one small change:

Twpp::Handle& handle = *static_cast<Twpp::Handle*>(data);

Instead, of Detail::Handle.

xricht17 commented 1 year ago

Thanks, should be fixed now.