Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BorlandBuilderC++ Allocation Failure

Status
Not open for further replies.

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:

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:
errorrw.gif


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!
 
Code:
for(int i=0;i<MAX;++i);          //Allocate memory for 39 objects
MyImages[i]=new MyImage;
Look at semicolon after loop header.
You DO NOT create objects in this loop: it has an empty body.
It's a very strange code: there is (modern) C++ error (undefined i) in the 2nd statement.
 
Hi ArkM,

I don't know from where did that semicolon appear. Probably during copy-paste and deleting not necessary stuff from this piece of code.
It'd show me that variable 'i' was not defined, before compiling, just as you said.

However, do you have any explanation about error that occurs?

Thanks, and sorry about code, my fault.

 
*Update*

I removed constructor

Code:
MyImage(){
        image=new TImage(TicketTab); }

So it doesn't allocate memory when object of MyImage is created, but I have put allocation process into for loop:


Code:
for(int i=0;i<MAX;++i){

        MyImages[i]->image=new TImage(TicketTab); //added line
        MyImages[i]->Index=i;
        MyImages[i]->image->Parent=this;
        //rest of code in for loop

After that, it seems that app shows error less frequently - it appears after 3rd opening of TicketTab.

Anyone experienced with this?
 
It seems the error is outside these snippets context.
The second code variant is exactly the same as the first one. It's useless code refactoring.
Insufficient info: we can't see MyImages array and Path pointer definitions, we know nothing about TiketTab scope etc...
 
Hmm.. let me provide more informations.

TicketTab is "available form" which is being called using this code:

Code:
 TicketTab=new TTicketTab(Applicaton);
this->Hide();
TicketTab->ShowModal();


"this" presents "Auto-create form" MainTab, and its button calls TicketTab using code from above.

If we'd isolate these two forms and remove absolutely all additional headers (headers of mine, necessary for app ). We'd keep same behaviour.

Here is my test-project which explains this.
 
 http://www.mediafire.com/?2t416221a82pkt6
ArkM said:
Insufficient info: we can't see MyImages array and Path pointer definitions, we know nothing about TiketTab scope etc...

In project I provided in last post, you don't need Path pointer definition, just change default location of images (which are in the same folder as project, but however, change the path).

I just asked if someone has had this experience, since I see it is an awkward error, so ArkM if you don't see any solution on first check, don't bother with it. [thumbsup2]
 
I found solution for problem!

Just replace "TicketTab" with "this". Here:


Code:
 MyImage(){
        image=new TImage(TicketTab); }


I hope someone finds this helpful!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top