INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

BorlandBuilderC++ Allocation Failure

BorlandBuilderC++ Allocation Failure

(OP)
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 --> C++

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 --> C++

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

Does anyone have the idea of decreasing possibility of allocation failure?


Thanks!

RE: BorlandBuilderC++ Allocation Failure

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.

RE: BorlandBuilderC++ Allocation Failure

(OP)
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.

RE: BorlandBuilderC++ Allocation Failure

(OP)
*Update*

I removed constructor

CODE --> C++

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 --> c++

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?

RE: BorlandBuilderC++ Allocation Failure

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...

RE: BorlandBuilderC++ Allocation Failure

(OP)
Hmm.. let me provide more informations.

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

CODE --> C++

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.

RE: BorlandBuilderC++ Allocation Failure

(OP)

Quote (ArkM)

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

RE: BorlandBuilderC++ Allocation Failure

(OP)
I found solution for problem!

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


CODE --> C++

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


I hope someone finds this helpful!!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close