views:

78

answers:

1

I have a structure defined in my header file:

struct video
{
 wchar_t* videoName;
 std::vector<wchar_t*> audio;
 std::vector<wchar_t*> subs;
};
struct ret
{
std::vector<video*> videos;
wchar_t* errMessage;
};
struct params{
 HWND form;
 wchar_t* cwd;
 wchar_t* disk;
 ret* returnData;
};

When I try to add my video structure to a vector of video* I get access violation reading 0xcdcdcdc1 (videoName is @ 0xcdcdcdcd, before I allocate it)

//extract of code where problem is
video v;
v.videoName = (wchar_t*)malloc((wcslen(line)+1)*sizeof(wchar_t));
wcscpy(v.videoName,line);
p->returnData->videos.push_back(&v); //error here
+3  A: 

I would guess that either p or p->returnData is an uninitialized/invalid pointer.

Also, this isn't causing your crash, but it will once you fix the current problem: beware of returning the pointer to a local variable. Once your function goes out of scope the local vector will be destroyed and &v will be an invalid pointer. If you want your vector to exist beyond the scope of the current function then you will need to allocate it on the heap:

vector *v = new video();
...
p->returnData->videos.push_back(v);
John Kugelman
Additionally he adds a pointer to a local variable which may also cause problems!
rstevens
The real solution is to ditch the pointers. There's no purpose of using them but to create extra work.
GMan
Thanks the p->returnData= (ret*)malloc(sizeof(ret));wasn't the right way of doing it, I did a new ret() and it worked great
Also I need pointers because this is in a thread and am going to pass it back to the other thread that's the reason for so many pointers
@GMan: ...and extra bugs...
John Dibling
@james: You only need one pointer, then. Just return a pointer to an allocated "return data" structure, *nothing else needs to be a pointer.*
GMan
thanks for pointing that out I created a new video pointer so wouldn't go out of scope