<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Arbeitsspeicher läuft über die Zweite :)]]></title><description><![CDATA[<p>Hi Leute,<br />
Seht euch meinen Code mal an- das einzige, was ich praktizieren möchte ist, dass ein Männchen über den Bildschirm läuft.<br />
Doch irgendwie ist der RAM von mir schon nach wenigen Sekunden komplett voll, weil ich im Thread bei jedem schleifendurchlauf den Buffer (damits nicht flimmert ihr wisst schon) mit nem neuen Bild befülle.<br />
Leider weiß ich nicht, wie ich den lösche und überhaupt hab ich das Gefühl, dass das irgendwie nicht der richtige Weg ist, denn mit &quot;delete buffer&quot; gehts nicht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /><br />
Ich würd mich freuen, wenn ihr euch den Code mal ansehen könntet und mir zeigen, was ich verändern muss damit es läuft <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /><br />
Danke<br />
David</p>
<p>PS:</p>
<pre><code>#include &lt;vcl.h&gt;
#pragma hdrstop
#include &quot;Unit1.h&quot;
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;
Thread *Insthread;
int speed=0;
boolean rechts=false;
boolean links=false;
Graphics::TBitmap * hintergrund = new Graphics::TBitmap;
Graphics::TBitmap * buffer = new Graphics::TBitmap;
Graphics::TBitmap * bild = new Graphics::TBitmap;

Thread * Thread1 = new Thread(true);

__fastcall Thread::Thread(bool CreateSuspended)
        : TThread(CreateSuspended)
{

}
void __fastcall Thread::Execute()
{
   int posx=0;
   int posy=400;
   int a=0;
   hintergrund-&gt;LoadFromFile(&quot;bild2.bmp&quot;);
   bild-&gt;LoadFromFile(&quot;bild.bmp&quot;);
   try
   {
      while(a&lt;1000)
      {
               buffer-&gt;Width=1000;
               buffer-&gt;Height=1000;
               bild-&gt;Transparent=true;
               bild-&gt;TransparentColor=bild-&gt;Canvas-&gt;Pixels[0][0];
               buffer-&gt;Canvas-&gt;Draw(0,0,hintergrund);
               buffer-&gt;Canvas-&gt;Draw(posx,posy,bild);
               Form1-&gt;Canvas-&gt;Draw(0,0,buffer);
               Sleep(1);
                a++;
               if(rechts==true)
               {
                  posx=posx+speed;
                  speed++;
                  if(speed&gt;=3)
                     speed=3;
               }
               if(links==true)
               {
                  posx=posx-speed;
                  speed++;
                  if(speed&gt;=3)
                     speed=3;
               }
      }
   }
               catch(Exception &amp;E)
            {

            }
}

__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{

}
void __fastcall TForm1::FormActivate(TObject *Sender)
{
   Insthread= Thread1;
   Thread1-&gt;Resume();
   Form1-&gt;Height = 600;
   Form1-&gt;Width= 900;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &amp;Key,
      TShiftState Shift)
{
   Form1-&gt;Caption=links;
   if(Key==37)
   {
      rechts=false;
      links=true;
      speed=0;
   }
   if(Key==39)
   {
       rechts=true;
       links=false;
       speed=0;
   }
   if(rechts==true &amp;&amp; links==false)
      bild-&gt;LoadFromFile(&quot;bild3.bmp&quot;);
   if(links==true &amp;&amp; rechts == false)
      bild-&gt;LoadFromFile(&quot;bild.bmp&quot;);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &amp;Key,
      TShiftState Shift)
{
   rechts=false;
   links=false;
   speed=0;
}
//---------------------------------------------------------------------------
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/160541/arbeitsspeicher-läuft-über-die-zweite</link><generator>RSS for Node</generator><lastBuildDate>Tue, 11 Aug 2026 06:16:09 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/160541.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 26 Sep 2006 18:21:15 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Arbeitsspeicher läuft über die Zweite :) on Tue, 26 Sep 2006 18:21:15 GMT]]></title><description><![CDATA[<p>Hi Leute,<br />
Seht euch meinen Code mal an- das einzige, was ich praktizieren möchte ist, dass ein Männchen über den Bildschirm läuft.<br />
Doch irgendwie ist der RAM von mir schon nach wenigen Sekunden komplett voll, weil ich im Thread bei jedem schleifendurchlauf den Buffer (damits nicht flimmert ihr wisst schon) mit nem neuen Bild befülle.<br />
Leider weiß ich nicht, wie ich den lösche und überhaupt hab ich das Gefühl, dass das irgendwie nicht der richtige Weg ist, denn mit &quot;delete buffer&quot; gehts nicht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /><br />
Ich würd mich freuen, wenn ihr euch den Code mal ansehen könntet und mir zeigen, was ich verändern muss damit es läuft <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /><br />
Danke<br />
David</p>
<p>PS:</p>
<pre><code>#include &lt;vcl.h&gt;
#pragma hdrstop
#include &quot;Unit1.h&quot;
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;
Thread *Insthread;
int speed=0;
boolean rechts=false;
boolean links=false;
Graphics::TBitmap * hintergrund = new Graphics::TBitmap;
Graphics::TBitmap * buffer = new Graphics::TBitmap;
Graphics::TBitmap * bild = new Graphics::TBitmap;

Thread * Thread1 = new Thread(true);

__fastcall Thread::Thread(bool CreateSuspended)
        : TThread(CreateSuspended)
{

}
void __fastcall Thread::Execute()
{
   int posx=0;
   int posy=400;
   int a=0;
   hintergrund-&gt;LoadFromFile(&quot;bild2.bmp&quot;);
   bild-&gt;LoadFromFile(&quot;bild.bmp&quot;);
   try
   {
      while(a&lt;1000)
      {
               buffer-&gt;Width=1000;
               buffer-&gt;Height=1000;
               bild-&gt;Transparent=true;
               bild-&gt;TransparentColor=bild-&gt;Canvas-&gt;Pixels[0][0];
               buffer-&gt;Canvas-&gt;Draw(0,0,hintergrund);
               buffer-&gt;Canvas-&gt;Draw(posx,posy,bild);
               Form1-&gt;Canvas-&gt;Draw(0,0,buffer);
               Sleep(1);
                a++;
               if(rechts==true)
               {
                  posx=posx+speed;
                  speed++;
                  if(speed&gt;=3)
                     speed=3;
               }
               if(links==true)
               {
                  posx=posx-speed;
                  speed++;
                  if(speed&gt;=3)
                     speed=3;
               }
      }
   }
               catch(Exception &amp;E)
            {

            }
}

__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{

}
void __fastcall TForm1::FormActivate(TObject *Sender)
{
   Insthread= Thread1;
   Thread1-&gt;Resume();
   Form1-&gt;Height = 600;
   Form1-&gt;Width= 900;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &amp;Key,
      TShiftState Shift)
{
   Form1-&gt;Caption=links;
   if(Key==37)
   {
      rechts=false;
      links=true;
      speed=0;
   }
   if(Key==39)
   {
       rechts=true;
       links=false;
       speed=0;
   }
   if(rechts==true &amp;&amp; links==false)
      bild-&gt;LoadFromFile(&quot;bild3.bmp&quot;);
   if(links==true &amp;&amp; rechts == false)
      bild-&gt;LoadFromFile(&quot;bild.bmp&quot;);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &amp;Key,
      TShiftState Shift)
{
   rechts=false;
   links=false;
   speed=0;
}
//---------------------------------------------------------------------------
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1145012</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1145012</guid><dc:creator><![CDATA[777]]></dc:creator><pubDate>Tue, 26 Sep 2006 18:21:15 GMT</pubDate></item><item><title><![CDATA[Reply to Arbeitsspeicher läuft über die Zweite :) on Tue, 26 Sep 2006 18:58:47 GMT]]></title><description><![CDATA[<p>Wie wärs, wenn du die Bilder nur einmal beim Start lädst und nicht jedesmal neu. Dann zeigst du halt bei Bedarf das jeweils richtige an. Mach die Bitmaps zu Membervariablen deiner Threadklasse. Das new kommt dann in den Konstruktor und ein dazugehöriges delete in den Destruktor. Entferne überhaupt alle globalen Variablen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1145047</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1145047</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Tue, 26 Sep 2006 18:58:47 GMT</pubDate></item><item><title><![CDATA[Reply to Arbeitsspeicher läuft über die Zweite :) on Tue, 26 Sep 2006 20:47:32 GMT]]></title><description><![CDATA[<p>genau und gegen das flimmer kann man im notfall auch noch was mit doublebuffered anfangen (oder ähnlich fkt)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1145134</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1145134</guid><dc:creator><![CDATA[ky_fr34k]]></dc:creator><pubDate>Tue, 26 Sep 2006 20:47:32 GMT</pubDate></item><item><title><![CDATA[Reply to Arbeitsspeicher läuft über die Zweite :) on Wed, 27 Sep 2006 12:28:29 GMT]]></title><description><![CDATA[<p>Mein neuer Quelltext sieht nun folgendermaßen aus:</p>
<pre><code>#include &lt;vcl.h&gt;
#pragma hdrstop
#include &quot;Unit1.h&quot;
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;
Thread *Insthread;
int speed=0;
boolean rechts=false;
boolean links=false;
Graphics::TBitmap * hintergrund = new Graphics::TBitmap;
Graphics::TBitmap * buffer = new Graphics::TBitmap;
Graphics::TBitmap * bild = new Graphics::TBitmap;
   int posx=0;
   int posy=400;

Thread * Thread1 = new Thread(true);

__fastcall Thread::Thread(bool CreateSuspended)
        : TThread(CreateSuspended)
{

}
void __fastcall Thread::Execute()
{
      while(!Thread1-&gt;Terminated)
      {
               buffer-&gt;Width=1000;
               buffer-&gt;Height=1000;
               bild-&gt;Transparent=true;
               bild-&gt;TransparentColor=bild-&gt;Canvas-&gt;Pixels[0][0];
               buffer-&gt;Canvas-&gt;Draw(0,0,hintergrund);
               buffer-&gt;Canvas-&gt;Draw(posx,posy,bild);
               Form1-&gt;Canvas-&gt;Draw(0,0,buffer);
               Sleep(1);
               if(rechts==true)
               {
                  posx=posx+speed;
                  speed++;
                  if(speed&gt;=3)
                     speed=3;
               }
               if(links==true)
               {
                  posx=posx-speed;
                  speed++;
                  if(speed&gt;=3)
                     speed=3;
               }
      }
}

__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{

}
void __fastcall TForm1::FormActivate(TObject *Sender)
{
   Insthread= Thread1;
   Thread1-&gt;Resume();
   Form1-&gt;Height = 600;
   Form1-&gt;Width= 900;
   hintergrund-&gt;LoadFromFile(&quot;bild2.bmp&quot;);
   bild-&gt;LoadFromFile(&quot;bild.bmp&quot;);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &amp;Key,
      TShiftState Shift)
{
   Form1-&gt;Caption=links;
   if(Key==37)
   {
      rechts=false;
      links=true;
      speed=0;
   }
   if(Key==39)
   {
       rechts=true;
       links=false;
       speed=0;
   }
   if(rechts==true &amp;&amp; links==false)
      bild-&gt;LoadFromFile(&quot;bild3.bmp&quot;);
   if(links==true &amp;&amp; rechts == false)
      bild-&gt;LoadFromFile(&quot;bild.bmp&quot;);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &amp;Key,
      TShiftState Shift)
{
   rechts=false;
   links=false;
   speed=0;
}
//---------------------------------------------------------------------------
</code></pre>
<p>Leider bleibt das Spiel nach einer Sekunde irgendwie stehn und wenn ich das sleep rausmache kommt direkt der Fehler, &quot;OutofResources&quot; obwohl der RAM nun leer genug bleibt und die Bitmaps außerhalb des Threads geladen werden...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1145386</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1145386</guid><dc:creator><![CDATA[777]]></dc:creator><pubDate>Wed, 27 Sep 2006 12:28:29 GMT</pubDate></item><item><title><![CDATA[Reply to Arbeitsspeicher läuft über die Zweite :) on Wed, 27 Sep 2006 12:44:56 GMT]]></title><description><![CDATA[<p>Ich bin ja nicht so der Thread-Experte, aber sollte nicht auf Objekte, die nicht Member des Thread sind, mit Hilfe von Synchronize() zugegriffen werden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1145391</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1145391</guid><dc:creator><![CDATA[Morris Szyslak]]></dc:creator><pubDate>Wed, 27 Sep 2006 12:44:56 GMT</pubDate></item><item><title><![CDATA[Reply to Arbeitsspeicher läuft über die Zweite :) on Wed, 27 Sep 2006 12:56:40 GMT]]></title><description><![CDATA[<p>Es muß nicht unbedingt Synchronize sein, aber auf jeden Fall müssen Daten, die sowohl von Form1, als auch vom Thread verwendet werden (in diesem Fall die ganzen globalen Variablen) über ein CriticalSection, oder einen Mutex geschützt werden.<br />
Da ein Teil der Variblen nur in Form1 gesetzt wird und im Thread nur gelesen werden, bietet sich hier ein TMultiReadExclusiveWriteSynchronizer-Objekt an.</p>
<p>Auch darfst Du NICHT aus dem Thread heraus, direkt auf den Canvas von Form1 zeichnen. Auch hier muss ein möglicher gleichzeitiger Zugriff unterbunden werdne.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1145397</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1145397</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Wed, 27 Sep 2006 12:56:40 GMT</pubDate></item><item><title><![CDATA[Reply to Arbeitsspeicher läuft über die Zweite :) on Wed, 27 Sep 2006 13:00:04 GMT]]></title><description><![CDATA[<p>oha- ich werd mir das mal genauer angucken</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1145399</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1145399</guid><dc:creator><![CDATA[777]]></dc:creator><pubDate>Wed, 27 Sep 2006 13:00:04 GMT</pubDate></item><item><title><![CDATA[Reply to Arbeitsspeicher läuft über die Zweite :) on Wed, 27 Sep 2006 13:07:37 GMT]]></title><description><![CDATA[<p>Nicht unbedingt. Synchronize synchronisiert den Thread mit dem Hauptthread, d.h. der thread wird angehalten, bis die darin aufgerufenen Routine beendet ist. Das ist nur notwendig, wenn was im Hauptthread geändert werden soll.<br />
<a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/13375">@777</a><br />
Was haben denn die TForm1 Funktionen hier zu suchen?? Du solltest deine Bilder im Konstruktor von Thread erzeugen und laden. Warum hast du zwei verschiedenen Variablen vom Typ Thread (Thread1 und Insthread)?</p>
<pre><code class="language-cpp">__fastcall Thread::Thread(bool CreateSuspended)
        : TThread(CreateSuspended)
{
  hintergrund = new Graphics::TBitmap;
  buffer = new Graphics::TBitmap;
  bild = new Graphics::TBitmap;
  hintergrund-&gt;LoadFromFile(&quot;bild2.bmp&quot;);
  bild-&gt;LoadFromFile(&quot;bild.bmp&quot;);
// usw.
}

//dann Destruktor
__fastcall Thread::~Thread()
{
  delete hintergrund;
  delete buffer;
  delete bild;
}
</code></pre>
<p>Dazu müssen die Bitmapinstanzen Membervariablen von Thread werden.<br />
Bitt schau dir nochmal ein Tutorial zum Thema objektorientierte Programmierung bzw. Klassen an.<br />
Ich hab heute leider nicht genug Zeit um das hier im Einzelnen durchzugehen.</p>
<p>Ciao</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1145402</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1145402</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Wed, 27 Sep 2006 13:07:37 GMT</pubDate></item><item><title><![CDATA[Reply to Arbeitsspeicher läuft über die Zweite :) on Wed, 27 Sep 2006 17:54:13 GMT]]></title><description><![CDATA[<p>Vielen Dank Leute- mein Programm läuft jetzt mit folgendem Code super <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<pre><code>#include &lt;vcl.h&gt;
#pragma hdrstop
#include &quot;Unit1.h&quot;
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;
Thread *Insthread;
int speed=3;
boolean rechts=false;
boolean links=false;
Graphics::TBitmap * hintergrund = new Graphics::TBitmap;
Graphics::TBitmap * buffer = new Graphics::TBitmap;
Graphics::TBitmap * bild = new Graphics::TBitmap;
   int posx=0;
   int posy=450;

Thread * Thread1 = new Thread(true);

__fastcall Thread::Thread(bool CreateSuspended)
        : TThread(CreateSuspended)
{

}
void __fastcall Thread::Execute()
{
   buffer-&gt;Width=1000;
   buffer-&gt;Height=1000;

  while(!Terminated)
  {
      Synchronize(syncro);
      Sleep(1);
  }
}
void __fastcall Thread::syncro()
{
   bild-&gt;Transparent=true;
   bild-&gt;TransparentColor=bild-&gt;Canvas-&gt;Pixels[0][0];
   buffer-&gt;Canvas-&gt;Draw(0,0,hintergrund);
   buffer-&gt;Canvas-&gt;Draw(posx,posy,bild);
   Form1-&gt;Canvas-&gt;Draw(0,0,buffer);

   if(rechts==true)
   {
      posx=posx+speed;
   }
   if(links==true)
   {
      posx=posx-speed;
   }
}
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{

}
void __fastcall TForm1::FormActivate(TObject *Sender)
{
   Insthread= Thread1;
   Thread1-&gt;Resume();
   Form1-&gt;Height = 600;
   Form1-&gt;Width= 900;
   hintergrund-&gt;LoadFromFile(&quot;bild2.bmp&quot;);
   bild-&gt;LoadFromFile(&quot;bild.bmp&quot;);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &amp;Key,
      TShiftState Shift)
{
   Form1-&gt;Caption=Key;
   if(Key==16)
   {
      speed=6;
   }
   else
   {
      speed=3;
   }

   if(Key==37)
   {
      rechts=false;
      links=true;
   }
   if(Key==39)
   {
       rechts=true;
       links=false;
   }
   if(rechts==true &amp;&amp; links==false)
      bild-&gt;LoadFromFile(&quot;bild3.bmp&quot;);
   if(links==true &amp;&amp; rechts == false)
      bild-&gt;LoadFromFile(&quot;bild.bmp&quot;);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &amp;Key,
      TShiftState Shift)
{
   rechts=false;
   links=false;
}
//---------------------------------------------------------------------------
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1145619</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1145619</guid><dc:creator><![CDATA[777]]></dc:creator><pubDate>Wed, 27 Sep 2006 17:54:13 GMT</pubDate></item></channel></rss>