Merge two SAFEARRAY's - SafeArrayPutElement memory access violation - com

I am getting some memory access violation while execute the following code:
UINT cDims = 1;
SAFEARRAYBOUND rgsabound[1];
long lLbound = 0;
long lUbound = 0;
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = pList1->rgsabound[0].cElements + pList2->rgsabound[0].cElements;
SAFEARRAY* mergeResult = SafeArrayCreate(VT_DISPATCH, cDims, reinterpret_cast<SAFEARRAYBOUND*>(rgsabound));
// Obtain bounds information of the SAFEARRAY.
SafeArrayGetLBound(pList2, 1, &lLbound);
SafeArrayGetUBound(pList2, 1, &lUbound);
long lDimSize = lUbound - lLbound + 1;
GoldMineConstantContactCOM::IBounceActivityPtr ptrActivity;
SafeArrayCopy(pList1, &mergeResult);
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = pList1->rgsabound[0].cElements + pList2->rgsabound[0].cElements;
SafeArrayRedim(mergeResult, rgsabound);
for (int i = 0; i < lDimSize; i++)
{
long rgIndices[1];
rgIndices[0] = i;
HRESULT hRes2 = SafeArrayGetElement(pList2, rgIndices, &ptrActivity);
rgIndices[0] = rgIndices[0] + pList1->rgsabound[0].cElements;
HRESULT hRes = SafeArrayPutElement(mergeResult, rgIndices, (void*)&ptrActivity);
}
return mergeResult;
The message I got is: Unhandled exception at 0x774115de : 0xC0000005: Access violation reading location 0x00000004.
Any help will be very helpful!
Thanks in advance
Regards,
Fabian

The & in SafeArrayCopy(pList1, &mergeResult); raised suspicions that the documentation confirmed: The function does not copy from the source array to your allocated one, it overwrites the pointer (leaking it) with a new array of the same dimension. However, your call to SafeArrayRedim appears to take care of (part of) the problem.
Also, you take care of retrieving the lower bound of pList2, but you don't use it during the actual copy.
Then, I'm not sure the smart pointer is used exactly right. I think maybe you should put its declaration inside the loop.
Finally, I think I've found the real culprit: The documentation of SafeArrayPutElement say:
The variant types VT_DISPATCH, VT_UNKNOWN, and VT_BSTR are pointers, and do not require another level of indirection.
Which means you should remove the & in (void*)&ptrActivity.

Related

'proc' undefined when trying to add a system call to xv6

I'm trying to add a "clone" system call to the xv6 os. The call creates a new kernel thread which shares the calling process’s address space. The following is my code in proc.c
int clone(void(*fcn)(void*), void* arg, void* stack)
{
int i, pid;
struct proc *np;
int *myarg;
int *myret;
if((np = allocproc()) == 0)
return -1;
np->pgdir = proc->pgdir; //Here's where it tell's me proc is undefined
np->sz = proc->sz;
np->parent = proc;
*np->tf = *proc->tf;
np->stack = stack;
np->tf->eax = 0;
np->tf->eip = (int)fcn;
myret = stack + 4096 - 2 * sizeof(int *);
*myret = 0xFFFFFFFF;
myarg = stack + 4096 - sizeof(int *);
*myarg = (int)arg;
np->tf->esp = (int)stack + PGSIZE - 2 * sizeof(int *);
np->tf->ebp = np->tf->esp;
np->isthread = 1;
for(i = 0; i < NOFILE; i++)
if(proc->ofile[i])
np->ofile[i] = filedup(proc->ofile[i]);
np->cwd = idup(proc->cwd);
safestrcpy(np->name, proc->name, sizeof(proc->name));
pid = np->pid;
acquire(&ptable.lock);
np->state = RUNNABLE;
release(&ptable.lock);
return pid;
}
Most of the implementations I found look just like this, however, whenever I try to make it tells me that 'proc' is undefined. Most implementations of clone that I've seen look nearly identical, with all of them utilizing proc. I'd be happy to share my sysproc.c code as well if that would help in any way.
Thank you!
This has nothing to do with your system call's implementation because proc global variable is being set by the scheduler right before resuming a selected "runnable" process.
The reason for null would probably be because calling this function from a wrong context.
A system call implementation is expected to be executed from a wrapping function named sys_mysysfunc that syscall function called due to a system call inerrupt initiated by a user application code.
Please share with us your entire implementation flow for additional assistance.

Have a memset issue in C++/CLI

I'm a newbie currently using c++/cli to wrap a few classes that i have used in my .lib file. And I am in dire need of using "memset" in my c++/cli. Anyone here knows how to use memset in c++/cli code?
The c++ code I'm trying to use in my c++/cli code:
memset(&DeviceInfo, 0, sizeof(FS_DEVICE_INFO));
Here's my c++/cli Code where I get the error when I try to use the same memset line from my c++ code:
bool newIFSWDevice::GetDeviceInfo(PFS_DEVICE_INFO pDevInfo)
{
IFSDevice* pDeviceWheel = nullptr;
FS_DEVICE_INFO DeviceInfo;
int x = 0;
while (nullptr != (pDeviceWheel = newFSDeviceEnumerator::EnumerateInstance(x++)))
{
memset(&DeviceInfo, 0, sizeof(FS_DEVICE_INFO)); //error line
pDeviceWheel->GetDeviceInfo(&DeviceInfo);
if (0 == wcscmp(DeviceInfo.Name, FS_DEVICE_WHEEL_PORSCHE_NAME)
break;
I tried using a for loop instead...
for (int i = 0; i <= sizeof(FS_DEVICE_INFO); i++)
FS_DEVICE_INFO[i] = 0;
But it still gives me an error "expression must have a constant value". Help would be much appreciated! :)
As noted in the comments, you're missing the header file #include <string.h>. See the documentation.
It's also worth noting that your for loop to do the clearing has several problems:
for (int i = 0; i <= sizeof(FS_DEVICE_INFO); i++)
FS_DEVICE_INFO[i] = 0;
sizeof(FS_DEVICE_INFO) gives you the size of that struct in bytes, but FS_DEVICE_INFO[i] indexes into an array of structs: [1] would be the second struct in the array, not the second byte! You would need to cast the pointer to char or something similar.
i <= sizeof(FS_DEVICE_INFO): The <= is incorrect. If the struct is 10 bytes large, you'd end up operating on bytes 0 through 10, which is 11 bytes total, stomping on whatever happened to be after the struct.
FS_DEVICE_INFO[i]: FS_DEVICE_INFO is the name of the class, your local variable is DeviceInfo, so this should be DeviceInfo[i]. This is why you're getting the expression must have a constant value error.

Realloc not expanding my array

I'm having trouble implementing realloc in a very basic way.
I'm trying to expand the region of memory at **ret, which is pointing to an array of structs
with ret = realloc(ret, newsize); and based on my debug strings I know newsize is correctly increasing over the course of the loop (going from the original size of 4 to 8 to 12 etc.), but when I do sizeof(ptr) it's still returning the original size of 4, and the things I'm trying to place into the newly allocated space can't be found (I think I've narrowed it down to realloc() which is why I'm formatting the question like this)
I can post the function in it's entirety if the problem isn't immediately evident to you, I'm just trying to not "cheat" with my homework too much (the code is kind of messy right now anyway, with heavy use of printf() for debug).
[EDIT] Alright, so based on your answers I'm failing at debugging my code, so I guess I'll post the whole function so you can tell me more about what I'm doing wrong.
(You can ignore the printf()'s since most of that is debug that isn't even working)
Booking **bookingSelectPaid(Booking **booking) {
Booking **ret = malloc(sizeof(Booking*));
printf("Initial address of ret = %p\n", ret);
size_t i = 0;
int numOfPaid = 0;
while (booking[i] != NULL)
{
if (booking[i]->paid == 1)
{
printf("Paying customer! sizeof(Booking*) = %d\n", (int)sizeof(Booking*));
++numOfPaid;
size_t newsize = sizeof(Booking*) * (numOfPaid + 1);
printf("Newsize = %d\n", (int)newsize);
Booking **temp = realloc(NULL, (size_t)newsize);
if (temp != NULL)
printf("Expansion success! => %p sizeof(new pointer) = %d ret = %p\n", temp, (int)sizeof(temp), ret);
ret = realloc(ret, newsize);
ret[i] = booking[i];
ret[i+1] = NULL;
}
++i;
printf("Sizeof(ret) = %d numOfPaid = %d\n", (int)sizeof(ret), numOfPaid);
}
return ret; }
[EDIT2] --> http://pastebin.com/xjzUBmPg
[EDIT3] Just to be clear, the printf's, the temp pointer and things of that nature are debug, and not part of the intended functionality. The line that is puzzling me is either the one with realloc(ret, newsize); or ret[i] = booking[i]
Basically I know for sure that booking contains a table of structs that ends in NULL, and I'm trying to bring the ones that have a specific value set to 1 (paid) onto the new table, which is what my main() is trying to get from this function... So where am I going wrong?
I think the problem here is that your sizeof(ptr) only returns the size of the pointer, which will depend on your architecture (you say 4, so that would mean you're running a 32-bit system).
If you allocate memory dynamically, you have to keep track of its size yourself.
Because sizeof(ptr) returns the size of the pointer, not the allocated size
Yep, sizeof(ptr) is a constant. As the other answer says, depends on the architecture. On a 32 bit architecture it will be 4 and on a 64 bit architecture it will be 8. If you need more help with questions like that this homework help web site can be great for you.
Good luck.

Memory access violation VC++

I'm getting a memory access violation which i am not able to figure out. I am guessing my syntax might be wrong somewhere. Here is my code
load(double **pDouble)
{
int size;
//pStruct is returned by a method of some object inside load
// arr is an array of double, also member of struct pointed by pStruct.
size = sizeof(pStruct->arr)/sizeof(double);
*pDouble = new double[size];
for(int i = 0 ; i < size; i++)
{
*pDouble[i] = pStruct->arr[i];
//the violation occurs for the second iteration of
// the loop
}
}
What could be causing the access violation?
Array indexing binds more tightly than pointer dereferencing. You probably meant:
(*pDouble)[i] = pStruct->arr[i];
There may be other errors though.

COM: Create a VT_ARRAY with VT_BSTR values

I'm a COM newbie and I think what I have is correct, but the runtime doesn't like it. Any help is much appreciated.
I need to invoke a COM function that takes in a single dimensional array of BSTRs. Specifically, the documentation says the parameter must be:
Function: AddFiles ( [in] VARIANT * filePaths )
filePaths The single-dimensioned array of full paths to each file or
folder. filePaths can be of type
VT_ARRAY|VT_VARIANT, where each entry
is a VT_BSTR, or VT_ARRAY|VT_BSTR.
I have a vector<wstring> myPaths of paths which I want to pass into the function that takes the parameter above. Here's the code I wrote. Calling AddFiles on myComObject results in an AV (myComObject is not null, and I can invoke other methods on it):
...
VARIANT filePaths;
VariantInit( &filePaths );
filePaths.vt = VT_ARRAY|VT_VARIANT;
filePaths.parray = SafeArrayCreateVector( VT_BSTR, 0, (unsigned int) myPaths.size() );
long i = 0;
for( vector<wstring>::iterator it = myPaths.begin();
it != myPaths.end();
it++, i++ )
{
BSTR myPath= SysAllocString(it->c_str());
SafeArrayPutElement( filePaths.parray, &i, myPath);
}
myComObject->AddFiles( &filePaths );
...
The COM object isn't my code and I can't debug into it, but I suspect I'm not creating that array properly - based on the requirement of the AddFiles function and the code I have, anyone have ideas about what I might be doing wrong?
If myComObject->AddFiles can only deal with VT_ARRAY|VT_VARIANT, the following should work too.
VARIANT myPath;
VariantInit(&myPath);
myPath.vt = VT_BSTR;
myPath.bstrVal = SysAllocString(it->c_str());
SafeArrayPutElement(filePaths.parray, &i, &myPath);
Don't you want:
filePaths.vt = VT_ARRAY|VT_BSTR;
Since you're creating a SafeArray of BSTRs?