Q:

Heap allocation error within static library

I have a working Visual Studio project.
I've created a static library with the files of the original project (except main.cpp), also
I've created a "tester" project (with the static lib linked to it) with only a main.cpp file from the original project.
Both compiles without any relevant error.
And tester runs appropriately.
But! At testing the "tester" I am getting a heap allocation error at a (not the first)
new[] operator invoked in a constructor implemented in the library.
That line working fine in the original project without any error.
The "little" version of the code:
//the staticlib
void test() {
 manager* m = new manager;
 m->open();
}
//....
class manager {
public:
open() {

        PRAWINPUTDEVICELIST lDevList;
        UINT lDevCount;
        GetRawInputDeviceList(NULL, &lDevCount, sizeof(RAWINPUTDEVICELIST));
        lDevList = (PRAWINPUTDEVICELIST) malloc(sizeof(RAWINPUTDEVICELIST)*lDevCount);
        GetRawInputDeviceList(lDevList, &lDevCount, sizeof(RAWINPUTDEVICELIST));

        if(lDevCount) {

            for(UINT i = 0; i < lDevCount; i++) {

                HIDP_CAPS mCaps;
                PHIDP_BUTTON_CAPS mButtonCaps;
                PHIDP_VALUE_CAPS mValueCaps;

                UINT size;
                GetRawInputDeviceInfo(lDevList[i].hDevice, RIDI_DEVICENAME, NULL, &size);
                char* name = new char[size+1];
            //just to be sure
            memset(name, 0, size+1);
            //surely sure
            name[size] = '\0';
                GetRawInputDeviceInfo(lDevList[i].hDevice, RIDI_DEVICENAME, name, &size);

                HANDLE lDev = CreateFile((LPCWSTR)name, 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
                                     NULL, OPEN_EXISTING, 0, NULL);;

                PHIDP_PREPARSED_DATA lPrep;
                HidD_GetPreparsedData(lDev, &lPrep);
                HidP_GetCaps(lPrep, &mCaps);

                if(mCaps.NumberInputButtonCaps) {
                                    //crash is here below
                                    //mCaps.NumberInputButtonCaps ~1
                    mButtonCaps = new HIDP_BUTTON_CAPS[mCaps.NumberInputButtonCaps];
                    HidP_GetButtonCaps(HidP_Input, mButtonCaps, &mCaps.NumberInputButtonCaps, lPrep);
                }

                if(mCaps.NumberInputValueCaps) {
                                    //if the first "crash-line" is commented, then
                                    //the crash is here
                    mValueCaps = new HIDP_VALUE_CAPS[mCaps.NumberInputValueCaps];
                    HidP_GetValueCaps(HidP_Input, mValueCaps, &mCaps.NumberInputValueCaps, lPrep);
                }

                CloseHandle(lDev);      
            }

        } 
    }
};

//the app

test();

Where I am wrong? Is it a typical novice commission I am not afraid of? 
Sorry for my English, and thanks ahead for your time!

A:

The error is that you should be allocating wide chars when you call GetRawInputDeviceInfo. From the manual

RIDI_DEVICENAME 0x20000007
  pData points to a string that contains the device name.
For this uiCommand only, the value in pcbSize is the character count
  (not the byte count).

In other words you should write
wchar_t* name = new wchar_t[size];
GetRawInputDeviceInfo(lDevList[i].hDevice, RIDI_DEVICENAME, name, &size);

Just from reading the manual, I have no actual experience with this API, but it seems a likely explanation.

