_Free_base因某些未知原因导致错误

时间:2015-07-11 15:42:32

标签: c++ winapi

我的程序调用了一个名为_free_base的函数,但是我的程序的任何部分都没有。当我调试它时,我发现错误发生在下面的代码片段中,但我无法隔离确切的位置:

HWND CreateComboBox(HWND hwnd, vector<Product> products, int charWidth, int charHeight)
{
  HWND result = CreateWindow(WC_COMBOBOX, TEXT(""),
      CBS_DROPDOWN | CBS_HASSTRINGS | WS_VISIBLE | WS_OVERLAPPED | WS_CHILD,
    102 + 2 * charWidth, (products.size() + 1) * charHeight, 
    100, 100, hwnd, NULL,
    (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);

  for (unsigned int iii = 0; iii < products.size(); iii++)
  {
    ComboBox_AddString(result, products[iii].name);
  }

  ComboBox_SetCurSel(result, 0);

  return result;
}

这是我的程序中发生错误的部分。 VS调用栈声明要调用的下一个函数是return result部分。这意味着它发生在ComboBox_SetCurSel期间?但当我删除该部分时,下一个要调用的函数仍然是return result。这就是为什么我无法隔离确切的错误位置的原因。在调用堆栈上,此文件上方的函数是此Microsoft文件:

free.c

    void __cdecl _free_base (void * pBlock)
    {

      int retval = 0;


      if (pBlock == NULL)
          return;

      RTCCALLBACK(_RTC_Free_hook, (pBlock, 0));

      retval = HeapFree(_crtheap, 0, pBlock);
      if (retval == 0)
      {
          errno = _get_errno_from_oserr(GetLastError());
      }  
    }

错误发生在retval = HeapFree...

最后,VS中的“输出”窗口发布了此Unhandled exception at 0x77133873 (ntdll.dll) in Inventory Program.exe: 0xC0000374: A heap has been corrupted (parameters: 0x7714CDD8).

我是新来的,所以如果您需要更多信息,请在评论中提问,不要评价我的问题。感谢。

哦,是的,这里是调用堆栈:

ntdll.dll!_RtlReportCriticalFailure@8()

ntdll.dll!_RtlpReportHeapFailure@4()    Unknown

ntdll.dll!_RtlpLogHeapFailure@24()  Unknown

ntdll.dll!_RtlFreeHeap@12() Unknown

kernel32.dll!_HeapFree@12() Unknown

>msvcr120.dll!free(void * pBlock) Line 51

Inventory Program.exe!CreateComboBox(HWND__ * hwnd, std::vector<Product,std::allocator<Product> > products, int charWidth, int charHeight) Line 74

Inventory Program.exe!WndProc(HWND__ * hwnd, unsigned int message,  unsigned int wParam, long lParam) Line 139  C++

[External Code] 

Inventory Program.exe!WinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, char * lpCmdLine, int iCmdShow) Line 48 C++

[External Code] 

编辑:这是我的产品类

product.h

#ifndef STOCK_H
#define STOCK_H
#include <vector>
#include <iostream>
#include <tchar.h>

using namespace std;

class Product
{
 public:
 TCHAR* name;
 double price;
 int stock;

 Product(string, double, int);

 ~Product();

 void sell(int sold);
 void stockup(int stocked);

};

void initProductList(vector<Product> &p_list);

void saveProductData(vector<Product> &p_list);

#endif

product.cpp

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include "product.h"

using namespace std;

Product::Product(string a, double b, int c)
{
    const char* cStringForm = a.c_str();

    name = new TCHAR[strlen(cStringForm) + 1];

    name[a.size()] = 0;

    copy(a.begin(), a.end(), name);

    price = b;
    stock = c;
}

Product::~Product()
{
    delete[] name;
}

void Product::sell(int sold)
{
    stock -= sold;
}

void Product::stockup(int stocked)
{
    stock += stocked;
}

void initProductList(vector<Product> &p_list)
{
    //Fills the p_list vector with Product objects

    p_list.reserve(50);

    //There is no file which contains the amount of products
    //It is placed in the first line of the product_name_file
    ifstream product_name_file("C:\\Users\\Jonathan\\My Programs\\Projects\\Inventory Program\\product_names.txt");
    ifstream product_price_file("C:\\Users\\Jonathan\\My Programs\\Projects\\Inventory Program\\product_prices.txt");
    ifstream product_stock_file("C:\\Users\\Jonathan\\My Programs\\Projects\\Inventory Program\\product_stocks.txt");
    int product_quantity(0);
    product_name_file >> product_quantity;

    for (int iii = 0; iii < product_quantity; iii++)
    {
        string name;
        double price;
        int stock;

        product_name_file >> name;
        product_price_file >> price;
        product_stock_file >> stock;

        Product a(name, price, stock);

        p_list.push_back(a);
    }

    product_name_file.close();
    product_price_file.close();
    product_stock_file.close();

}

void saveProductData(vector<Product> &p_list)
{
    ofstream productName("C:\\Users\\Jonathan\\My Programs\\Projects\\Inventory Program\\product_names.txt", 
    ofstream::out | ofstream::trunc);
    ofstream productPrice("C:\\Users\\Jonathan\\My Programs\\Projects\\Inventory Program\\product_prices.txt",
    ofstream::out | ofstream::trunc);
    ofstream productStock("C:\\Users\\Jonathan\\My Programs\\Projects\\Inventory Program\\product_stocks.txt",
    ofstream::out | ofstream::trunc);

    productName << p_list.size() << "\n";

    for (unsigned int iii = 0; iii < p_list.size(); iii++)
    {
        productName << p_list[iii].name << "\n";
        productName.flush();
        productPrice << p_list[iii].price << "\n";
        productPrice.flush();
        productStock << p_list[iii].stock << "\n";
        productStock.flush();
    }
    productName.close();
    productPrice.close();
    productStock.close();
}

0 个答案:

没有答案