Release Error

I have a strange error. My debug build works well, but my release build crashes after succesful build. In my code:

1
2
3
4
5
6
7
8
9
10
11
this->MaxFrames = MemAllocator.MaxFrames;

// show me how much byte i need ?
//std::ostringstream asd;

//asd<< ( this->MaxFrames * sizeof(D3DXMATRIX) );

//MessageBox(NULL,asd.str().c_str(),"bilgi",MB_OK);

// allocate memory for the FinalMatrices array
FinalMatrices = new D3DXMATRIX[this->MaxFrames];//ERROR HERE! 


But when i uncomment MessageBox function, it doesn't crash :D What is happening ?


1
2
3
4
5
6
7
8
9
10
this->MaxFrames = MemAllocator.MaxFrames;

std::ostringstream asd;

asd<< ( this->MaxFrames * sizeof(D3DXMATRIX) );

MessageBox(NULL,asd.str().c_str(),"bilgi",MB_OK);

// allocate memory for the FinalMatrices array
FinalMatrices = new D3DXMATRIX[this->MaxFrames];//NO ERROR! 


I'm very confused...

Shouldn't it be:

1
2
3

MessageBox( NULL, asd.str(), "bilgi", MB_OK );


?
There is no error there. Error is bad allocation error.

 
F i n a l M a t r i c e s = n ew D 3 D X MA T R I X [ t h i s - > M a x F r ame s ] ;//ERROR 


But if i write messagebox, no error :)
My apologies tolga gerkci: I obviously missread your post.
What's the value of this->MaxFrames ?
35.
Those bad alloc errors can be pretty complicated. The problem can be in any other part of code with bad class casting or procesing after allocated array space or any other bug with peeing in memory where you can't pee... debug program allocates memory in different way than release so thats why those problems can occure even only in one case. Sorry for not helping. All I wanted to say is - consider checking other parts of code for bugs.
But when i uncomment
 
MessageBox(NULL,asd.str().c_str(),"bilgi",MB_OK);

this code, it doesn't chash. Why? I tried Sleep(1000); instead of messagebox, it crashed again. Why it doesn't chash ? What does MessageBox do?
It is pure magic for me how run time allocation works and why running MessageBox will prevent program from crashing. :)

But still I think the problem is somewhere else and not in MessageBox or FinalMatrices allocation. And it can be anywhere.

Does the error message tell you something more? Any other information?
Error:
1
2
Unhandled exception at 0x7c812aeb in dx_deneme_2.exe: Microsoft C++
exception: std::bad_alloc at memory location 0x0012fda0..


It must be magic :)
Any help?
Either you are trying to allocate too much memory (like a negative value), or you have a leak.

By the way
1
2
3
4
5
6
int WINAPI MessageBox(
  __in_opt  HWND hWnd,
  __in_opt  LPCTSTR lpText, //¿ typedef const char* LPCTSTR; ?
  __in_opt  LPCTSTR lpCaption,
  __in      UINT uType
);
I checked my code but i have no leak. My program trying to allocate 2kb memory. I have 4gb ram :D
and std::string::c_str() method returns a LPCSTR(long pointer C STRing)

I tried using sleep instead of messagebox but i get error. MessageBox is a magician i think :D
closed account (zb0S216C)
Sounds like the internal pointer within asd is null. Check to see if asd has characters within it before continuing.

Wazzak
If I use this code:

1
2
this->MaxFrames = MemAllocator.MaxFrames;
FinalMatrices = new D3DXMATRIX[this->MaxFrames];//ERROR HERE!  


I get bad_alloc error in RELEASE MODE.(debug mode works fine :D)

If i use this code:

1
2
3
4
5
this->MaxFrames = MemAllocator.MaxFrames;
std::ostringstream asd;
asd<< ( this->MaxFrames * sizeof(D3DXMATRIX) );
MessageBox(NULL,asd.str().c_str(),"bilgi",MB_OK);
FinalMatrices = new D3DXMATRIX[this->MaxFrames];//NO ERROR!  


I get NO ERROR.

So, my asd pointer is not null and messagebox works fine. it says i need 2kb ram. But when i comment messagebox i get bad_alloc error ..........
1
2
3
this->MaxFrames = MemAllocator.MaxFrames;
cerr << "Trying to allocate " << thist->MaxFrames << '\n';
FinalMatrices = new D3DXMATRIX[this->MaxFrames]; 

1
2
3
4
5
6
7
this->MaxFrames = MemAllocator.MaxFrames;
cerr << "Before " << thist->MaxFrames << '\n';
std::ostringstream asd;
asd<< ( this->MaxFrames * sizeof(D3DXMATRIX) );
MessageBox(NULL,asd.str().c_str(),"bilgi",MB_OK);
cerr << "After " << thist->MaxFrames << '\n';
FinalMatrices = new D3DXMATRIX[this->MaxFrames];


std::string::c_str() method returns a LPCSTR
but the function asks for LPCTSTR, ¿?
closed account (zb0S216C)
If that message box is really keeping your program stable, you have some notable design issues (possibly algorithm issues). I would reconsider a redesign of your function.

Your code looks a mess, by the way (no offence).

Wazzak
Last edited on
@ne555
i tried your code. No changes... And using just cerr don't prevent this error but messagebox...

And c_str returns:
Pointer to an internal array containing the c-string equivalent to the string content.

@framework
I checked my class. No leak, no error, no problem... Msdn says ok :)

... I will rewrite my class ...
I wanted to know the output...
Before 35
After 35
Topic archived. No new replies allowed.