MrEARTHSHAcKER
Programmer
- Jan 26, 2012
- 20
Hi,
My problem is allocation failure.
Namely, I need to dynamically create 39 images, load pictures for them, add OnClick action and arrange them in Form.
Everything works perfectly through this code:
More details about this: from Main form, I dynamically call form where I should do all of this stuff. It works perfectly when I do it first time after launching app.
But when I close that form (TicketTab), and start it again from Main form, I get error:
And I think it is bad allocation because of three things:
1.Doesn't happen always, sometimes after 2nd or 3rd launch;
2.Considering Borland doesn't terminate app after this, I can "continue" running it, and it opens TicketTab, but no images were created.
3.After Borland "pauses" app, this line gets highlighted:
Does anyone have the idea of decreasing possibility of allocation failure?
Thanks!
My problem is allocation failure.
Namely, I need to dynamically create 39 images, load pictures for them, add OnClick action and arrange them in Form.
Everything works perfectly through this code:
Code:
class MyImage{ //Class groups Index and *image, I need Index because of
//ImageClickEvent function, but it ain't matter now
public:
TImage *image;
int Index;
MyImage(){
image=new TImage(TicketTab); } //When object is created, it allocates *image
~MyImage(){ delete image;}
void __fastcall ImageClickEvent(TObject* Sender);
};
void __fastcall MyImage::ImageClickEvent(TObject* Sender){ //This function is being passed to default function
image->Picture->LoadFromFile(
Path->TexturesPath()+"\\TicketTab\\Numbers\\x"+IntToStr(Index+1)+".bmp");
//some code
}
}
#define MAX 39 //Number of pictures
MyImage *MyImages[MAX];
//---------------------------------------------------------------------------
void __fastcall TTicketTab::FormCreate(TObject *Sender)
{
//--------------------------------
int _Top=144; //Starting coordinates
int _Left=184;
for(int i=0;i<MAX;++i); //Allocate memory for 39 objects
MyImages[i]=new MyImage;
for(int i=0;i<MAX;++i){ //Everything in this loop is just setting propertis and position of images
MyImages[i]->Index=i;
MyImages[i]->image->Parent=this;
MyImages[i]->image->Picture->LoadFromFile(
Path->TexturesPath()+"\\TicketTab\\Numbers\\"+IntToStr(i+1)+".bmp");
MyImages[i]->image->Width=25;
MyImages[i]->image->Height=25;
MyImages[i]->image->Stretch=true;
MyImages[i]->image->Visible=true;
MyImages[i]->image->OnClick=MyImages[i]->ImageClickEvent;
MyImages[i]->image->Top=_Top;
MyImages[i]->image->Left=_Left;
if((_Left==248 && _Top==272) ||(_Left==352 && _Top==272)){
_Left+=40;
_Top=144;
}
else if(_Left==248||_Left==352||_Left==456){
_Left-=64;
_Top+=32;
}
else if(_Left==456 && _Top==208)
break;
else _Left+=32;
}
More details about this: from Main form, I dynamically call form where I should do all of this stuff. It works perfectly when I do it first time after launching app.
But when I close that form (TicketTab), and start it again from Main form, I get error:
And I think it is bad allocation because of three things:
1.Doesn't happen always, sometimes after 2nd or 3rd launch;
2.Considering Borland doesn't terminate app after this, I can "continue" running it, and it opens TicketTab, but no images were created.
3.After Borland "pauses" app, this line gets highlighted:
Code:
MyImage(){
image=new TImage(TicketTab); }
Does anyone have the idea of decreasing possibility of allocation failure?
Thanks!