You should be able to do what you are trying to do but have a bug or design flaw.
It is best to keep all memory in a single structure owned by the same owner. This line: t.Value.waves = libpointer('doublePtr',matlabstruct.waves); places a MATLAB owned and managed memory object inside t that is owned and allocated by your DLL. You free t then clear it in MATLAB but MATLAB does not know it is deallocated and goes to free waves causing a crash.
Without seeing the definition of the structure (in c) you are returning and a bit more info on what is expected I may not be able to give a complete response but you could start by trying this sequence however I have doubts that you want to make the above pointer assignment to waves.
t.Value.waves=[]; or = libpointer;
t.Value.range=[];
calllib('DLL32', 'DeallocateInput', t);
clear t;
Best Answer