我有一个名为wszBuffer
的全局变量
我在功能(HeapAlloc
)中someFunc()
,在另一个功能(HeapFree
)中anotherFunc()
我主要调用函数,但是在调用anotherFunc()
它有一些垃圾,还有我的另一个HeapAllocated变量的值
类似这样的东西:
但是如果我打电话给anotherFunc_with_return()
而不是打电话给anotherFunc()
就可以了
但是我不想在函数中有返回值
有什么建议吗?
示例代码:
// Global
LPWSTR wszBuffer;
VOID someFunc()
{
DWORD dwSize = 0;
// doing somthing and calculate dwSize...
wszBuffer = (LPWSTR) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize);
// continue...
}
VOID anotherFunc()
{
// do somthing with wszBuffer and then free it
if (wszBuffer != NULL)
{
HeapFree(GetProcessHeap(), NULL, wszBuffer);
wszBuffer = NULL;
}
}
LPWSTR anotherFunc_with_return()
{
// do somthing with wszBuffer and then free it
if (wszBuffer != NULL)
{
HeapFree(GetProcessHeap(), NULL, wszBuffer);
wszBuffer = NULL;
}
return wszBuffer;
}
int main()
{
someFunc();
anotherFunc(); // It's not good
// It's OK
wszBuffer = anotherFunc_with_return();
return 0;
}