Reputation: 69
I am working on a project with SFML that involves a lot of menus with a lot of buttons, so I am creating functions to take minimal input and automatically create and format these buttons. I had this working splendidly when the function called for already constructed buttons as parameters, but I want to simplify this to take strings, which will be used to construct buttons, which will be stored in a vector. When I tried doing this, I recieved this error:
Unhandled exception at 0x76a7c41f in Menu.exe: Microsoft C++ exception:
std::bad_alloc at memory location 0x003cd0a0..
and I am pointed to this in dbgheap.c:
for (;;)
{
/* do the allocation
*/
here>>> pvBlk = _heap_alloc_dbg_impl(nSize, nBlockUse, szFileName, nLine, errno_tmp);
if (pvBlk)
{
return pvBlk;
}
if (nhFlag == 0)
{
if (errno_tmp)
{
*errno_tmp = ENOMEM;
}
return pvBlk;
}
/* call installed new handler */
if (!_callnewh(nSize))
{
if (errno_tmp)
{
*errno_tmp = ENOMEM;
}
return NULL;
}
/* new handler was successful -- try to allocate again */
}
Here is my code, and what I changed.
This function provides no errors:
void page::addLeft(int n, ...)
{
va_list left;
va_start(left, n);
for (int i = 0; i < n; i++)
{
leftButtons.push_back(va_arg(left, button));
//takes parameters of type "button", a working custom class
}
va_end(left);
}
This function gives me the unhandled exception: std::bad_alloc
void page::addLeft(int n, ...)
{
va_list left;
va_start(left, n);
for (int i = 0; i < n; i++)
{
std::string s = va_arg(left, std::string);
//takes a parameter of type "string" and uses it in the default constructor
//of button. the default constructor for button works.
leftButtons.push_back(button(s));
}
va_end(left);
}
I am very new to SFML, but I don't think that's the problem here. Any and all help is appreciated.
Upvotes: 1
Views: 746
Reputation: 84
va_arg doesn't work with std::string. So after first iteration of for loop we are going to reference unknown memory. One way to make your example work is as follows:
void page::addLeft(int n, ...)
{
va_list left;
va_start(left, n);
for (int i = 0; i < n; i++)
{
std::string s = va_arg(left, const char *);
//takes a parameter of type "string" and uses it in the default constructor
//of button. the default constructor for button works.
leftButtons.push_back(button(s));
}
va_end(left);
}
Upvotes: 2